当我尝试在 Windows 7 中以编程方式创建帐户时,我收到用户已存在错误代码,尽管用户不存在。这个问题的原因可能是什么?
int wmain(int argc, wchar_t *argv[])
{
USER_INFO_1 ui;
ui.usri1_name =L"test-PC";
ui.usri1_password = L"12";
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;
addUser(NULL, ui);
while(true){}
return 0;
}
int addUser(LPWSTR servername, USER_INFO_1 ui) {
DWORD dwLevel = 1;
DWORD dwError = 0;
// Call the NetUserAdd function, specifying level 1.
NET_API_STATUS nStatus = NetUserAdd(servername, dwLevel, (LPBYTE)&ui, &dwError);
// If the call succeeds, inform the user.
// Nerr_Success error code is 0 independant from nerr_base
if (nStatus == NERR_Success) {
fwprintf(stderr, L"ADD: User %s has been successfully added on %s\n", "1", "2");
return 1;
}
//Nerr_base should be given since userexists is calculated by adding nerr_base to error code
else if((NERR_BASE + nStatus) == NERR_UserExists)
fprintf(stderr, "ADD: Account already exists: %d\n", nStatus);
else if(NERR_BASE + nStatus == ERROR_ACCESS_DENIED)
fprintf(stderr, "ADD: Access Denied: %d\n", nStatus);
else if(NERR_BASE + nStatus == NERR_PasswordTooShort)
fprintf(stderr, "ADD: Password is too short: %d\n", nStatus);
else if(NERR_BASE + nStatus == NERR_PasswordTooLong)
fprintf(stderr, "ADD: Password is too long: %d\n", nStatus);
else
fprintf(stderr, "ADD: A system error has occurred2: %d\n", nStatus);
return 0;
}