After a successful call to CreateProcess, I try to get the path to the created process using GetModuleFileNameEx (the lpApplicationName and lpCommandLine parameters may differ or be null so that in this case they are not reliable). The problem is that GetModuleFileNameEx failed with error 6 (ERROR_INVALID_HANDLE), leaving its buffer with invalid data. I cannot understand the reason, since CreateProcess succeeds and the process descriptor must be stored correctly in pi.hProcess.
Hope you can shed some light, thanks in advance!
EDIT: Update: I noticed that removing CREATE_SUSPENDED also fixes this problem, but I need this flag. How can i do
typedef DWORD (WINAPI *fGetModuleFileNameExA)
(
HANDLE hProcess,
HMODULE hModule,
LPSTR lpFilename,
DWORD nSize
);
fGetModuleFileNameExA _GetModuleFileNameExA = (fGetModuleFileNameExA) GetProcAddress( LoadLibraryA("Psapi.dll"), "GetModuleFileNameExA");
PROCESS_INFORMATION pi;
if (!CreateProcessW( ApplicationName,
CommandLine,
NewProcess.lpProcessAttributes,
NewProcess.lpThreadAttributes,
NewProcess.bInheritHandles,
CREATE_SUSPENDED | CREATE_NEW_CONSOLE,
NULL,
CurrentDirectory,
&NewProcess.bufStartupInfo,
&pi)
) MessageBoxA(0, "Error creating process", "", 0);
char ProcessPath[MAX_PATH];
if (!_GetModuleFileNameExA(pi.hProcess, NULL, ProcessPath, MAX_PATH)) {GetLastError();}
MessageBoxA(0, ProcessPath, "GetModuleFileNameEx",0);