gpt4 book ai didi

c++ - 在 CreateProcess 之后调用 GetModuleFileNameEx 时出现 ERROR_INVALID_HANDLE

转载 作者:可可西里 更新时间:2023-11-01 11:53:03 27 4
gpt4 key购买 nike

成功调用 CreateProcess 后,我尝试使用 GetModuleFileNameEx 获取创建的进程的路径(lpApplicationName 和 lpCommandLine 参数可以变化或为空,因此在这种情况下它们不可靠)。问题是 GetModuleFileNameEx 失败并出现错误 6 (ERROR_INVALID_HANDLE),从而使其缓冲区中包含无效数据。我无法理解原因,因为 CreateProcess 成功并且进程句柄应该已正确保存在 pi.hProcess 中。

希望您能有所启发,在此先感谢您!

编辑:更新:我注意到删除 CREATE_SUSPENDED 也可以解决此问题,但我需要设置该标志。我该怎么办?

// Defining GetModuleFileNameExA function
typedef DWORD (WINAPI *fGetModuleFileNameExA)
(
HANDLE hProcess,
HMODULE hModule,
LPSTR lpFilename,
DWORD nSize
);
//Load dinamically DLL function on program startup:
fGetModuleFileNameExA _GetModuleFileNameExA = (fGetModuleFileNameExA) GetProcAddress( LoadLibraryA("Psapi.dll"), "GetModuleFileNameExA");

// **** OTHER UNRELATED CODE HERE ****


PROCESS_INFORMATION pi;

//This call succeeds
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];

//Problem here: call fails with error 6
if (!_GetModuleFileNameExA(pi.hProcess, NULL, ProcessPath, MAX_PATH)) {GetLastError();}

//Invalid data is displayed
MessageBoxA(0, ProcessPath, "GetModuleFileNameEx",0);

最佳答案

来自CreateProcess documentation在 MSDN 上:

Note that the function returns before the process has finished initialization. If a required DLL cannot be located or fails to initialize, the process is terminated. To get the termination status of a process, call GetExitCodeProcess.

...

The calling thread can use the WaitForInputIdle function to wait until the new process has finished its initialization and is waiting for user input with no input pending. This can be useful for synchronization between parent and child processes, because CreateProcess returns without waiting for the new process to finish its initialization. For example, the creating process would use WaitForInputIdle before trying to find a window associated with the new process.

Similar question

关于c++ - 在 CreateProcess 之后调用 GetModuleFileNameEx 时出现 ERROR_INVALID_HANDLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com