使用有效句柄时,使用Win32,GetModuleBaseName()和GetModuleFileNameEx()失败,GetLastError()= 6

时间:2012-05-08 16:37:00

标签: winapi

首先是一些简单的代码片段:

m_hProcessHandle = ::OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD | PROCESS_DUP_HANDLE | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION), FALSE, m_dwProcessIdentifier);
if (NULL != m_hProcessHandle)
{
    if (FALSE != ::OpenProcessToken(m_hProcessHandle, (TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE), &m_hImpersonizationToken))
    {
        wchar_t wszFullExecutableFileName[MAX_PATH];
        if (0 == ::GetModuleBaseName(m_hProcessHandle, NULL, wszFullExecutableFileName, (sizeof(wszFullExecutableFileName)/sizeof(wchar_t))))
        {
            __DebugMessage(L"GetModuleBaseName() failed with GetLastError() = %d", ::GetLastError());
        }
        else
        {
            if (0 == ::GetModuleFileNameEx(m_hProcessHandle, NULL, wszFullExecutableFileName, (sizeof(wszFullExecutableFileName)/sizeof(wchar_t))))
            {
                __DebugMessage(L"GetModuleFileNameEx() failed with GetLastError() = %d", ::GetLastError());
            }
            else
            {
                m_strFullFileName = wszFullExecutableFileName;
            }
        }
    }
}

OpenProcess()和OpenProcessToken()一样返回一个有效的句柄,但是当我调用后续的GetModuleBaseName()和GetModuleFileNameEx()函数时,我得到GetLastError()= 6(句柄无效)。我在Windows 7上以管理员身份运行该代码。是什么给出了?

欢呼声, GumbyTheBorg

1 个答案:

答案 0 :(得分:0)

您必须以管理员身份运行此程序,才能使其正常运行。我刚刚对其进行了测试,并且每行之后GetLastError()= 0,这意味着没有问题。