Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DWORD_PTR GetModuleBaseEx( const char* lpModuleName, DWORD_PTR dwProcessId )
- {
- MODULEENTRY32 me32;
- HANDLE hSnapshot = INVALID_HANDLE_VALUE;
- if( !lpModuleName || !dwProcessId )
- return NULL;
- hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
- if( hSnapshot == INVALID_HANDLE_VALUE )
- return NULL;
- if( !Module32First( hSnapshot, &me32 ) )
- {
- CloseHandle( hSnapshot );
- return NULL;
- }
- do
- {
- if( !strcmp( lpModuleName, me32.szModule ) )
- {
- CloseHandle( hSnapshot );
- return ( DWORD_PTR )me32.modBaseAddr;
- }
- }
- while( Module32Next( hSnapshot, &me32 ) );
- CloseHandle( hSnapshot );
- return NULL;
- }
- DWORD_PTR GetModuleBaseEx( const char* lpModuleName, HANDLE hProcess )
- {
- MEMORY_BASIC_INFORMATION Mbi;
- DWORD_PTR dwIndex = NULL;
- for( dwIndex = NULL; dwIndex < 0x7FFFFFF; dwIndex += Mbi.RegionSize )
- {
- if( !VirtualQueryEx( hProcess, ( LPVOID )( dwIndex ), &Mbi, sizeof( MEMORY_BASIC_INFORMATION ) ) )
- continue;
- char szOut[ 256 ];
- if( !GetMappedFileName( hProcess, Mbi.BaseAddress, szOut, 256 ) )
- continue;
- std::string String = szOut;
- int iFound = String.find_last_of( "\\" );
- if( !iFound )
- continue;
- String = String.substr( iFound + 1, String.length( ) );
- if( !strcmp( lpModuleName, String.c_str( ) ) )
- return ( DWORD_PTR )Mbi.BaseAddress;
- }
- return NULL;
- }
- DWORD_PTR GetModuleBaseEx( const char* lpModuleName, DWORD_PTR dwProcessId )
- {
- HMODULE hModule[ 1024 ];
- HANDLE hProcess = INVALID_HANDLE_VALUE;
- DWORD_PTR dwNeeded = NULL;
- DWORD_PTR dwIndex = NULL;
- hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE< dwProcessId );
- if( hProcess == INVALID_HANDLE_VALUE )
- return NULL;
- if( EnumProcessModules( hProcess, hModule, sizeof( hModule ), &dwNeeded ) )
- {
- for( dwIndex = NULL; dwIndex < ( dwNeeded / sizeof( HMODULE ) ); dwIndex++ )
- {
- char lpCurrentModule[ MAX_PATH ] = "<unknown>";
- if( GetModuleFileNameEx( hProcess, hModule[ dwIndex ], lpCurrentModule, sizeof( lpCurrentModule ) ) )
- {
- if( !strcmp( lpCurrentModule, lpModuleName ) )
- return ( DWORD_PTR )( hModule[ dwIndex ] );
- }
- }
- }
- return NULL;
- }
- DWORD_PTR GetProcessIdEx( const char* lpProcessName )
- {
- PROCESSENTRY32 pe32;
- HANDLE hSnapshot = INVALID_HANDLE_VALUE;
- if( !lpProcessName )
- return NULL;
- hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL );
- if( hSnapshot == INVALID_HANDLE_VALUE )
- return NULL;
- if( !Process32First( hSnapshot, &pe32 ) )
- {
- CloseHandle( hSnapshot );
- return NULL;
- }
- do
- {
- if( !strcmp( lpProcessName, pe32.szExeFile ) )
- {
- CloseHandle( hSnapshot );
- return pe32.th32ProcessID;
- }
- }
- while( Process32Next( hSnapshot, &pe32 ) );
- CloseHandle( hSnapshot );
- return NULL;
- }
- DWORD_PTR GetProcessIdEx( const char* lpProcessName )
- {
- DWORD_PTR dwProcessArray[ 1024 ];
- DWORD_PTR dwNeeded = NULL;
- DWORD_PTR dwProcessCount = NULL;
- DWORD_PTR dwIndex = NULL;
- if( !lpProcessName )
- return NULL;
- if( !EnumProcesses( dwProcessArray, sizeof( dwProcessArray ), &dwNeeded ) )
- return NULL;
- dwProcessCount = dwNeeded / sizeof( DWORD_PTR );
- for( dwIndex = NULL; dwIndex < dwProcessCount; dwIndex++ )
- {
- if( dwProcessArray[ dwIndex ] )
- {
- char lpCurrentProcess[ MAX_PATH ] = "<unknown>";
- HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessArray[ dwIndex ] );
- if( hProcess != INVALID_HANDLE_VALUE )
- {
- HMODULE hModule;
- DWORD_PTR dwNeeded;
- if( EnumProcessModules( hProcess, &hModule, sizeof( hModule ), &dwNeeded ) )
- {
- GetModuleBaseName( hProcess, hModule, lpCurrentProcess, sizeof( lpCurrentProcess ) / sizeof( char ) );
- }
- }
- if( !strcmp( lpProcessName, lpCurrentProcess ) )
- return dwProcessArray[ dwIndex ];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment