建立新的電腦帳戶

下列程式碼範例示範如何使用 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,讓指定的使用者能夠建立電腦帳戶。 這可讓非系統管理員能夠建立電腦帳戶。 呼叫端必須先啟用此許可權,才能新增電腦帳戶。 如需帳戶許可權的詳細資訊,請參閱 許可權授權常數