Skip to content

Commit 20cecc0

Browse files
committed
Refactored process related functions in the loader
1 parent 1770d87 commit 20cecc0

File tree

4 files changed

+114
-144
lines changed

4 files changed

+114
-144
lines changed

MTA10/loader/Install.cpp

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,20 @@
1414
#include "../../vendor/unrar/dll.hpp"
1515

1616

17+
// Will not terminate a 64 bit process, or the current process
1718
bool TerminateProcessFromPathFilename ( const SString& strPathFilename, bool bTestOnly = false )
1819
{
19-
DWORD dwProcessIDs[250];
20-
DWORD pBytesReturned = 0;
21-
if ( EnumProcesses ( dwProcessIDs, 250 * sizeof(DWORD), &pBytesReturned ) )
20+
for ( auto processId : MyEnumProcesses() )
2221
{
23-
DWORD id1 = GetCurrentProcessId();
24-
for ( unsigned int i = 0; i < pBytesReturned / sizeof ( DWORD ); i++ )
22+
if ( GetProcessPathFilename( processId ).EqualsI( strPathFilename ) )
2523
{
26-
DWORD id2 = dwProcessIDs[i];
27-
if ( id2 == id1 )
28-
continue;
29-
// Skip 64 bit processes to avoid errors
30-
if ( !Is32bitProcess ( dwProcessIDs[i] ) )
31-
continue;
32-
// Open the process
33-
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessIDs[i]);
34-
if ( hProcess )
24+
if ( !bTestOnly )
3525
{
36-
HMODULE pModule;
37-
DWORD cbNeeded;
38-
if ( EnumProcessModules ( hProcess, &pModule, sizeof ( HMODULE ), &cbNeeded ) )
39-
{
40-
WCHAR szModuleName[MAX_PATH*2] = L"";
41-
if ( GetModuleFileNameExW( hProcess, pModule, szModuleName, NUMELMS(szModuleName) ) )
42-
{
43-
SString strModuleName = ToUTF8( szModuleName );
44-
if ( stricmp ( strModuleName, strPathFilename ) == 0 )
45-
{
46-
if ( !bTestOnly )
47-
TerminateProcess ( hProcess, 0 );
48-
CloseHandle ( hProcess );
49-
return true;
50-
}
51-
}
52-
}
53-
54-
// Close the process
55-
CloseHandle ( hProcess );
26+
TerminateProcess( processId );
5627
}
28+
return true;
5729
}
5830
}
59-
6031
return false;
6132
}
6233

MTA10/loader/MainFunctions.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -665,18 +665,11 @@ void CheckAntiVirusStatus( void )
665665

666666
if ( bEnableScaremongering )
667667
{
668-
std::vector < DWORD > processIdList = MyEnumProcesses();
669-
for ( uint i = 0; i < processIdList.size (); i++ )
668+
for ( auto processId : MyEnumProcesses( true ) )
670669
{
671-
DWORD processId = processIdList[i];
672-
// Skip 64 bit processes to avoid errors
673-
if ( !Is32bitProcess ( processId ) )
674-
continue;
675-
676-
std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processId );
677-
for ( uint i = 0; i < filenameList.size (); i++ )
670+
SString strProcessPathFileName = GetProcessPathFilename ( processId );
671+
if ( !strProcessPathFileName.empty() )
678672
{
679-
const SString& strProcessPathFileName = filenameList[i];
680673
SLibVersionInfo libVersionInfo;
681674
if ( GetLibVersionInfo ( strProcessPathFileName, &libVersionInfo ) )
682675
{
@@ -693,7 +686,7 @@ void CheckAntiVirusStatus( void )
693686
}
694687
}
695688
if ( bEnableScaremongering )
696-
WriteDebugEvent( SString( "AV Searched %d processes, but could not find av helper", processIdList.size() ) );
689+
WriteDebugEvent( "AV Searched %d processes, but could not find av helper" );
697690
}
698691
}
699692

MTA10/loader/Utils.cpp

Lines changed: 101 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,15 @@ typedef WINBASEAPI BOOL (WINAPI *LPFN_QueryFullProcessImageNameW)(__in HANDLE hP
352352

353353
///////////////////////////////////////////////////////////////////////////
354354
//
355-
// GetPossibleProcessPathFilenames
355+
// GetProcessPathFilename
356+
//
356357
//
357-
// Get all image names for a processID
358358
//
359359
///////////////////////////////////////////////////////////////////////////
360-
std::vector < SString > GetPossibleProcessPathFilenames ( DWORD processID )
360+
SString GetProcessPathFilename ( DWORD processID )
361361
{
362362
static LPFN_QueryFullProcessImageNameW fnQueryFullProcessImageNameW = NULL;
363363
static bool bDoneGetProcAddress = false;
364-
365-
std::vector < SString > result;
366-
367364
if ( !bDoneGetProcAddress )
368365
{
369366
// Find 'QueryFullProcessImageNameA'
@@ -377,50 +374,64 @@ std::vector < SString > GetPossibleProcessPathFilenames ( DWORD processID )
377374
for ( int i = 0 ; i < 2 ; i++ )
378375
{
379376
HANDLE hProcess = OpenProcess ( i == 0 ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID );
380-
381377
if ( hProcess )
382378
{
383379
WCHAR szProcessName[MAX_PATH] = L"";
384380
DWORD dwSize = NUMELMS(szProcessName);
385381
DWORD bOk = fnQueryFullProcessImageNameW ( hProcess, 0, szProcessName, &dwSize );
386382
CloseHandle( hProcess );
387-
388-
if ( bOk && wcslen ( szProcessName ) > 0 )
389-
ListAddUnique ( result, ToUTF8 ( szProcessName ) );
383+
if ( bOk )
384+
{
385+
wchar_t szBuffer[MAX_PATH * 2] = L"";
386+
if ( GetLongPathNameW( szProcessName, szBuffer, NUMELMS(szBuffer) - 1 ) )
387+
{
388+
return ToUTF8( szBuffer );
389+
}
390+
}
390391
}
391392
}
392393
}
393394

394395
{
395-
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );
396-
397-
if ( hProcess )
398396
{
399-
WCHAR szProcessName[MAX_PATH] = L"";
400-
DWORD bOk = GetModuleFileNameExW ( hProcess, NULL, szProcessName, NUMELMS(szProcessName) );
401-
CloseHandle ( hProcess );
402-
403-
if ( bOk && wcslen ( szProcessName ) > 0 )
404-
ListAddUnique ( result, ToUTF8 ( szProcessName ) );
397+
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );
398+
if ( hProcess )
399+
{
400+
WCHAR szProcessName[MAX_PATH] = L"";
401+
DWORD bOk = GetModuleFileNameExW( hProcess, NULL, szProcessName, NUMELMS(szProcessName) );
402+
CloseHandle( hProcess );
403+
if ( bOk )
404+
{
405+
wchar_t szBuffer[MAX_PATH * 2] = L"";
406+
if ( GetLongPathNameW( szProcessName, szBuffer, NUMELMS(szBuffer) - 1 ) )
407+
{
408+
return ToUTF8( szBuffer );
409+
}
410+
}
411+
}
405412
}
406-
}
407413

408-
for ( int i = 0 ; i < 2 ; i++ )
409-
{
410-
HANDLE hProcess = OpenProcess ( i == 0 ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID );
411-
412-
if ( hProcess )
414+
for ( int i = 0 ; i < 2 ; i++ )
413415
{
414-
WCHAR szProcessName[MAX_PATH] = L"";
415-
DWORD bOk = GetProcessImageFileNameW ( hProcess, szProcessName, NUMELMS(szProcessName) );
416-
CloseHandle( hProcess );
417-
418-
if ( bOk && wcslen ( szProcessName ) > 0 )
419-
ListAddUnique ( result, ToUTF8 ( devicePathToWin32Path ( szProcessName ) ) );
416+
HANDLE hProcess = OpenProcess( i == 0 ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID );
417+
if ( hProcess )
418+
{
419+
WCHAR szProcessName[MAX_PATH] = L"";
420+
DWORD bOk = GetProcessImageFileNameW( hProcess, szProcessName, NUMELMS(szProcessName) );
421+
CloseHandle( hProcess );
422+
if ( bOk )
423+
{
424+
wchar_t szBuffer[MAX_PATH * 2] = L"";
425+
if ( GetLongPathNameW( devicePathToWin32Path( szProcessName ), szBuffer, NUMELMS(szBuffer) - 1 ) )
426+
{
427+
return ToUTF8( szBuffer );
428+
}
429+
}
430+
}
420431
}
421432
}
422433

423-
return result;
434+
return "";
424435
}
425436

426437

@@ -431,7 +442,7 @@ std::vector < SString > GetPossibleProcessPathFilenames ( DWORD processID )
431442
//
432443
//
433444
///////////////////////////////////////////////////////////////////////////
434-
std::vector < DWORD > MyEnumProcesses ( void )
445+
std::vector < DWORD > MyEnumProcesses ( bool bInclude64bit, bool bIncludeCurrent )
435446
{
436447
uint uiSize = 200;
437448
std::vector < DWORD > processIdList;
@@ -455,7 +466,18 @@ std::vector < DWORD > MyEnumProcesses ( void )
455466
uiSize *= 2;
456467
}
457468

458-
return processIdList;
469+
// Filter list
470+
std::vector < DWORD > filteredList;
471+
for( auto processId : processIdList )
472+
{
473+
if ( !bInclude64bit && !Is32bitProcess ( processId ) )
474+
continue;
475+
if ( !bIncludeCurrent && processId == GetCurrentProcessId() )
476+
continue;
477+
filteredList.push_back( processId );
478+
}
479+
480+
return filteredList;
459481
}
460482

461483

@@ -502,18 +524,11 @@ std::vector < DWORD > GetGTAProcessList ( void )
502524
{
503525
std::vector < DWORD > result;
504526

505-
std::vector < DWORD > processIdList = MyEnumProcesses ();
506-
for ( uint i = 0; i < processIdList.size (); i++ )
527+
for ( auto processId : MyEnumProcesses() )
507528
{
508-
DWORD processId = processIdList[i];
509-
// Skip 64 bit processes to avoid errors
510-
if ( !Is32bitProcess ( processId ) )
511-
continue;
512-
513-
std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processId );
514-
for ( uint i = 0; i < filenameList.size (); i++ )
515-
if ( filenameList[i].EndsWith ( MTA_GTAEXE_NAME ) || filenameList[i].EndsWith ( MTA_HTAEXE_NAME ) )
516-
ListAddUnique ( result, processId );
529+
SString strPathFilename = GetProcessPathFilename ( processId );
530+
if ( strPathFilename.EndsWith ( MTA_GTAEXE_NAME ) || strPathFilename.EndsWith ( MTA_HTAEXE_NAME ) )
531+
ListAddUnique ( result, processId );
517532
}
518533

519534
if ( DWORD processId = FindProcessId ( MTA_GTAEXE_NAME ) )
@@ -550,23 +565,15 @@ void TerminateGTAIfRunning ( void )
550565
{
551566
std::vector < DWORD > processIdList = GetGTAProcessList ();
552567

553-
if ( processIdList.size () )
568+
// Try to stop all GTA process id's
569+
for ( uint i = 0 ; i < 3 && processIdList.size () ; i++ )
554570
{
555-
// Try to stop all GTA process id's
556-
for ( uint i = 0 ; i < 3 && processIdList.size () ; i++ )
571+
for ( auto processId : processIdList )
557572
{
558-
for ( std::vector < DWORD > ::iterator iter = processIdList.begin () ; iter != processIdList.end (); ++iter )
559-
{
560-
HANDLE hProcess = OpenProcess ( PROCESS_TERMINATE, 0, *iter );
561-
if ( hProcess )
562-
{
563-
TerminateProcess ( hProcess, 0 );
564-
CloseHandle ( hProcess );
565-
}
566-
}
567-
Sleep ( 1000 );
568-
processIdList = GetGTAProcessList ();
573+
TerminateProcess( processId );
569574
}
575+
Sleep ( 1000 );
576+
processIdList = GetGTAProcessList ();
570577
}
571578
}
572579

@@ -582,18 +589,11 @@ std::vector < DWORD > GetOtherMTAProcessList ( void )
582589
{
583590
std::vector < DWORD > result;
584591

585-
std::vector < DWORD > processIdList = MyEnumProcesses ();
586-
for ( uint i = 0; i < processIdList.size (); i++ )
592+
for ( auto processId : MyEnumProcesses() )
587593
{
588-
DWORD processId = processIdList[i];
589-
// Skip 64 bit processes to avoid errors
590-
if ( !Is32bitProcess ( processId ) )
591-
continue;
592-
593-
std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processId );
594-
for ( uint i = 0; i < filenameList.size (); i++ )
595-
if ( filenameList[i].EndsWith ( MTA_EXE_NAME ) )
596-
ListAddUnique ( result, processId );
594+
SString strPathFilename = GetProcessPathFilename ( processId );
595+
if ( strPathFilename.EndsWith ( MTA_EXE_NAME ) )
596+
ListAddUnique ( result, processId );
597597
}
598598

599599
if ( DWORD processId = FindProcessId ( MTA_EXE_NAME ) )
@@ -635,14 +635,9 @@ void TerminateOtherMTAIfRunning ( void )
635635
// Try to stop all other MTA process id's
636636
for ( uint i = 0 ; i < 3 && processIdList.size () ; i++ )
637637
{
638-
for ( std::vector < DWORD > ::iterator iter = processIdList.begin () ; iter != processIdList.end (); ++iter )
638+
for ( auto processId : processIdList )
639639
{
640-
HANDLE hProcess = OpenProcess ( PROCESS_TERMINATE, 0, *iter );
641-
if ( hProcess )
642-
{
643-
TerminateProcess ( hProcess, 0 );
644-
CloseHandle ( hProcess );
645-
}
640+
TerminateProcess( processId );
646641
}
647642
Sleep ( 1000 );
648643
processIdList = GetOtherMTAProcessList ();
@@ -735,17 +730,13 @@ SString GetMTASAPath ( void )
735730
///////////////////////////////////////////////////////////////
736731
bool LookForGtaProcess ( SString& strOutPathFilename )
737732
{
738-
std::vector < DWORD > processIdList = GetGTAProcessList ();
739-
for ( uint i = 0 ; i < processIdList.size () ; i++ )
733+
for ( auto processId : GetGTAProcessList() )
740734
{
741-
std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processIdList[i] );
742-
for ( uint i = 0 ; i < filenameList.size () ; i++ )
735+
SString strPathFilename = GetProcessPathFilename ( processId );
736+
if ( FileExists ( strPathFilename ) )
743737
{
744-
if ( FileExists ( filenameList[i] ) )
745-
{
746-
strOutPathFilename = filenameList[i];
747-
return true;
748-
}
738+
strOutPathFilename = strPathFilename;
739+
return true;
749740
}
750741
}
751742
return false;
@@ -1400,6 +1391,24 @@ bool Is32bitProcess ( DWORD processID )
14001391
}
14011392

14021393

1394+
///////////////////////////////////////////////////////////////////////////
1395+
//
1396+
// TerminateProcess
1397+
//
1398+
// Terminate process from pid
1399+
//
1400+
///////////////////////////////////////////////////////////////////////////
1401+
void TerminateProcess( DWORD dwProcessID, uint uiExitCode )
1402+
{
1403+
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, 0, dwProcessID );
1404+
if ( hProcess )
1405+
{
1406+
TerminateProcess( hProcess, uiExitCode );
1407+
CloseHandle( hProcess );
1408+
}
1409+
}
1410+
1411+
14031412
///////////////////////////////////////////////////////////////////////////
14041413
//
14051414
// CreateSingleInstanceMutex
@@ -1977,18 +1986,14 @@ void ForbodenProgramsMessage ( void )
19771986
forbodenList.push_back( "CheatEngine" );
19781987

19791988
SString strResult;
1980-
std::vector < DWORD > processIdList = MyEnumProcesses ();
1981-
for ( uint i = 0; i < processIdList.size (); i++ )
1989+
for ( auto processId : MyEnumProcesses( true ) )
19821990
{
1983-
std::vector < SString > pathFilenameList = GetPossibleProcessPathFilenames ( processIdList[i] );
1984-
for ( uint p = 0; p < pathFilenameList.size (); p++ )
1991+
SString strPathFilename = GetProcessPathFilename ( processId );
1992+
SString strFilename = ExtractFilename( strPathFilename );
1993+
for ( auto forbodenName : forbodenList )
19851994
{
1986-
SString strFilename = ExtractFilename( pathFilenameList[p] );
1987-
for ( uint f = 0; f < forbodenList.size (); f++ )
1988-
{
1989-
if ( strFilename.Replace( " ", "" ).BeginsWithI( forbodenList[f] ) )
1990-
strResult += strFilename + "\n";
1991-
}
1995+
if ( strFilename.Replace( " ", "" ).BeginsWithI( forbodenName ) )
1996+
strResult += strFilename + "\n";
19921997
}
19931998
}
19941999

0 commit comments

Comments
 (0)