本文介绍了在CreateProcess后调用GetModuleFileNameEx时为ERROR_INVALID_HANDLE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



希望你能提前一些光, p>

编辑:一个更新:我注意到删除CREATE_SUSPENDED也消除了这个问题,但我需要设置标志。如何做?

  //定义GetModuleFileNameExA函数
typedef DWORD(WINAPI * fGetModuleFileNameExA)

HANDLE hProcess,
HMODULE hModule,
LPSTR lpFilename,
DWORD nSize
);
//在程序启动时加载dinamically DLL函数:
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);

CHAR ProcessPath [MAX_PATH];

//问题:调用失败,错误6
if(!_GetModuleFileNameExA(pi.hProcess,NULL,ProcessPath,MAX_PATH)){GetLastError();}

//显示无效数据
MessageBoxA(0,ProcessPath,GetModuleFileNameEx,0);

解决方案

从在MSDN上:


After a successful call to CreateProcess, I am trying to get the path of the created process using GetModuleFileNameEx (lpApplicationName and lpCommandLine parameters can vary or be null so they aren't reliable in this case). The problem is that GetModuleFileNameEx fails with error 6 (ERROR_INVALID_HANDLE), leaving its buffer with invalid data. I cannot understand the reason, since CreateProcess succeeds and process handle should have been saved correctly in pi.hProcess.

Hope you can shed some light, thanks in advance!

EDIT: An update: I noticed that removing the CREATE_SUSPENDED removes this problem too, but I need that flag set. How can I do?

// 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);
解决方案

From the CreateProcess documentation on MSDN:

Similar question

这篇关于在CreateProcess后调用GetModuleFileNameEx时为ERROR_INVALID_HANDLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:40