0

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

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

4

1 回答 1

0

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

于 2020-03-28T21:47:33.187 回答