Skip to content

Commit

Permalink
Refactored process related functions in the loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ccw808 committed Dec 31, 2015
1 parent 1770d87 commit 20cecc0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 144 deletions.
41 changes: 6 additions & 35 deletions MTA10/loader/Install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,20 @@
#include "../../vendor/unrar/dll.hpp"


// Will not terminate a 64 bit process, or the current process
bool TerminateProcessFromPathFilename ( const SString& strPathFilename, bool bTestOnly = false )
{
DWORD dwProcessIDs[250];
DWORD pBytesReturned = 0;
if ( EnumProcesses ( dwProcessIDs, 250 * sizeof(DWORD), &pBytesReturned ) )
for ( auto processId : MyEnumProcesses() )
{
DWORD id1 = GetCurrentProcessId();
for ( unsigned int i = 0; i < pBytesReturned / sizeof ( DWORD ); i++ )
if ( GetProcessPathFilename( processId ).EqualsI( strPathFilename ) )
{
DWORD id2 = dwProcessIDs[i];
if ( id2 == id1 )
continue;
// Skip 64 bit processes to avoid errors
if ( !Is32bitProcess ( dwProcessIDs[i] ) )
continue;
// Open the process
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessIDs[i]);
if ( hProcess )
if ( !bTestOnly )
{
HMODULE pModule;
DWORD cbNeeded;
if ( EnumProcessModules ( hProcess, &pModule, sizeof ( HMODULE ), &cbNeeded ) )
{
WCHAR szModuleName[MAX_PATH*2] = L"";
if ( GetModuleFileNameExW( hProcess, pModule, szModuleName, NUMELMS(szModuleName) ) )
{
SString strModuleName = ToUTF8( szModuleName );
if ( stricmp ( strModuleName, strPathFilename ) == 0 )
{
if ( !bTestOnly )
TerminateProcess ( hProcess, 0 );
CloseHandle ( hProcess );
return true;
}
}
}

// Close the process
CloseHandle ( hProcess );
TerminateProcess( processId );
}
return true;
}
}

return false;
}

Expand Down
15 changes: 4 additions & 11 deletions MTA10/loader/MainFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,18 +665,11 @@ void CheckAntiVirusStatus( void )

if ( bEnableScaremongering )
{
std::vector < DWORD > processIdList = MyEnumProcesses();
for ( uint i = 0; i < processIdList.size (); i++ )
for ( auto processId : MyEnumProcesses( true ) )
{
DWORD processId = processIdList[i];
// Skip 64 bit processes to avoid errors
if ( !Is32bitProcess ( processId ) )
continue;

std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processId );
for ( uint i = 0; i < filenameList.size (); i++ )
SString strProcessPathFileName = GetProcessPathFilename ( processId );
if ( !strProcessPathFileName.empty() )
{
const SString& strProcessPathFileName = filenameList[i];
SLibVersionInfo libVersionInfo;
if ( GetLibVersionInfo ( strProcessPathFileName, &libVersionInfo ) )
{
Expand All @@ -693,7 +686,7 @@ void CheckAntiVirusStatus( void )
}
}
if ( bEnableScaremongering )
WriteDebugEvent( SString( "AV Searched %d processes, but could not find av helper", processIdList.size() ) );
WriteDebugEvent( "AV Searched %d processes, but could not find av helper" );
}
}

Expand Down
197 changes: 101 additions & 96 deletions MTA10/loader/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,15 @@ typedef WINBASEAPI BOOL (WINAPI *LPFN_QueryFullProcessImageNameW)(__in HANDLE hP

///////////////////////////////////////////////////////////////////////////
//
// GetPossibleProcessPathFilenames
// GetProcessPathFilename
//
//
// Get all image names for a processID
//
///////////////////////////////////////////////////////////////////////////
std::vector < SString > GetPossibleProcessPathFilenames ( DWORD processID )
SString GetProcessPathFilename ( DWORD processID )
{
static LPFN_QueryFullProcessImageNameW fnQueryFullProcessImageNameW = NULL;
static bool bDoneGetProcAddress = false;

std::vector < SString > result;

if ( !bDoneGetProcAddress )
{
// Find 'QueryFullProcessImageNameA'
Expand All @@ -377,50 +374,64 @@ std::vector < SString > GetPossibleProcessPathFilenames ( DWORD processID )
for ( int i = 0 ; i < 2 ; i++ )
{
HANDLE hProcess = OpenProcess ( i == 0 ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID );

if ( hProcess )
{
WCHAR szProcessName[MAX_PATH] = L"";
DWORD dwSize = NUMELMS(szProcessName);
DWORD bOk = fnQueryFullProcessImageNameW ( hProcess, 0, szProcessName, &dwSize );
CloseHandle( hProcess );

if ( bOk && wcslen ( szProcessName ) > 0 )
ListAddUnique ( result, ToUTF8 ( szProcessName ) );
if ( bOk )
{
wchar_t szBuffer[MAX_PATH * 2] = L"";
if ( GetLongPathNameW( szProcessName, szBuffer, NUMELMS(szBuffer) - 1 ) )
{
return ToUTF8( szBuffer );
}
}
}
}
}

{
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );

if ( hProcess )
{
WCHAR szProcessName[MAX_PATH] = L"";
DWORD bOk = GetModuleFileNameExW ( hProcess, NULL, szProcessName, NUMELMS(szProcessName) );
CloseHandle ( hProcess );

if ( bOk && wcslen ( szProcessName ) > 0 )
ListAddUnique ( result, ToUTF8 ( szProcessName ) );
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );
if ( hProcess )
{
WCHAR szProcessName[MAX_PATH] = L"";
DWORD bOk = GetModuleFileNameExW( hProcess, NULL, szProcessName, NUMELMS(szProcessName) );
CloseHandle( hProcess );
if ( bOk )
{
wchar_t szBuffer[MAX_PATH * 2] = L"";
if ( GetLongPathNameW( szProcessName, szBuffer, NUMELMS(szBuffer) - 1 ) )
{
return ToUTF8( szBuffer );
}
}
}
}
}

for ( int i = 0 ; i < 2 ; i++ )
{
HANDLE hProcess = OpenProcess ( i == 0 ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID );

if ( hProcess )
for ( int i = 0 ; i < 2 ; i++ )
{
WCHAR szProcessName[MAX_PATH] = L"";
DWORD bOk = GetProcessImageFileNameW ( hProcess, szProcessName, NUMELMS(szProcessName) );
CloseHandle( hProcess );

if ( bOk && wcslen ( szProcessName ) > 0 )
ListAddUnique ( result, ToUTF8 ( devicePathToWin32Path ( szProcessName ) ) );
HANDLE hProcess = OpenProcess( i == 0 ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID );
if ( hProcess )
{
WCHAR szProcessName[MAX_PATH] = L"";
DWORD bOk = GetProcessImageFileNameW( hProcess, szProcessName, NUMELMS(szProcessName) );
CloseHandle( hProcess );
if ( bOk )
{
wchar_t szBuffer[MAX_PATH * 2] = L"";
if ( GetLongPathNameW( devicePathToWin32Path( szProcessName ), szBuffer, NUMELMS(szBuffer) - 1 ) )
{
return ToUTF8( szBuffer );
}
}
}
}
}

return result;
return "";
}


Expand All @@ -431,7 +442,7 @@ std::vector < SString > GetPossibleProcessPathFilenames ( DWORD processID )
//
//
///////////////////////////////////////////////////////////////////////////
std::vector < DWORD > MyEnumProcesses ( void )
std::vector < DWORD > MyEnumProcesses ( bool bInclude64bit, bool bIncludeCurrent )
{
uint uiSize = 200;
std::vector < DWORD > processIdList;
Expand All @@ -455,7 +466,18 @@ std::vector < DWORD > MyEnumProcesses ( void )
uiSize *= 2;
}

return processIdList;
// Filter list
std::vector < DWORD > filteredList;
for( auto processId : processIdList )
{
if ( !bInclude64bit && !Is32bitProcess ( processId ) )
continue;
if ( !bIncludeCurrent && processId == GetCurrentProcessId() )
continue;
filteredList.push_back( processId );
}

return filteredList;
}


Expand Down Expand Up @@ -502,18 +524,11 @@ std::vector < DWORD > GetGTAProcessList ( void )
{
std::vector < DWORD > result;

std::vector < DWORD > processIdList = MyEnumProcesses ();
for ( uint i = 0; i < processIdList.size (); i++ )
for ( auto processId : MyEnumProcesses() )
{
DWORD processId = processIdList[i];
// Skip 64 bit processes to avoid errors
if ( !Is32bitProcess ( processId ) )
continue;

std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processId );
for ( uint i = 0; i < filenameList.size (); i++ )
if ( filenameList[i].EndsWith ( MTA_GTAEXE_NAME ) || filenameList[i].EndsWith ( MTA_HTAEXE_NAME ) )
ListAddUnique ( result, processId );
SString strPathFilename = GetProcessPathFilename ( processId );
if ( strPathFilename.EndsWith ( MTA_GTAEXE_NAME ) || strPathFilename.EndsWith ( MTA_HTAEXE_NAME ) )
ListAddUnique ( result, processId );
}

if ( DWORD processId = FindProcessId ( MTA_GTAEXE_NAME ) )
Expand Down Expand Up @@ -550,23 +565,15 @@ void TerminateGTAIfRunning ( void )
{
std::vector < DWORD > processIdList = GetGTAProcessList ();

if ( processIdList.size () )
// Try to stop all GTA process id's
for ( uint i = 0 ; i < 3 && processIdList.size () ; i++ )
{
// Try to stop all GTA process id's
for ( uint i = 0 ; i < 3 && processIdList.size () ; i++ )
for ( auto processId : processIdList )
{
for ( std::vector < DWORD > ::iterator iter = processIdList.begin () ; iter != processIdList.end (); ++iter )
{
HANDLE hProcess = OpenProcess ( PROCESS_TERMINATE, 0, *iter );
if ( hProcess )
{
TerminateProcess ( hProcess, 0 );
CloseHandle ( hProcess );
}
}
Sleep ( 1000 );
processIdList = GetGTAProcessList ();
TerminateProcess( processId );
}
Sleep ( 1000 );
processIdList = GetGTAProcessList ();
}
}

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

std::vector < DWORD > processIdList = MyEnumProcesses ();
for ( uint i = 0; i < processIdList.size (); i++ )
for ( auto processId : MyEnumProcesses() )
{
DWORD processId = processIdList[i];
// Skip 64 bit processes to avoid errors
if ( !Is32bitProcess ( processId ) )
continue;

std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processId );
for ( uint i = 0; i < filenameList.size (); i++ )
if ( filenameList[i].EndsWith ( MTA_EXE_NAME ) )
ListAddUnique ( result, processId );
SString strPathFilename = GetProcessPathFilename ( processId );
if ( strPathFilename.EndsWith ( MTA_EXE_NAME ) )
ListAddUnique ( result, processId );
}

if ( DWORD processId = FindProcessId ( MTA_EXE_NAME ) )
Expand Down Expand Up @@ -635,14 +635,9 @@ void TerminateOtherMTAIfRunning ( void )
// Try to stop all other MTA process id's
for ( uint i = 0 ; i < 3 && processIdList.size () ; i++ )
{
for ( std::vector < DWORD > ::iterator iter = processIdList.begin () ; iter != processIdList.end (); ++iter )
for ( auto processId : processIdList )
{
HANDLE hProcess = OpenProcess ( PROCESS_TERMINATE, 0, *iter );
if ( hProcess )
{
TerminateProcess ( hProcess, 0 );
CloseHandle ( hProcess );
}
TerminateProcess( processId );
}
Sleep ( 1000 );
processIdList = GetOtherMTAProcessList ();
Expand Down Expand Up @@ -735,17 +730,13 @@ SString GetMTASAPath ( void )
///////////////////////////////////////////////////////////////
bool LookForGtaProcess ( SString& strOutPathFilename )
{
std::vector < DWORD > processIdList = GetGTAProcessList ();
for ( uint i = 0 ; i < processIdList.size () ; i++ )
for ( auto processId : GetGTAProcessList() )
{
std::vector < SString > filenameList = GetPossibleProcessPathFilenames ( processIdList[i] );
for ( uint i = 0 ; i < filenameList.size () ; i++ )
SString strPathFilename = GetProcessPathFilename ( processId );
if ( FileExists ( strPathFilename ) )
{
if ( FileExists ( filenameList[i] ) )
{
strOutPathFilename = filenameList[i];
return true;
}
strOutPathFilename = strPathFilename;
return true;
}
}
return false;
Expand Down Expand Up @@ -1400,6 +1391,24 @@ bool Is32bitProcess ( DWORD processID )
}


///////////////////////////////////////////////////////////////////////////
//
// TerminateProcess
//
// Terminate process from pid
//
///////////////////////////////////////////////////////////////////////////
void TerminateProcess( DWORD dwProcessID, uint uiExitCode )
{
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, 0, dwProcessID );
if ( hProcess )
{
TerminateProcess( hProcess, uiExitCode );
CloseHandle( hProcess );
}
}


///////////////////////////////////////////////////////////////////////////
//
// CreateSingleInstanceMutex
Expand Down Expand Up @@ -1977,18 +1986,14 @@ void ForbodenProgramsMessage ( void )
forbodenList.push_back( "CheatEngine" );

SString strResult;
std::vector < DWORD > processIdList = MyEnumProcesses ();
for ( uint i = 0; i < processIdList.size (); i++ )
for ( auto processId : MyEnumProcesses( true ) )
{
std::vector < SString > pathFilenameList = GetPossibleProcessPathFilenames ( processIdList[i] );
for ( uint p = 0; p < pathFilenameList.size (); p++ )
SString strPathFilename = GetProcessPathFilename ( processId );
SString strFilename = ExtractFilename( strPathFilename );
for ( auto forbodenName : forbodenList )
{
SString strFilename = ExtractFilename( pathFilenameList[p] );
for ( uint f = 0; f < forbodenList.size (); f++ )
{
if ( strFilename.Replace( " ", "" ).BeginsWithI( forbodenList[f] ) )
strResult += strFilename + "\n";
}
if ( strFilename.Replace( " ", "" ).BeginsWithI( forbodenName ) )
strResult += strFilename + "\n";
}
}

Expand Down

0 comments on commit 20cecc0

Please sign in to comment.