0

我正在尝试使用 VC++ 在 Visual Studio 2017 中确定运行应用程序的用户帐户是否属于 Admin 组。但是,当我在 IDE 中运行该程序时,它会在给定函数的末尾停止,并出现异常对话框,该对话框显示“Target.exe 中 0x7697B022 处的未处理异常:Microsoft C++ 异常:内存位置 0x00CFF638 处的无符号长。发生”。但是我在 IDE 中单击“继续”,执行继续并完成程序执行。

BOOL TargetAppClass::IsUserInAdminGroup()
{
    BOOL fInAdminGroup = FALSE;
    DWORD dwError = ERROR_SUCCESS;
    HANDLE hToken = NULL;
    HANDLE hTokenToCheck = NULL;
    DWORD cbSize = 0;
    OSVERSIONINFO osver = { sizeof(osver) };

    // Open the primary access token of the process for query and duplicate.
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE,
        &hToken))
    {
        dwError = GetLastError();
    }


    OSVERSIONINFOEX ptrVerInfo;
    ZeroMemory(&ptrVerInfo, sizeof(OSVERSIONINFOEX));
    ptrVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    ptrVerInfo.dwMajorVersion = 10;
    ptrVerInfo.dwMinorVersion = 1;

    DWORDLONG dwlConditionMask = 0;
    int op = VER_GREATER_EQUAL;

    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, op);
    VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, op);
    VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, op);
    VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMINOR, op);


    if (!VerifyVersionInfoA(&ptrVerInfo, VER_MAJORVERSION, dwlConditionMask))
    {
        dwError = GetLastError();
    }

    if (ptrVerInfo.dwMajorVersion >= 6)
    {
        TOKEN_ELEVATION_TYPE elevType;
        if (!GetTokenInformation(hToken, TokenElevationType, &elevType,
            sizeof(elevType), &cbSize))
        {
            dwError = GetLastError();
        }

        // If limited, get the linked elevated token for further check.
        if (TokenElevationTypeLimited == elevType)
        {
            if (!GetTokenInformation(hToken, TokenLinkedToken, &hTokenToCheck,
                sizeof(hTokenToCheck), &cbSize))
            {
                dwError = GetLastError();
            }
        }
    }

    if (!hTokenToCheck)
    {
        if (!DuplicateToken(hToken, SecurityIdentification, &hTokenToCheck))
        {
            dwError = GetLastError();
        }
    }

    // Create the SID corresponding to the Administrators group.
    BYTE adminSID[SECURITY_MAX_SID_SIZE];
    cbSize = sizeof(adminSID);
    if (!CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, &adminSID,
        &cbSize))
    {
        dwError = GetLastError();
    }

    // To determine whether a SID is enabled in a token, that is, whether it 
    // has the SE_GROUP_ENABLED attribute, call CheckTokenMembership.
    if (!CheckTokenMembership(hTokenToCheck, &adminSID, &fInAdminGroup))
    {
        dwError = GetLastError();
    }

    // Centralized cleanup for all allocated resources.
    if (hToken)
    {
        CloseHandle(hToken);
        hToken = NULL;
    }
    if (hTokenToCheck)
    {
        CloseHandle(hTokenToCheck);
        hTokenToCheck = NULL;
    }

    // Throw the error if something failed in the function.
    if (ERROR_SUCCESS != dwError)
    {
        throw dwError;
    }

    return fInAdminGroup;
}

我想知道我在做什么错误,应该做些什么来消除这个异常。

4

1 回答 1

0

这是一个 try-catch 块错误。当我从代码中删除版本检查逻辑时,它开始工作。可能“OSVERSIONINFOEX ptrVerInfo”这个变量初始化已被跳过一些如何导致错误。

于 2019-07-10T09:00:35.687 回答