-1

I'm using the following library Lmaccess.h library netapi32.lib

So I'm trying to create a user programmatically through a windows desktop application. I can create a local user fine using the netUserAdd call.

USER_INFO_1 ui1_userInfo;
USER_INFO_2 ui2_userInfo;
DWORD dwLevel = 2;
//DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;

String sServerName = NULL;
if (edit_Server->Text.Trim() != "")
{
    sServerName = edit_Server->Text.Trim();
}

populateUserInfo(ui2_userInfo);
//populateUserInfo(ui1_userInfo);
try
{
    nStatus = NetUserAdd(sServerName.c_str(), dwLevel, (LPBYTE)&ui2_userInfo, &dwError );
    //nStatus = NetUserAdd(sServerName.c_str(), dwLevel, (LPBYTE)&ui1_userInfo, &dwError );
    printStatusResult(nStatus, dwError);
}
catch (...)      //Non Delphi Exception
{
    ShowMessage("BOOM");
    //return;
}

The problem is when I tried to add the user to a group using NetUserGetGroups I get a 2220 which is Group not Found.

NET_API_STATUS nStatus;
DWORD dwError = 1;
String sGroup = "Power Users";
String sServerName = edit_Server->Text.Trim();

nStatus = NetGroupAddUser(sServerName.c_str(), sGroup.c_str(), edit_Username->Text.c_str());
//nStatus = NetLocalGroupAddMembers(sServerName.c_str(), sGroup.c_str(), 3, (LPBYTE)&ui2_userInfo
printStatusResult(nStatus, dwError); 

I took a step back and I decided to try and use a user that I didn't create programmatically. I also called NetUserGetGroups

//Does work
sServerName = "vm-Win7Pro32";
String sUserName= "myUser";
DWORD dlevel = 0;
LPGROUP_USERS_INFO_0 hiME = NULL;
DWORD perfmaxlen = MAX_PREFERRED_LENGTH;
DWORD entriesread;
DWORD totalentries;
PDWORD_PTR resume_handle = NULL;
nStatus = NetUserGetGroups(sServerName.c_str(), sUserName.c_str(), dlevel, (LPBYTE*) &hiME, perfmaxlen, &entriesread, &totalentries);

if (nStatus != NERR_Success)
{
    printStatusResult(nStatus, NULL);
}
else
{
    ShowMessage(hiME->grui0_name);
}

This guy always returns success and displays None as my group even when the user is in a group. (At the very minimum it should be in the users group. )

What am I'm missing here. Is there a different function that I should be calling.

References Add user https://learn.microsoft.com/en-us/windows/win32/api/lmaccess/ns-lmaccess-user_info_2 https://learn.microsoft.com/en-us/windows/win32/api/lmaccess/nf-lmaccess-netuseradd?redirectedfrom=MSDN

NetGroupAddUser https://learn.microsoft.com/en-us/windows/win32/api/lmaccess/nf-lmaccess-netgroupadduser

NetUserGetGroups https://learn.microsoft.com/en-us/windows/win32/api/lmaccess/nf-lmaccess-netusergetgroups

themaniac27
  • 1,109
  • 3
  • 15
  • 27

1 Answers1

0

Should haved used netlocalgroupaddmembers

//
//NET_API_STATUS NET_API_FUNCTION NetLocalGroupAddMembers(
//  LPCWSTR servername,
//  LPCWSTR groupname,
//  DWORD   level,
//  LPBYTE  buf,
//  DWORD   totalentries
//);
dlevel = 3;
LOCALGROUP_MEMBERS_INFO_3 why;
String sDomainAndUser = sServerName + "\\\\" + sUserName;
why.lgrmi3_domainandname = sDomainAndUser.c_str();
nStatus = NetLocalGroupAddMembers(sServerName.c_str(), sGroup.c_str(), dlevel, (LPBYTE) &why, 1);
themaniac27
  • 1,109
  • 3
  • 15
  • 27