code_app/app/upgrade/HGUpgrade.cpp

193 lines
5.5 KiB
C++

#include "HGUpgrade.h"
#include "base/HGDef.h"
#include "base/HGInc.h"
#include "version/HGVersion.h"
#if defined(HG_CMP_MSC)
#include <shellapi.h>
#include <tlhelp32.h>
#endif
bool AppIsRun(const std::string& appName)
{
if (appName == HGVERSION_APPNAME_SCANNER)
{
#if defined(HG_CMP_MSC)
std::wstring appPath;
#if defined(OEM_HANWANG)
std::wstring regName = L"SOFTWARE\\HanvonScan";
#elif defined(OEM_LISICHENG)
std::wstring regName = L"SOFTWARE\\LanxumScan";
#else
std::wstring regName = L"SOFTWARE\\HuaGoScan";
#endif
HKEY hKey = nullptr;
RegOpenKeyExW(HKEY_LOCAL_MACHINE, regName.c_str(), 0, KEY_QUERY_VALUE, &hKey);
if (nullptr != hKey)
{
WCHAR szData[MAX_PATH] = { 0 };
DWORD cbData = MAX_PATH;
if (ERROR_SUCCESS == RegQueryValueExW(hKey, L"Application", nullptr, nullptr, (LPBYTE)szData, &cbData))
{
appPath = szData;
}
RegCloseKey(hKey);
}
if (appPath.empty())
{
return false;
}
bool ret = false;
HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
BOOL bFindFirstProcess = ::Process32First(hSnapshot, &pe);
if (bFindFirstProcess)
{
do
{
WCHAR exeFullPath[1024] = { 0 };
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
if (nullptr != hProcess)
{
DWORD bufferLen = 1024;
::QueryFullProcessImageName(hProcess, 0, exeFullPath, &bufferLen);
::CloseHandle(hProcess);
}
if (0 == wcsicmp(exeFullPath, appPath.c_str()))
{
ret = true;
break;
}
} while (::Process32Next(hSnapshot, &pe));
}
::CloseHandle(hSnapshot);
}
return ret;
#else
std::string cmd;
std::string appPath;
#if defined (UOS)
#if defined(OEM_HANWANG)
cmd = "ps -wef | grep HanvonScan";
appPath = "/opt/apps/com.hanvonchina.hanvonscan/files/bin/HanvonScan";
#elif defined(OEM_LISICHENG)
cmd = "ps -wef | grep LanxumScan";
appPath = "/opt/apps/com.lanxumchina.lanxumscan/files/bin/LanxumScan";
#else
cmd = "ps -wef | grep HuaGoScan";
appPath = "/opt/apps/com.huagaochina.huagoscan/files/bin/HuaGoScan";
#endif
#else
#if defined(OEM_HANWANG)
cmd = "ps -wef | grep HanvonScan";
appPath = "/opt/apps/scanner-driver-hanvon/HanvonScan";
#elif defined(OEM_LISICHENG)
cmd = "ps -wef | grep LanxumScan";
appPath = "/opt/apps/scanner-driver-lanxum/LanxumScan";
#else
cmd = "ps -wef | grep HuaGoScan";
appPath = "/opt/apps/scanner-driver-huagao/HuaGoScan";
#endif
#endif
bool ret = false;
FILE *fp = popen(cmd.c_str(), "r");
if (nullptr != fp)
{
char data[2048] = {0};
while (data == fgets(data, 2048, fp))
{
if (nullptr != strstr(data, appPath.c_str()))
{
ret = true;
break;
}
}
pclose(fp);
}
return ret;
#endif
}
return false;
}
void RunApp(const std::string& appName)
{
if (appName == HGVERSION_APPNAME_SCANNER)
{
#if defined(HG_CMP_MSC)
std::wstring appPath;
#if defined(OEM_HANWANG)
std::wstring regName = L"SOFTWARE\\HanvonScan";
#elif defined(OEM_LISICHENG)
std::wstring regName = L"SOFTWARE\\LanxumScan";
#else
std::wstring regName = L"SOFTWARE\\HuaGoScan";
#endif
HKEY hKey = nullptr;
RegOpenKeyExW(HKEY_LOCAL_MACHINE, regName.c_str(), 0, KEY_QUERY_VALUE, &hKey);
if (nullptr != hKey)
{
WCHAR szData[MAX_PATH] = { 0 };
DWORD cbData = MAX_PATH;
if (ERROR_SUCCESS == RegQueryValueExW(hKey, L"Application", nullptr, nullptr, (LPBYTE)szData, &cbData))
{
appPath = szData;
}
RegCloseKey(hKey);
}
if (!appPath.empty())
{
ShellExecuteW(nullptr, L"open", appPath.c_str(), nullptr, nullptr, SW_SHOW);
}
#else
std::string appPath;
#if defined (UOS)
#if defined(OEM_HANWANG)
appPath = "sh /opt/apps/com.hanvonchina.hanvonscan/files/bin/HanvonScan.sh &";
#elif defined(OEM_LISICHENG)
appPath = "sh /opt/apps/com.lanxumchina.lanxumscan/files/bin/LanxumScan.sh &";
#else
appPath = "sh /opt/apps/com.huagaochina.huagoscan/files/bin/HuaGoScan.sh &";
#endif
#else
#if defined(OEM_HANWANG)
appPath = "sh /opt/apps/scanner-driver-hanvon/HanvonScan.sh &";
#elif defined(OEM_LISICHENG)
appPath = "sh /opt/apps/scanner-driver-lanxum/LanxumScan.sh &";
#else
appPath = "sh /opt/apps/scanner-driver-huagao/HuaGoScan.sh &";
#endif
#endif
system(appPath.c_str());
#endif
}
}