@@ -1223,7 +1223,7 @@ static LONG SafeNtQueryInformationThread(HANDLE ThreadHandle, INT ThreadInformat
1223
1223
HMODULE ntdll = LoadLibraryA (" ntdll.dll" );
1224
1224
1225
1225
if (ntdll)
1226
- lookup.function = ( FunctionPointer) GetProcAddress (ntdll, " NtQueryInformationThread" );
1226
+ lookup.function = reinterpret_cast < FunctionPointer>( GetProcAddress (ntdll, " NtQueryInformationThread" ) );
1227
1227
else
1228
1228
return 0xC0000135L ; // STATUS_DLL_NOT_FOUND
1229
1229
}
@@ -1246,10 +1246,13 @@ DWORD SharedUtil::GetMainThreadId()
1246
1246
if (dwMainThreadID == 0 )
1247
1247
{
1248
1248
// Get the module information for the currently running process
1249
+ DWORD processEntryPointAddress = 0 ;
1249
1250
MODULEINFO moduleInfo = {};
1250
- GetModuleInformation (GetCurrentProcess (), GetModuleHandle (nullptr ), &moduleInfo, sizeof (MODULEINFO));
1251
-
1252
- DWORD processEntryPointAddress = reinterpret_cast <DWORD>(moduleInfo.EntryPoint );
1251
+
1252
+ if (GetModuleInformation (GetCurrentProcess (), GetModuleHandle (nullptr ), &moduleInfo, sizeof (MODULEINFO)) != 0 )
1253
+ {
1254
+ processEntryPointAddress = reinterpret_cast <DWORD>(moduleInfo.EntryPoint );
1255
+ }
1253
1256
1254
1257
// Find oldest thread in the current process ( http://www.codeproject.com/Questions/78801/How-to-get-the-main-thread-ID-of-a-process-known-b )
1255
1258
HANDLE hThreadSnap = CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, 0 );
@@ -1258,26 +1261,31 @@ DWORD SharedUtil::GetMainThreadId()
1258
1261
{
1259
1262
ULONGLONG ullMinCreateTime = ULLONG_MAX;
1260
1263
1264
+ DWORD currentProcessID = GetCurrentProcessId ();
1265
+
1261
1266
THREADENTRY32 th32 = {};
1262
1267
th32.dwSize = sizeof (THREADENTRY32);
1263
1268
1264
1269
for (BOOL bOK = Thread32First (hThreadSnap, &th32); bOK; bOK = Thread32Next (hThreadSnap, &th32))
1265
1270
{
1266
- if (th32.th32OwnerProcessID == GetCurrentProcessId () )
1271
+ if (th32.th32OwnerProcessID == currentProcessID )
1267
1272
{
1268
1273
HANDLE hThread = OpenThread (THREAD_QUERY_INFORMATION, TRUE , th32.th32ThreadID );
1269
1274
1270
1275
if (hThread)
1271
1276
{
1272
1277
// Check the thread by entry point first
1273
- DWORD entryPointAddress = 0 ;
1274
-
1275
- if (QueryThreadEntryPointAddress (hThread, &entryPointAddress) && entryPointAddress == processEntryPointAddress)
1278
+ if (processEntryPointAddress != 0 )
1276
1279
{
1277
- dwMainThreadID = th32.th32ThreadID ;
1278
- CloseHandle (hThread);
1279
- CloseHandle (hThreadSnap);
1280
- return dwMainThreadID;
1280
+ DWORD entryPointAddress = 0 ;
1281
+
1282
+ if (QueryThreadEntryPointAddress (hThread, &entryPointAddress) && entryPointAddress == processEntryPointAddress)
1283
+ {
1284
+ dwMainThreadID = th32.th32ThreadID ;
1285
+ CloseHandle (hThread);
1286
+ CloseHandle (hThreadSnap);
1287
+ return dwMainThreadID;
1288
+ }
1281
1289
}
1282
1290
1283
1291
// If entry point check failed, find the oldest thread in the system
0 commit comments