I looked through similar questions, but did not find a solution to my problem.
Exception class:
class Exception{
public:
Exception(LPCWSTR text){
QMessageBox::information(0, "Catch",
QString::fromWCharArray(text) + ", Code: " +
QString::number(GetLastError()));
//EnumModules is return FALSE in function getHinstance, Code: 299
}
}
And main code:
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, 7068); //PID of opened calculator
if(handle == INVALID_HANDLE_VALUE)
throw Exception(L"invalid handle in function getHinstance");
int hNeeded = 1024;
HINSTANCE hModules[hNeeded];
DWORD bNeeded = 0;
PWSTR fileName = new WCHAR[512];
if(!EnumProcessModulesEx(handle, hModules, sizeof(hModules), &bNeeded, LIST_MODULES_ALL))
throw Exception(L"EnumModules is return FALSE in function getHinstance");
for(int i = 0; i < bNeeded / sizeof(HINSTANCE); ++i){
GetModuleBaseNameW(handle, hModules[i], fileName, sizeof(WCHAR) * 512);
if(lstrcmpW(fileName, moduleName) == 0){
delete [] fileName;
return hModules[i];
}
}
handle is a valid value of handle process
This code executes in a 64 bit process to enum modules in a 64 bit process
I can reproduce ERROR_PARTIAL_COPY (299) error when running the following code as a 64 bit process.
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 17152); //PID of opened notepad.exe
DWORD errCode = GetLastError();
HMODULE hModules[100];
DWORD bNeeded = 0;
EnumProcessModulesEx(handle, hModules, sizeof(hModules), &bNeeded, LIST_MODULES_ALL);
errCode = GetLastError();
And solve the error when use LIST_MODULES_64BIT instead of LIST_MODULES_ALL.
My notepad.exe is a 64 bit process.
So it seems you need use LIST_MODULES_64BIT for enumerating the modules of a 64-bit process when use EnumProcessModulesEx.
Or you can use EnumProcessModules:
EnumProcessModules(handle, hModules, sizeof(hModules), &bNeeded);