Skip to content

Commit 059f3f8

Browse files
committed
Flip game process launch logic
Prefer CreateProcessW over ShellExecute
1 parent ea663b8 commit 059f3f8

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

Client/loader/MainFunctions.cpp

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,61 +1046,58 @@ void CheckLibVersions()
10461046
BOOL StartGtaProcess(const SString& lpApplicationName, const SString& lpCommandLine, const SString& lpCurrentDirectory,
10471047
LPPROCESS_INFORMATION lpProcessInformation, DWORD& dwOutError, SString& strOutErrorContext)
10481048
{
1049+
STARTUPINFOW startupInfo{};
1050+
startupInfo.cb = sizeof(startupInfo);
1051+
BOOL wasProcessCreated = CreateProcessW(*FromUTF8(lpApplicationName), FromUTF8(lpCommandLine).data(), nullptr, nullptr, FALSE, 0, nullptr,
1052+
*FromUTF8(lpCurrentDirectory), &startupInfo, lpProcessInformation);
1053+
1054+
if (wasProcessCreated)
1055+
return true;
1056+
10491057
std::vector<DWORD> processIdListBefore = GetGTAProcessList();
1050-
// Start GTA
1051-
BOOL bResult = ShellExecuteNonBlocking("open", lpApplicationName, lpCommandLine, lpCurrentDirectory);
10521058

1053-
if (bResult == FALSE)
1059+
if (!ShellExecuteNonBlocking("open", lpApplicationName, lpCommandLine, lpCurrentDirectory))
10541060
{
1055-
STARTUPINFOW startupInfo{};
1056-
startupInfo.cb = sizeof(startupInfo);
1057-
bResult = CreateProcessW(*FromUTF8(lpApplicationName), FromUTF8(lpCommandLine).data(), nullptr, nullptr, FALSE, 0, nullptr,
1058-
*FromUTF8(lpCurrentDirectory), &startupInfo, lpProcessInformation);
1059-
1060-
if (!bResult)
1061-
{
1062-
dwOutError = GetLastError();
1063-
strOutErrorContext = "CreateProcess";
1064-
}
1061+
dwOutError = GetLastError();
1062+
strOutErrorContext = "ShellExecute";
1063+
return false;
10651064
}
1066-
else
1065+
1066+
// Determine pid of new gta process
1067+
for (uint i = 0; i < 10; i++)
10671068
{
1068-
// Determine pid of new gta process
1069-
for (uint i = 0; i < 10; i++)
1069+
std::vector<DWORD> processIdList = GetGTAProcessList();
1070+
for (DWORD pid : processIdList)
10701071
{
1071-
std::vector<DWORD> processIdList = GetGTAProcessList();
1072-
for (DWORD pid : processIdList)
1072+
if (ListContains(processIdListBefore, pid))
10731073
{
1074-
if (ListContains(processIdListBefore, pid))
1075-
{
1076-
continue;
1077-
}
1078-
lpProcessInformation->dwProcessId = pid;
1079-
lpProcessInformation->hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, FALSE, pid);
1080-
break;
1074+
continue;
10811075
}
1082-
if (lpProcessInformation->dwProcessId)
1083-
break;
1084-
Sleep(500);
1076+
lpProcessInformation->dwProcessId = pid;
1077+
lpProcessInformation->hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, FALSE, pid);
1078+
break;
10851079
}
1080+
if (lpProcessInformation->dwProcessId)
1081+
break;
1082+
Sleep(500);
1083+
}
10861084

1087-
if (lpProcessInformation->dwProcessId == 0)
1088-
{
1089-
// Unable to get pid
1090-
dwOutError = ERROR_INVALID_FUNCTION;
1091-
strOutErrorContext = "FindPID";
1092-
bResult = false;
1093-
}
1094-
else if (lpProcessInformation->hProcess == nullptr)
1095-
{
1096-
// Unable to OpenProcess
1097-
dwOutError = ERROR_ELEVATION_REQUIRED;
1098-
strOutErrorContext = "OpenProcess";
1099-
bResult = false;
1100-
}
1085+
if (lpProcessInformation->dwProcessId == 0)
1086+
{
1087+
// Unable to get pid
1088+
dwOutError = ERROR_INVALID_FUNCTION;
1089+
strOutErrorContext = "FindPID";
1090+
wasProcessCreated = false;
1091+
}
1092+
else if (lpProcessInformation->hProcess == nullptr)
1093+
{
1094+
// Unable to OpenProcess
1095+
dwOutError = ERROR_ELEVATION_REQUIRED;
1096+
strOutErrorContext = "OpenProcess";
1097+
wasProcessCreated = false;
11011098
}
11021099

1103-
return bResult;
1100+
return wasProcessCreated;
11041101
}
11051102

11061103
//////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)