Advertisement
captmicro

Untitled

Jun 16th, 2010
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. HMODULE getRemoteModule(HANDLE proc, TCHAR *moduleName)
  2. {
  3.     if (proc == NULL) { return NULL; }
  4.  
  5.     HMODULE psapi;
  6.     psapi = LoadLibrary(L"psapi");
  7.  
  8.     _EnumProcessModules EPM;
  9.     EPM = (_EnumProcessModules)GetProcAddress(psapi, "EnumProcessModules");
  10.     if (EPM == NULL) { FreeLibrary(psapi); return NULL; }
  11.  
  12.     _GetModuleFileNameExW GMFNEXW;
  13.     GMFNEXW = (_GetModuleFileNameExW)GetProcAddress(psapi, "GetModuleFileNameExW");
  14.     if (GMFNEXW == NULL) { FreeLibrary(psapi); return NULL; }
  15.  
  16.     TCHAR currModuleName[MAX_PATH];
  17.     HMODULE modules[256];
  18.     DWORD cbNeeded;
  19.     UINT idx;
  20.  
  21.     if (EPM(proc, modules, sizeof(modules), &cbNeeded))
  22.     {
  23.         for (idx = 0; idx < (cbNeeded / sizeof(HMODULE)); idx++)
  24.         {
  25.             if (GMFNEXW(proc, modules[idx], currModuleName, sizeof(moduleName) / sizeof(TCHAR)))
  26.             {
  27.                 if (lstrcmpW(moduleName, currModuleName) == 0)
  28.                 {
  29.                     FreeLibrary(psapi);
  30.                     return modules[idx];
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     FreeLibrary(psapi);
  37.     return NULL;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement