@@ -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// /////////////////////////////////////////////////////////////
736731bool 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