keybode

stuff

Jan 19th, 2014
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. DWORD_PTR GetModuleBaseEx( const char* lpModuleName, DWORD_PTR dwProcessId )
  2. {
  3.     MODULEENTRY32   me32;
  4.     HANDLE          hSnapshot = INVALID_HANDLE_VALUE;
  5.  
  6.     if( !lpModuleName || !dwProcessId )
  7.         return NULL;
  8.  
  9.     hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
  10.  
  11.     if( hSnapshot == INVALID_HANDLE_VALUE )
  12.         return NULL;
  13.  
  14.     if( !Module32First( hSnapshot, &me32 ) )
  15.     {
  16.         CloseHandle( hSnapshot );
  17.         return NULL;
  18.     }
  19.  
  20.     do
  21.     {
  22.         if( !strcmp( lpModuleName, me32.szModule ) )
  23.         {
  24.             CloseHandle( hSnapshot );
  25.             return ( DWORD_PTR )me32.modBaseAddr;
  26.         }
  27.     }
  28.     while( Module32Next( hSnapshot, &me32 ) );
  29.  
  30.     CloseHandle( hSnapshot );
  31.  
  32.     return NULL;
  33. }
  34.  
  35. DWORD_PTR GetModuleBaseEx( const char* lpModuleName, HANDLE hProcess )
  36. {
  37.     MEMORY_BASIC_INFORMATION    Mbi;
  38.     DWORD_PTR                   dwIndex = NULL;
  39.  
  40.     for( dwIndex = NULL; dwIndex < 0x7FFFFFF; dwIndex += Mbi.RegionSize )
  41.     {
  42.         if( !VirtualQueryEx( hProcess, ( LPVOID )( dwIndex ), &Mbi, sizeof( MEMORY_BASIC_INFORMATION ) ) )
  43.             continue;
  44.  
  45.         char szOut[ 256 ];
  46.  
  47.         if( !GetMappedFileName( hProcess, Mbi.BaseAddress, szOut, 256 ) )
  48.             continue;
  49.  
  50.         std::string String = szOut;
  51.         int iFound = String.find_last_of( "\\" );
  52.  
  53.         if( !iFound )
  54.             continue;
  55.  
  56.         String = String.substr( iFound + 1, String.length( ) );
  57.  
  58.         if( !strcmp( lpModuleName, String.c_str( ) ) )
  59.             return ( DWORD_PTR )Mbi.BaseAddress;
  60.     }
  61.  
  62.     return NULL;
  63. }
  64.  
  65. DWORD_PTR GetModuleBaseEx( const char* lpModuleName, DWORD_PTR dwProcessId )
  66. {
  67.     HMODULE     hModule[ 1024 ];
  68.     HANDLE      hProcess    = INVALID_HANDLE_VALUE;
  69.     DWORD_PTR   dwNeeded    = NULL;
  70.     DWORD_PTR   dwIndex     = NULL;
  71.  
  72.     hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE< dwProcessId );
  73.  
  74.     if( hProcess == INVALID_HANDLE_VALUE )
  75.         return NULL;
  76.  
  77.     if( EnumProcessModules( hProcess, hModule, sizeof( hModule ), &dwNeeded ) )
  78.     {
  79.         for( dwIndex = NULL; dwIndex < ( dwNeeded / sizeof( HMODULE ) ); dwIndex++ )
  80.         {
  81.             char lpCurrentModule[ MAX_PATH ] = "<unknown>";
  82.  
  83.             if( GetModuleFileNameEx( hProcess, hModule[ dwIndex ], lpCurrentModule, sizeof( lpCurrentModule ) ) )
  84.             {
  85.                 if( !strcmp( lpCurrentModule, lpModuleName ) )
  86.                     return ( DWORD_PTR )( hModule[ dwIndex ] );
  87.             }
  88.         }
  89.     }
  90.  
  91.     return NULL;
  92. }
  93.  
  94. DWORD_PTR GetProcessIdEx( const char* lpProcessName )
  95. {
  96.     PROCESSENTRY32  pe32;
  97.     HANDLE          hSnapshot = INVALID_HANDLE_VALUE;
  98.  
  99.     if( !lpProcessName )
  100.         return NULL;
  101.  
  102.     hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL );
  103.  
  104.     if( hSnapshot == INVALID_HANDLE_VALUE )
  105.         return NULL;
  106.  
  107.     if( !Process32First( hSnapshot, &pe32 ) )
  108.     {
  109.         CloseHandle( hSnapshot );
  110.         return NULL;
  111.     }
  112.  
  113.     do
  114.     {
  115.         if( !strcmp( lpProcessName, pe32.szExeFile ) )
  116.         {
  117.             CloseHandle( hSnapshot );
  118.             return pe32.th32ProcessID;
  119.         }
  120.     }
  121.     while( Process32Next( hSnapshot, &pe32 ) );
  122.  
  123.     CloseHandle( hSnapshot );
  124.  
  125.     return NULL;
  126. }
  127.  
  128. DWORD_PTR GetProcessIdEx( const char* lpProcessName )
  129. {
  130.     DWORD_PTR dwProcessArray[ 1024 ];
  131.     DWORD_PTR dwNeeded          = NULL;
  132.     DWORD_PTR dwProcessCount    = NULL;
  133.     DWORD_PTR dwIndex           = NULL;
  134.  
  135.     if( !lpProcessName )
  136.         return NULL;
  137.  
  138.     if( !EnumProcesses( dwProcessArray, sizeof( dwProcessArray ), &dwNeeded ) )
  139.         return NULL;
  140.  
  141.     dwProcessCount = dwNeeded / sizeof( DWORD_PTR );
  142.  
  143.     for( dwIndex = NULL; dwIndex < dwProcessCount; dwIndex++ )
  144.     {
  145.         if( dwProcessArray[ dwIndex ] )
  146.         {
  147.             char lpCurrentProcess[ MAX_PATH ] = "<unknown>";
  148.  
  149.             HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessArray[ dwIndex ] );
  150.  
  151.             if( hProcess != INVALID_HANDLE_VALUE )
  152.             {
  153.                 HMODULE     hModule;
  154.                 DWORD_PTR   dwNeeded;
  155.  
  156.                 if( EnumProcessModules( hProcess, &hModule, sizeof( hModule ), &dwNeeded ) )
  157.                 {
  158.                     GetModuleBaseName( hProcess, hModule, lpCurrentProcess, sizeof( lpCurrentProcess ) / sizeof( char ) );
  159.                 }
  160.             }
  161.  
  162.             if( !strcmp( lpProcessName, lpCurrentProcess ) )
  163.                 return dwProcessArray[ dwIndex ];
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment