這是我在Windows中創建一個帳戶的示例。如果您想將此帳戶添加到本地用戶,那麼您可以登錄當前機器,因此需要調用NetLocalGroupAddMembers函數。作爲本地組名稱您應該只鍵入「用戶」,但它在Windows的語言版本上有所不同。在波蘭版本,它將是「Użytkownicy」等。 如果您想要在遠程服務器上添加一個帳戶,那麼它需要調用NetGetDCName和驢第一個參數給它的服務器地址。然後在此示例中的其他函數中使用PDC。 請記住以管理員身份運行此代碼(即使在調試時也是IDE)或者您將有訪問被拒絕錯誤。
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <lmcons.h>
#include <lmaccess.h>
#include <lmerr.h>
#include <lmapibuf.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "netapi32.lib")
NET_API_STATUS NetSample(LPWSTR lpszDomain,
LPWSTR lpszUser,
LPWSTR lpszPassword,
LPWSTR lpszLocalGroup)
{
USER_INFO_1 user_info;
LOCALGROUP_INFO_1 localgroup_info;
LOCALGROUP_MEMBERS_INFO_3 localgroup_members;
LPWSTR lpszPrimaryDC = NULL;
NET_API_STATUS err = 0;
DWORD parm_err = 0;
// Get the name of the primary domain controller.
// Be sure to free the returned buffer.
// On local machine we don't need to do this
//err = NetGetDCName(NULL, // local computer
// NULL, // domain name
// (LPBYTE *) &lpszPrimaryDC); // returned PDC
//if (err != 0)
//{
// printf("Error getting DC name: %d\n", err);
// return(err);
//}
// Set up the USER_INFO_1 structure.
user_info.usri1_name = lpszUser;
user_info.usri1_password = lpszPassword;
user_info.usri1_priv = USER_PRIV_USER;
user_info.usri1_home_dir = TEXT("");
user_info.usri1_comment = TEXT("");
user_info.usri1_flags = UF_SCRIPT;
user_info.usri1_script_path = TEXT("");
//err = NetUserDel(NULL, L"NowyUse2r"); this will delete user
err = NetUserAdd(NULL/*lpszPrimaryDC*/, // PDC name
1, // level
(LPBYTE) &user_info, // input buffer
&parm_err); // parameter in error
switch (err)
{
case 0:
printf("User successfully created.\n");
break;
case NERR_UserExists:
printf("User already exists.\n");
err = 0;
break;
case ERROR_INVALID_PARAMETER:
printf("Invalid parameter error adding user; parameter index = %d\n",
parm_err);
NetApiBufferFree(lpszPrimaryDC);
return(err);
case ERROR_ACCESS_DENIED:
printf("ERROR_ACCESS_DENIED; parameter index = %d\n",
parm_err);
NetApiBufferFree(lpszPrimaryDC);
return(err);
default:
printf("Error adding user: %d\n", err);
NetApiBufferFree(lpszPrimaryDC);
return(err);
}
// Set up the LOCALGROUP_INFO_1 structure.
localgroup_info.lgrpi1_name = lpszLocalGroup;
localgroup_info.lgrpi1_comment = TEXT("Sample local group.");
err = NetLocalGroupAdd(NULL, // PDC name
1, // level
(LPBYTE) &localgroup_info, // input buffer
&parm_err); // parameter in error
switch (err)
{
case 0:
printf("Local group successfully created.\n");
break;
case ERROR_ALIAS_EXISTS:
printf("Local group already exists.\n");
err = 0;
break;
case ERROR_INVALID_PARAMETER:
printf("Invalid parameter error adding local group; parameter index = %d\n",
err, parm_err);
NetApiBufferFree(lpszPrimaryDC);
//return(err);
default:
printf("Error adding local group: %d\n", err);
NetApiBufferFree(lpszPrimaryDC);
//return(err);
}
// Now add the user to the local group.
localgroup_members.lgrmi3_domainandname = lpszUser;
err = NetLocalGroupAddMembers(NULL, // PDC name
lpszLocalGroup, // group name
3, // name
(LPBYTE) &localgroup_members, // buffer
1); // count
switch (err)
{
case 0:
printf("User successfully added to local group.\n");
break;
case ERROR_MEMBER_IN_ALIAS:
printf("User already in local group.\n");
err = 0;
break;
default:
printf("Error adding user to local group: %d\n", err);
break;
}
NetApiBufferFree(lpszPrimaryDC);
return(err);
}
int main()
{
NET_API_STATUS err = 0;
printf("Calling NetSample.\n");
err = NetSample(L"",
L"NewUser",
L"SamplePswd",
L"Users");
printf("NetSample returned %d\n", err);
return(0);
}
我想我們需要更多的信息來幫助,也許也會看到一些代碼。 – Rup
可能是UAC問題。您可能想要檢查程序是否以管理員身份運行。 –
Eddited。謝謝 – kubudi