windows上升级前确保app未运行,升级后启动app

This commit is contained in:
luoliangyi 2022-07-02 14:51:58 +08:00
parent e485069d34
commit 4cbf6be1dc
4 changed files with 127 additions and 67 deletions

View File

@ -11,6 +11,8 @@
#include <HGString.h>
#if defined(HG_CMP_MSC)
#include <iphlpapi.h>
#include <shellapi.h>
#include <tlhelp32.h>
#endif
static void GetMacAddrList(std::vector<std::string> &macList)
@ -421,3 +423,115 @@ bool Upgrade(const std::string& pkgPath, const std::string &desc)
PostInfo(1, desc);
return ret;
}
bool AppIsRun()
{
#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 (NULL != 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
return false;
#endif
}
void RunApp()
{
#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(NULL, L"open", appPath.c_str(), NULL, NULL, SW_SHOW);
}
#else
#endif
}

View File

@ -12,4 +12,10 @@ bool PostUninstallInfo(const std::string &desc);
// 升级安装, 使用之前的安装路径
bool Upgrade(const std::string& pkgPath, const std::string &desc);
// 判断app是否在运行
bool AppIsRun();
// 运行app
void RunApp();
#endif /* __HGUPGRADE_H__ */

View File

@ -1,6 +1,7 @@
#include "mainwindow.h"
#include <QApplication>
#include <QThread>
#include <QScreen>
#include "HGUpgrade.h"
#include "curl/curl.h"
@ -37,11 +38,17 @@ int main(int argc, char *argv[])
{
QApplication a(argc, argv);
while (AppIsRun())
QThread::msleep(20);
MainWindow w(pkgPath, desc);
QScreen *screen = QGuiApplication::primaryScreen();
w.move((screen->size().width() - w.width()) / 2, (screen->size().height() - w.height()) / 2);
w.show();
a.exec();
if (!AppIsRun())
RunApp();
}
curl_global_cleanup();

View File

@ -1603,73 +1603,6 @@ namespace ver_1
return ret;
}
static std::string MakePreFileData(const char* pszBoundary, const char* pszRemoteFileName)
{
char data[512];
sprintf(data, "--%s\r\nContent-Disposition: form-data; name=\"filedata\"; filename=\"%s\"\r\n",
pszBoundary, pszRemoteFileName);
std::string ret = data;
ret += "Content-Type: application/octet-stream; charset=utf-8\r\n";
ret += "Content-Transfer-Encoding: binary\r\n";
ret += "\r\n";
return ret;
}
static std::string MakePostFileData(const char* pszBoundary)
{
char data[512];
sprintf(data, "\r\n--%s\r\nContent-Disposition: form-data; name=\"submitted\"\r\n\r\nsubmit\r\n--%s--\r\n", pszBoundary, pszBoundary);
return data;
}
static void ParseHttpURL(const std::string& url, std::string& addr, int& port, std::string& path)
{
addr.clear();
port = 0;
path.clear();
std::string url2;
std::string::size_type pos = url.find("//");
if (std::string::npos != pos)
{
std::string protocal = url.substr(0, pos);
if (protocal != "http:")
{
return;
}
url2 = url.substr(pos + 2);
}
else
{
url2 = url;
}
std::string addr_port;
pos = url2.find("/");
if (std::string::npos != pos)
{
addr_port = url2.substr(0, pos);
path = url2.substr(pos);
}
else
{
addr_port = url2;
}
pos = addr_port.find(":");
if (std::string::npos != pos)
{
addr = addr_port.substr(0, pos);
port = atoi(addr_port.substr(pos + 1).c_str());
}
else
{
addr = addr_port;
port = 80;
}
}
bool ManagerV1::HTTPUpload(const std::string& localFileName, const std::string& httpUrl, const std::string& remoteFileName,
const std::string& httpMethod, const std::string& header, const std::string& param)
{