release/win/remove_old_app/HGRemoveOldApp.cpp

217 lines
6.4 KiB
C++

#include <Windows.h>
#include <atlstr.h>
#include <vector>
#include <assert.h>
#include <shlobj.h>
static void SplitString(const CStringA &src, CHAR sep, std::vector<CStringA> &dst)
{
dst.clear();
int len = src.GetLength();
CStringA str;
bool inParam = false;
for (int i = 0; i < len; ++i)
{
if (inParam || src[i] != sep)
{
str.AppendChar(src[i]);
if ('\"' == src[i])
inParam = !inParam;
}
else
{
if (!str.IsEmpty())
{
dst.push_back(str);
str.Empty();
}
}
}
if (!str.IsEmpty())
{
dst.push_back(str);
str.Empty();
}
}
static void RemoveQuotes(const CStringA& src, CStringA &dst)
{
dst = src;
while (dst.GetLength() >= 2 && dst[0] == '\"' && dst[dst.GetLength() - 1] == '\"')
{
dst = dst.Mid(1, dst.GetLength() - 2);
}
}
static CStringA GetAppUninstallString()
{
CStringA strUninstall;
HKEY hKey = NULL;
#if defined(OEM_HANWANG)
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\InstallShield_{D6F0C80E-A058-485D-AA12-49D0656EC2BE}", 0, KEY_QUERY_VALUE, &hKey);
#elif defined(OEM_LISICHENG)
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\InstallShield_{EA98728C-9793-4B3B-8D6C-3FAE94648972}", 0, KEY_QUERY_VALUE, &hKey);
#else
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\InstallShield_{6BE74035-CDC8-4CBF-937C-EBC08A9D6233}", 0, KEY_QUERY_VALUE, &hKey);
#endif
if (NULL != hKey)
{
CHAR szData[MAX_PATH] = { 0 };
DWORD cbData = MAX_PATH;
if (ERROR_SUCCESS == RegQueryValueExA(hKey, "UninstallString", NULL, NULL, (LPBYTE)szData, &cbData))
{
strUninstall = szData;
}
RegCloseKey(hKey);
}
return strUninstall;
}
static int UninstallApp()
{
CStringA strUninstall = GetAppUninstallString();
if (strUninstall.IsEmpty())
{
return 0;
}
int ret = 1;
std::vector<CStringA> cmdList;
SplitString(strUninstall, ' ', cmdList);
if (!cmdList.empty())
{
CStringA exePath;
RemoveQuotes(cmdList[0], exePath);
int pos = exePath.ReverseFind('\\');
if (-1 == pos)
pos = exePath.ReverseFind('/');
if (-1 != pos)
{
CStringA exeDir = exePath.Left(pos + 1);
CHAR tmpPath[MAX_PATH] = { 0 };
GetTempPathA(MAX_PATH, tmpPath);
if (tmpPath[strlen(tmpPath) - 1] != '\\')
strcat_s(tmpPath, "\\");
CStringA tmpDir;
tmpDir.Format("%s{72A421E1-88A8-4D5C-97FA-F76B5B8A05E8}\\", tmpPath);
SHCreateDirectoryExA(NULL, tmpDir, NULL);
CopyFileA(exeDir + "setup.exe", tmpDir + "setup.exe", FALSE);
CopyFileA(exeDir + "ISSetup.dll", tmpDir + "ISSetup.dll", FALSE);
CopyFileA(exeDir + "0x0804.ini", tmpDir + "0x0804.ini", FALSE);
CopyFileA(exeDir + "Setup.ilg", tmpDir + "Setup.ilg", FALSE);
CopyFileA(exeDir + "setup.ini", tmpDir + "setup.ini", FALSE);
PROCESS_INFORMATION ProcessInfo;
STARTUPINFOA StartupInfo;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
char command[256];
sprintf_s(command, "%s%s -l0x0804 -removeonly", (LPCSTR)tmpDir, "setup.exe");
if (CreateProcessA(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
CStringA strUninstall = GetAppUninstallString();
if (strUninstall.IsEmpty())
{
ret = 0;
}
}
}
}
return ret;
}
static CStringA GetDriverUninstallString()
{
CStringA strUninstall;
HKEY hKey = NULL;
#if defined(OEM_HANWANG)
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{3D15346F-3E17-494E-B14B-5F221CD284B4}", 0, KEY_QUERY_VALUE, &hKey);
#elif defined(OEM_LISICHENG)
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{0C317880-F6D9-4293-92F2-0AA9CD260420}", 0, KEY_QUERY_VALUE, &hKey);
#else
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{BDCDADB8-188F-4013-9591-BF231C595731}", 0, KEY_QUERY_VALUE, &hKey);
#endif
if (NULL != hKey)
{
CHAR szData[MAX_PATH] = { 0 };
DWORD cbData = MAX_PATH;
if (ERROR_SUCCESS == RegQueryValueExA(hKey, "UninstallString", NULL, NULL, (LPBYTE)szData, &cbData))
{
strUninstall = szData;
}
RegCloseKey(hKey);
}
return strUninstall;
}
static int UninstallDriver()
{
CStringA strUninstall = GetDriverUninstallString();
if (strUninstall.IsEmpty())
{
return 0;
}
int ret = 1;
PROCESS_INFORMATION ProcessInfo;
STARTUPINFOA StartupInfo;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
char command[256];
#if defined(OEM_HANWANG)
sprintf_s(command, "MsiExec.exe /x {3D15346F-3E17-494E-B14B-5F221CD284B4} /quiet");
#elif defined(OEM_LISICHENG)
sprintf_s(command, "MsiExec.exe /x {0C317880-F6D9-4293-92F2-0AA9CD260420} /quiet");
#else
sprintf_s(command, "MsiExec.exe /x {BDCDADB8-188F-4013-9591-BF231C595731} /quiet");
#endif
if (CreateProcessA(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
CStringA strUninstall = GetDriverUninstallString();
if (strUninstall.IsEmpty())
{
ret = 0;
}
}
return ret;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
int ret = UninstallApp();
if (0 == ret)
{
ret = UninstallDriver();
}
return ret;
}