下列程式代碼範例示範如何使用 NetUserAdd 函式建立新的電腦帳戶。
以下是管理電腦帳戶的考慮:
- 電腦帳戶名稱應該全部大寫,以便與帳戶管理工具保持一致性。
- 計算機帳戶名稱一律有尾端貨幣符號 ($)。 任何用來管理計算機帳戶的函式都必須建置計算機名稱,使計算機帳戶名稱的最後一個字元是貨幣符號 ($)。 對於網域間信任,帳戶名稱為 TrustingDomainName$。
- 計算機名稱長度上限為 MAX_COMPUTERNAME_LENGTH (15)。 這個長度不包括尾端美元符號($)。
- 新計算機帳戶的密碼應該是計算機帳戶名稱的小寫表示法,而沒有尾端貨幣符號 ($)。 對於網域間信任,密碼可以是符合關聯性信任端所指定值的任意值。
- 密碼長度上限為 LM20_PWLEN (14)。 如果計算機帳戶名稱超過此長度,密碼應該截斷為這個長度。
- 在電腦帳戶建立期間提供的密碼只有在電腦帳戶在網域上變成作用中之前才有效。 在信任關係啟用期間會建立新的密碼。
#include <windows.h>
#include <lm.h>
#pragma comment(lib, "netapi32.lib")
BOOL AddMachineAccount(
LPWSTR wTargetComputer,
LPWSTR MachineAccount,
DWORD AccountType
)
{
LPWSTR wAccount;
LPWSTR wPassword;
USER_INFO_1 ui;
DWORD cbAccount;
DWORD cbLength;
DWORD dwError;
//
// Ensure a valid computer account type was passed.
//
if (AccountType != UF_WORKSTATION_TRUST_ACCOUNT &&
AccountType != UF_SERVER_TRUST_ACCOUNT &&
AccountType != UF_INTERDOMAIN_TRUST_ACCOUNT
)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
//
// Obtain number of chars in computer account name.
//
cbLength = cbAccount = lstrlenW(MachineAccount);
//
// Ensure computer name doesn't exceed maximum length.
//
if(cbLength > MAX_COMPUTERNAME_LENGTH) {
SetLastError(ERROR_INVALID_ACCOUNT_NAME);
return FALSE;
}
//
// Allocate storage to contain Unicode representation of
// computer account name + trailing $ + NULL.
//
wAccount=(LPWSTR)HeapAlloc(GetProcessHeap(), 0,
(cbAccount + 1 + 1) * sizeof(WCHAR) // Account + '$' + NULL
);
if(wAccount == NULL) return FALSE;
//
// Password is the computer account name converted to lowercase;
// you will convert the passed MachineAccount in place.
//
wPassword = MachineAccount;
//
// Copy MachineAccount to the wAccount buffer allocated while
// converting computer account name to uppercase.
// Convert password (in place) to lowercase.
//
while(cbAccount--) {
wAccount[cbAccount] = towupper( MachineAccount[cbAccount] );
wPassword[cbAccount] = towlower( wPassword[cbAccount] );
}
//
// Computer account names have a trailing Unicode '$'.
//
wAccount[cbLength] = L'$';
wAccount[cbLength + 1] = L'\0'; // terminate the string
//
// If the password is greater than the max allowed, truncate.
//
if(cbLength > LM20_PWLEN) wPassword[LM20_PWLEN] = L'\0';
//
// Initialize the USER_INFO_1 structure.
//
ZeroMemory(&ui, sizeof(ui));
ui.usri1_name = wAccount;
ui.usri1_password = wPassword;
ui.usri1_flags = AccountType | UF_SCRIPT;
ui.usri1_priv = USER_PRIV_USER;
dwError=NetUserAdd(
wTargetComputer, // target computer name
1, // info level
(LPBYTE) &ui, // buffer
NULL
);
//
// Free allocated memory.
//
if(wAccount) HeapFree(GetProcessHeap(), 0, wAccount);
//
// Indicate whether the function was successful.
//
if(dwError == NO_ERROR)
return TRUE;
else {
SetLastError(dwError);
return FALSE;
}
}
呼叫帳戶管理功能的用戶必須具有目標電腦上的系統管理員許可權。 在現有電腦帳戶的情況下,帳戶的建立者可以管理帳戶,而不論系統管理成員資格為何。 如需更多關於呼叫需要系統管理員權限之函式的詳細資訊,請參閱 以特殊權限執行。
您可以在目標計算機上授與 SeMachineAccountPrivilege,讓指定的使用者能夠建立電腦帳戶。 這可讓非系統管理員建立電腦帳戶。 呼叫端必須先啟用此許可權,才能新增計算機帳戶。 如需帳戶許可權的詳細資訊,請參閱 Privileges 和 Authorization Constants。