I am using the Win Api function GetModuleBaseName to retrieve the process name from the current window (my application is 32 bits running on Win7 64 bits):
HWND Handle = GetForegroundWindow();
DWORD lpdwProcessId;
HANDLE PID;
WCHAR ProcessName[1024];
GetWindowThreadProcessId(Handle,&lpdwProcessId);
PID=OpenProcess(PROCESS_ALL_ACCESS,false,lpdwProcessId);
if (PID)
{
if(GetModuleBaseName(PID,NULL,ProcessName,sizeof ProcessName) == 0) {
wcscpy(ProcessName, L"??");
DWORD er = GetLastError();
printf("error code: %i\n", GetLastError());
}
}
else
{
wcscpy(ProcessName, L"??");
}
This code is working fine all 32 bits programs but not with 64 bits programs such as MSPaint where the last error returned is
error 299 : ERROR_PARTIAL_COPY : "Only part of a ReadProcessMemory or WriteProcessMemory request was completed."
MSDN doesn’t document why this particular error may happen. I read somewhere that this error can happen for the EnumProcessModulesEx due to problem between 32 and 64 bits programs, but no such thing is mentionned for GetModuleBaseName.
Is there a way to know where this is comming from and how to fix it?
thanks
Documentation for GetModuleBaseName suggests that calling GetProcessImageFileName or QueryFullProcessImageName will be more reliable than calling GetModuleBaseName with a NULL module handle.