views:

189

answers:

3

I have a function with blow detail.

typedef part

   typedef DWORD (WINAPI *GETMODULEFILENAMEEX)(HANDLE hProcess, HMODULE hModule, LPTSTR   lpBaseName,DWORD nSize); 

   typedef BOOL (WINAPI *PFNTERMINATEPROCESS)(HANDLE hProcess,UINT uExitCode);

/// GetProcessName function

void GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize)
{

    HMODULE lib=LoadLibrary(TEXT("Psapi.dll"));
    GetModuleFileNameEx=(GETMODULEFILENAMEEX)GetProcAddress
    (lib,"GetModuleFileNameExW");
    _tcscpy_s(szProcessName, cchSize, TEXT("---"));


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

   if (hProcess == NULL) {
      _tcscpy_s(szProcessName, cchSize, TEXT("???"));
      return;
   }

   if (GetModuleFileNameEx(hProcess,(HMODULE)0, szProcessName, cchSize) 
       == 0) {
     if (!GetProcessImageFileName(hProcess, szProcessName, cchSize)) {
         _tcscpy_s(szProcessName, cchSize, TEXT("???"));
      }
   }
   CloseHandle(hProcess);
}

I want use this function in below function

BOOL WINAPI Hook_TerminateProcess(HANDLE hProcess,UINT uExitCode) {
  BOOL nResult=false;
  TCHAR szProcessName[MAX_PATH];


 nResult = ((PFNTERMINATEPROCESS)(PROC) g_TerminateProcess)(hProcess,uExitCode);

 GetProcessName(HandleToULong(hProcess),szProcessName,MAX_PATH); //my question here


    MessageBox(0, szProcessName  ,TEXT("My MessageBox Info"),MB_OK | MB_ICONERROR);

   return(nResult);
}

When I call function GetProcessName, this must return process name but it ??? str always. I call this function directly by PID, for example GetProcessName(2018,szProcessName,MAX_PATH);. 2018 for example is a pid and it work. I don't know why HandleToULong(hProcess) doesn't work. My hProcess must be a handle type certainly now how I fix this problem?

A: 

You must call GetProcessId rather than HandleToULong. You need a process ID, not a handle-converted-to-an-unsigned-long

Anthony Williams
of curse .thank you but again dont work and return ??? strhow i show hProcess value in message box .maybe it haven't correct value
Phoenix
You're calling GetProcessName AFTER g_TerminateProcess. In which case, the process probably no longer exists, so you can't open the handle. Try switching the calls round.
Anthony Williams
A: 

In Windows, a process ID is different from a process handle. You are taking the process handle in Hook_TerminateProcess and passing it into GetProcessName as a process ID. This will never work.

You should refactor GetProcessName to take a handle and then have an overload that takes a process ID. The process ID overload does the OpenProcess work to convert it into a handle and the CloseHandle work to clean it up.

After the refactoring, you'll have two methods:

void GetProcessName(HANDLE hProcess, PTSTR szProcessName, size_t cchSize);
void GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize);
Chris Schmich
Hook_TerminateProcess is instead of TerminateProcess winapi orginal function and this prototype is TerminateProcess(HANDLE hProcess,UINT uExitCode);now i have to pass hProcess with type of Handle to OpenProcess that give a pid with type of DWORDwhen i convert HANDLE to DWORD with func HAndletoulong it dont workand i cant retrive processname for my decision?in other hand i want to get process name with a pid of type Handle that terminateprocess gave to me.
Phoenix
Yes, you can convert a HANDLE to a DWORD with `HandleToULong`, but ***a handle is not a process ID***. Just because you can cast data doesn't mean it's correct. Please read my answer again. Remove the `OpenProcess` call and just have the process handle be the parameter to the method (instead of the process ID). If you need to pass a process ID in from somewhere else, create another method to call `OpenProcess` first to get a valid handle.
Chris Schmich
i refactor it but again dont workAnthony Williams told me use GetProcessId instead of HandletoUlong and pass hProcess(process handle) to PID(process id) but i dont know why openprocess return NULL how i show hProcess value in message box .maybe it haven't correct value
Phoenix
A: 
  1. How can you terminate the process then expect the handle to still be valid? cause if any clean up is performed, all data is lost(you don't explicitly copy the handle, so this can happen)
  2. your error seems to stem from where your retrieving hProcess, in which case you should check GetLastError to see why its failing
Necrolis
i placed nResult = ((PFNTERMINATEPROCESS)(PROC) g_TerminateProcess)(hProcess,uExitCode); after MessageBox but again i dont word i mean getprocess name return ??? str again
Phoenix