[win] updatesvc: additional processing when running in app mode

This commit is contained in:
SimplestStudio
2023-09-10 14:32:22 +03:00
parent cde49bba06
commit 80639a8da0
3 changed files with 38 additions and 7 deletions

View File

@ -89,6 +89,7 @@ int __cdecl _tmain (int argc, TCHAR *argv[])
return 0;
} else
if (lstrcmpi(argv[1], _T("--run-as-app")) == 0) {
NS_Utils::setRunAsApp();
CSocket socket(0, INSTANCE_SVC_PORT);
if (!socket.isPrimaryInstance())
return 0;

View File

@ -55,6 +55,18 @@
namespace NS_Utils
{
bool run_as_app = false;
void setRunAsApp()
{
run_as_app = true;
}
bool isRunAsApp()
{
return run_as_app;
}
wstring GetLastErrorAsString()
{
DWORD errorMessageID = ::GetLastError();
@ -78,6 +90,10 @@ namespace NS_Utils
if (showError)
str += L" " + GetLastErrorAsString();
wchar_t *title = const_cast<LPTSTR>(TEXT(VER_PRODUCTNAME_STR));
if (isRunAsApp()) {
MessageBox(NULL, str.c_str(), title, MB_ICONERROR | MB_SERVICE_NOTIFICATION_NT3X | MB_SETFOREGROUND);
return 0;
}
DWORD title_size = (DWORD)wcslen(title) * sizeof(wchar_t);
DWORD res;
DWORD session_id = WTSGetActiveConsoleSessionId();
@ -166,13 +182,7 @@ namespace NS_File
bool runProcess(const wstring &fileName, const wstring &args)
{
DWORD dwSessionId = WTSGetActiveConsoleSessionId();
if (dwSessionId == 0xFFFFFFFF) {
return false;
}
HANDLE hUserToken = NULL;
if (!WTSQueryUserToken(dwSessionId, &hUserToken)) {
if (NS_Utils::isRunAsApp()) {
STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
@ -189,6 +199,16 @@ namespace NS_File
return false;
}
DWORD dwSessionId = WTSGetActiveConsoleSessionId();
if (dwSessionId == 0xFFFFFFFF) {
return false;
}
HANDLE hUserToken = NULL;
if (!WTSQueryUserToken(dwSessionId, &hUserToken)) {
return false;
}
HANDLE hTokenDup = NULL;
if (!DuplicateTokenEx(hUserToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hTokenDup)) {
CloseHandle(hUserToken);
@ -379,6 +399,14 @@ namespace NS_File
wstring tempPath()
{
if (NS_Utils::isRunAsApp()) {
WCHAR buff[MAX_PATH] = {0};
DWORD res = ::GetTempPath(MAX_PATH, buff);
if (res != 0)
return fromNativeSeparators(parentPath(buff));
return L"";
}
DWORD sesId = WTSGetActiveConsoleSessionId();
if (sesId == 0xFFFFFFFF)
return L"";

View File

@ -48,6 +48,8 @@ using std::list;
namespace NS_Utils
{
void setRunAsApp();
bool isRunAsApp();
wstring GetLastErrorAsString();
int ShowMessage(wstring str, bool showError = false);
}