Unhandled Exception in Visual Studio 2017 : unsigned long at memory location 0x0023F6A0

676 Views Asked by At

I'm trying to determine whether the User account in which application running is belongs to Admin group or not in Visual studio 2017 using VC++. But when I'm running the Program in IDE it stops at the end of given function with exception dialog which says "Unhandled exception at 0x7697B022 in Target.exe: Microsoft C++ exception: unsigned long at memory location 0x00CFF638. occurred". But I click on 'Continue' in IDE, the execution goes ahead and completes the Program execution.

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;
}

I wanted to know what mistake I'm doing, what should be done to eliminate this exception.

1

There are 1 best solutions below

0
Raj G Y On

This is an try-catch block Error. When I removed version checking logic from the code, it starts working. probably "OSVERSIONINFOEX ptrVerInfo" this variables initialization has get skipped some how and it causes the error.