Exception : 0xC0000005 in NetUserAdd

-4

I am trying to create new users, group and adding these users to group. I am able to create group successfully but I am getting exception while creating user.

I have created a group and deleted a group before creating user to check if I have problem in netapi32.dll but these 2 function run properly.

I have open Visual Studio in : Run as Administrator

OS : Windows 10 Professional Architecture : x64 Development Tool-set : MSVC 2017 Target : Debug x64

Exception thrown at 0x00007FFD710BF4A6 (ntdll.dll) in TestBench.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF. occurred

Code :

void createUser(const std::wstring& serverName, const std::wstring& userName, const std::wstring& password, uint8_t userPriviledge, const std::list<std::wstring>& groupNameList, const std::wstring& comment)
{
    try
    {
        WCHAR userName1[2500];
    WCHAR password1[2500];
    DWORD dwError = 0;
    NET_API_STATUS  returnVal;
    USER_INFO_1     userInfo;
    //userInfo.usri1_name = const_cast<LPWSTR>(userName.c_str());
    wcsncpy_s(userName1, userName.c_str(), userName.length());
    userInfo.usri1_name = userName1;
    //userInfo.usri1_password = const_cast<LPWSTR>(password.c_str());
    wcsncpy_s(password1, password.c_str(), password.length());
    userInfo.usri1_password = password1;
    userInfo.usri1_priv = userPriviledge == USER_PRIV_ADMIN ? USER_PRIV_ADMIN : userPriviledge == USER_PRIV_GUEST ? USER_PRIV_GUEST : USER_PRIV_USER;
    userInfo.usri1_home_dir = NULL;
    userInfo.usri1_flags = UF_SCRIPT;
    userInfo.usri1_script_path = NULL;
    //serverName.empty() ? nullptr : serverName.c_str()
    returnVal = NetUserAdd(NULL, 1, reinterpret_cast<LPBYTE>(std::addressof(userInfo)), &dwError);
    //returnVal = NetUserAdd(NULL, 1, (LPBYTE)&userInfo, NULL);
    if (returnVal == NERR_Success)
    {
        std::wstring messageInfo = TEXT("User Create Successfully");
        std::wcout << messageInfo;
        if (not groupNameList.empty())
        {
            if (not std::all_of(groupNameList.begin(), groupNameList.end(), [&serverName, &userName](const std::wstring& groupName) {
                LOCALGROUP_MEMBERS_INFO_3   groupInfo;
                groupInfo.lgrmi3_domainandname = const_cast<LPWSTR>(userName.c_str());
                NET_API_STATUS returnVal = NetLocalGroupAddMembers(serverName.empty() ? nullptr : serverName.c_str(), groupName.c_str(), 3, reinterpret_cast<LPBYTE>(std::addressof(groupInfo)), 1);
                if (returnVal not_eq NERR_Success)
                {
                    //Message Box
                    std::wstring errStr = TEXT("Error while adding user to group. Error : ");
                    if (returnVal == NERR_GroupNotFound)
                        errStr += TEXT("The local group specified by the groupname parameter does not exist.");
                    if (returnVal == ERROR_ACCESS_DENIED)
                        errStr += TEXT("The user does not have access to the requested information");
                    else if (returnVal == ERROR_NO_SUCH_MEMBER)
                        errStr += TEXT("One or more of the members specified do not exist. Therefore, no new members were added.");
                    else if (returnVal == ERROR_MEMBER_IN_ALIAS)
                        errStr += TEXT("One or more of the members specified were already members of the local group. No new members were added.");
                    else if (returnVal == ERROR_INVALID_MEMBER)
                        errStr += TEXT("One or more of the members cannot be added because their account type is invalid. No new members were added.");
                    else
                        errStr += TEXT("Unknown Error Code : ") + std::to_wstring(returnVal);
                    std::wcerr << errStr << std::endl;
                    return false;
                }
                return true;
            }))
            {
                deleteUser(serverName, userName);
                return;
            }
        }
        messageInfo = TEXT("User Added to group Successfully");
        std::wcout << messageInfo;
        return;
    }
    else
    {
        std::wstring errStr = TEXT("Error while creating user. Error : ");
        if (returnVal == ERROR_ACCESS_DENIED)
            errStr += TEXT("The user does not have access to the requested information");
        else if (returnVal == NERR_InvalidComputer)
            errStr += TEXT("The computer name is invalid.");
        else if (returnVal == NERR_NotPrimary)
            errStr += TEXT("The operation is allowed only on the primary domain controller of the domain.");
        else if (returnVal == NERR_GroupExists)
            errStr += TEXT("The group already exists. ");
        else if (returnVal == NERR_UserExists)
            errStr += TEXT("The user account already exists.");
        else if (returnVal == NERR_PasswordTooShort)
            errStr += TEXT("The password is shorter than required. (The password could also be too long, be too recent in its change history, not have enough unique characters, or not meet another password policy requirement.)");
        else
            errStr += TEXT("Unknown Error Code : ") + std::to_wstring(returnVal);
        std::wcerr << errStr << std::endl;
        return;
    }
    }
    catch (const std::exception& ex)
    {
        std::cerr << "Excption : " << ex.what() << std::endl;
    }
}

void deleteGroup(const std::wstring& serverName, const std::wstring& groupName)
{
    NET_API_STATUS          returnVal;
    returnVal = NetLocalGroupDel(serverName.empty() ? nullptr : serverName.c_str(), groupName.c_str());
    if (returnVal == NERR_Success)
    {
        std::wstring messageInfo = TEXT("Group Deletion Successfully");
        std::wcout << messageInfo;
    }
    else
    {
        std::wstring errStr = TEXT("Error while deleting groups. Error : ");
        if (returnVal == ERROR_ACCESS_DENIED)
            errStr += TEXT("The user does not have access to the requested information");
        else if (returnVal == NERR_InvalidComputer)
            errStr += TEXT("The computer name is invalid.");
        else if (returnVal == NERR_NotPrimary)
            errStr += TEXT("The operation is allowed only on the primary domain controller of the domain.");
        else if (returnVal == NERR_GroupNotFound)
            errStr += TEXT("The local group specified by the groupname parameter does not exist.");
        else if (returnVal == ERROR_NO_SUCH_ALIAS)
            errStr += TEXT("The specified local group does not exist.");
        else
            errStr += TEXT("Unknown Error Code : ") + std::to_wstring(returnVal);
        std::wcerr << errStr << std::endl;
    }
}

Main Code :

int main()
{
    std::wstring serverName, groupName = TEXT("TestGroup"), comment = TEXT("Group Created for Testing");
    std::wstring userName = TEXT("user123"), password = TEXT("pasword123");
    std::list<std::wstring> groupNameList;

    getGroupList(serverName);
    std::wcout << std::endl << std::endl;
    createGroup(serverName, groupName, comment);
    getGroupList(serverName);
    std::wcout << std::endl << std::endl;
    deleteGroup(serverName, groupName);
    getGroupList(serverName);
    std::wcout << std::endl << std::endl;

    createUser(serverName, userName, password, 1, groupNameList, comment);
    deleteUser(serverName, userName);
}
c++
windows
winapi
asked on Stack Overflow Nov 26, 2018 by Dark Sorrow • edited Nov 26, 2018 by Dark Sorrow

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0