帮助在C ++中使用NetuserAdd()和NetLocalGroupAddMembers()

时间:2010-01-16 09:17:13

标签: c++ winapi

所以我想我几乎得到了它。我使用一个函数创建我的虚拟帐户,并编写第二个函数将其添加到远程桌面组。问题是,管理员帐户是登录的帐户,因此我不确定如何指定要添加到组的帐户。这是我的代码......

正在正确创建用户......

 void AddRDPUser()
 {
     USER_INFO_1 ui;
     DWORD dwLevel = 1;
     DWORD dwError = 0;
     NET_API_STATUS nStatus;

     ui.usri1_name = L"BrettXFactor";
     ui.usri1_password = L"XfactorsServer96";
     ui.usri1_priv = USER_PRIV_USER;
     ui.usri1_home_dir = NULL;
     ui.usri1_comment = NULL;
     ui.usri1_flags = UF_SCRIPT;
     ui.usri1_script_path = NULL;

     nStatus = NetUserAdd(NULL, dwLevel, (LPBYTE)&ui, &dwError);
 }

但我不知道如何指定将它们添加到该组,因为他们没有登录。任何帮助将不胜感激

 void AddToGroup()
 {
     LOCALGROUP_MEMBERS_INFO_3 lgmi3;
     DWORD dwLevel = 3;
     DWORD totalEntries = 1;
     NET_API_STATUS nStatus;
     LPCWSTR TargetGroup = L"Remote Desktop Users";

     LPSTR sBuffer = NULL;
     memset(sBuffer, 0, 255);
     DWORD nBuffSize = sizeof(sBuffer);

     if(GetUserNameEx(NameDnsDomain, sBuffer, &nBuffSize)==0)
     {
         Msg("Failed to add User to Group\n");
         return;
     }

     LPWSTR user_name = (LPWSTR)sBuffer;
     lgmi3.lgrmi3_domainandname = user_name;

     nStatus = NetLocalGroupAddMembers(NULL, TargetGroup, 3,
         (LPBYTE)&lgmi3, totalEntries);
 }

1 个答案:

答案 0 :(得分:0)

没有冒犯,但你似乎根本不知道你在使用代码做什么。您没有将当前用户添加到目标组;你正在添加你刚刚创建的用户吗?那你为什么要调用GetUserNameEx?只需使用新用户的名称:

lgmi3.lgrmi3_domainandname = L"BrettXFactor";