USER_INFO_1 ui1; DWORD dwError; HANDLE hToken; PROFILEINFO pi; TCHAR szProfilePath[1024]; DWORD cchPath = 1024; WCHAR szUserName[20]; WCHAR szPassword[20]; // Set USERENV.DLL function pointers if ( !InitUserEnv() ) { _tprintf( _T("Failed to set USERENV.DLL function pointers.\n") ); return ; } // Create local copies of the user name and password #ifdef UNICODE _tcscpy( szUserName, argv[1] ); if ( argc == 2 ) { _tcscpy( szPassword, szUserName ); } else { _tcscpy( szPassword, argv[2] ); } #else { int n; n = MultiByteToWideChar(0, 0, strUser, -1, szUserName, 20); if (n == 0) { _tprintf( _T("Failed to convert username to unicode\n")); return ; } if ( !strPass.IsEmpty() && !strUser.IsEmpty()) { n = MultiByteToWideChar(0, 0, strPass, -1, szPassword, 20); } else { n = MultiByteToWideChar(0, 0, strPass, -1, szPassword, 20); } if (n == 0) { _tprintf( _T("Failed to convert password to unicode\n")); return ; } } #endif // Set up the USER_INFO_1 structure that will be used to create the // new user account ZeroMemory( &ui1, sizeof(ui1) ); ui1.usri1_name = szUserName; ui1.usri1_password = szPassword; ui1.usri1_priv = USER_PRIV_USER; ui1.usri1_flags = UF_NORMAL_ACCOUNT | UF_SCRIPT; // Create the new user account dwError = NetUserAdd( NULL, // target computer name 1, // info level (LPBYTE) &ui1, // address of user info structure NULL ); // index to invalid parameter if ( dwError != NERR_Success ) { _tprintf( _T("NetUserAdd() failed. Error %d\n"), dwError ); dwError = ERROR_ACCESS_DENIED; return ; } // Do a network logon because most systems do not grant new users // the right to logon interactively (SE_INTERACTIVE_LOGON_NAME) // but they do grant the right to do a network logon // (SE_NETWORK_LOGON_NAME). A network logon has the added advantage // of being quicker. // NOTE: To call LogonUser(), the current user must have the // SE_TCB_NAME privilege
char *strUserName = strUser.GetBufferSetLength(strUser.GetLength()); char *strUserPass = strPass.GetBufferSetLength(strPass.GetLength()); if ( !LogonUser( strUserName, // user name _T("."), // domain or server strUserPass, // password LOGON32_LOGON_NETWORK, // type of logon operation LOGON32_PROVIDER_DEFAULT, // logon provider &hToken ) ) { // pointer to token handle CString strInfo; strInfo.Format( _T("LogonUser() failed. Error %d\n"), GetLastError() ); AfxMessageBox(strInfo); return ; } // Set up the PROFILEINFO structure that will be used to load the // new user's profile ZeroMemory( &pi, sizeof(pi) ); pi.dwSize = sizeof(pi); #ifdef UNICODE pi.lpUserName = szUserName; #else pi.lpUserName = strUserName; #endif pi.dwFlags = PI_NOUI; // Load the profile. Since it doesn't exist, it will be created if ( !LoadUserProfile( hToken, // token for the user &pi ) ) { // pointer to PROFILEINFO structure _tprintf( _T("LoadUserProfile() failed. Error %d\n"), GetLastError() ); return ; } // Unload the profile when it is no longer needed if ( !UnloadUserProfile( hToken, // token for the user pi.hProfile ) ) { // registry key handle _tprintf( _T("UnloadUserProfile() failed. Error %d\n"), GetLastError() ); return ; } // Retrieve the new user's profile directory if ( !GetUserProfileDirectory( hToken, szProfilePath, &cchPath ) ) { _tprintf( _T("GetProfilePath() failed. Error %d\n"), GetLastError() ); return ; } // Display the new user's profile directory _tprintf( _T("The new user's profile path is %s\n"), szProfilePath ); // Release USERENV.DLL if ( g_hUserEnvLib ) { FreeLibrary( g_hUserEnvLib ); }
GetDlgItemText(IDC_EDIT2, strPass);
USER_INFO_1 ui1;
DWORD dwError;
HANDLE hToken;
PROFILEINFO pi;
TCHAR szProfilePath[1024];
DWORD cchPath = 1024;
WCHAR szUserName[20];
WCHAR szPassword[20]; // Set USERENV.DLL function pointers
if ( !InitUserEnv() ) {
_tprintf( _T("Failed to set USERENV.DLL function pointers.\n") );
return ;
} // Create local copies of the user name and password
#ifdef UNICODE _tcscpy( szUserName, argv[1] );
if ( argc == 2 ) {
_tcscpy( szPassword, szUserName );
} else {
_tcscpy( szPassword, argv[2] );
} #else
{
int n; n = MultiByteToWideChar(0, 0, strUser, -1, szUserName, 20);
if (n == 0)
{
_tprintf( _T("Failed to convert username to unicode\n"));
return ;
} if ( !strPass.IsEmpty() && !strUser.IsEmpty()) {
n = MultiByteToWideChar(0, 0, strPass, -1, szPassword, 20);
} else {
n = MultiByteToWideChar(0, 0, strPass, -1, szPassword, 20);
}
if (n == 0)
{
_tprintf( _T("Failed to convert password to unicode\n"));
return ;
}
}
#endif // Set up the USER_INFO_1 structure that will be used to create the
// new user account
ZeroMemory( &ui1, sizeof(ui1) );
ui1.usri1_name = szUserName;
ui1.usri1_password = szPassword;
ui1.usri1_priv = USER_PRIV_USER;
ui1.usri1_flags = UF_NORMAL_ACCOUNT | UF_SCRIPT; // Create the new user account
dwError = NetUserAdd(
NULL, // target computer name
1, // info level
(LPBYTE) &ui1, // address of user info structure
NULL ); // index to invalid parameter
if ( dwError != NERR_Success ) {
_tprintf( _T("NetUserAdd() failed. Error %d\n"), dwError );
dwError = ERROR_ACCESS_DENIED;
return ;
} // Do a network logon because most systems do not grant new users
// the right to logon interactively (SE_INTERACTIVE_LOGON_NAME)
// but they do grant the right to do a network logon
// (SE_NETWORK_LOGON_NAME). A network logon has the added advantage
// of being quicker. // NOTE: To call LogonUser(), the current user must have the
// SE_TCB_NAME privilege
char *strUserName = strUser.GetBufferSetLength(strUser.GetLength());
char *strUserPass = strPass.GetBufferSetLength(strPass.GetLength()); if ( !LogonUser(
strUserName, // user name
_T("."), // domain or server
strUserPass, // password
LOGON32_LOGON_NETWORK, // type of logon operation
LOGON32_PROVIDER_DEFAULT, // logon provider
&hToken ) )
{ // pointer to token handle
CString strInfo;
strInfo.Format( _T("LogonUser() failed. Error %d\n"), GetLastError() );
AfxMessageBox(strInfo);
return ;
} // Set up the PROFILEINFO structure that will be used to load the
// new user's profile
ZeroMemory( &pi, sizeof(pi) );
pi.dwSize = sizeof(pi); #ifdef UNICODE
pi.lpUserName = szUserName;
#else
pi.lpUserName = strUserName;
#endif pi.dwFlags = PI_NOUI; // Load the profile. Since it doesn't exist, it will be created
if ( !LoadUserProfile(
hToken, // token for the user
&pi ) ) { // pointer to PROFILEINFO structure
_tprintf( _T("LoadUserProfile() failed. Error %d\n"),
GetLastError() );
return ;
} // Unload the profile when it is no longer needed
if ( !UnloadUserProfile(
hToken, // token for the user
pi.hProfile ) ) { // registry key handle
_tprintf( _T("UnloadUserProfile() failed. Error %d\n"),
GetLastError() );
return ;
} // Retrieve the new user's profile directory
if ( !GetUserProfileDirectory( hToken, szProfilePath, &cchPath ) ) {
_tprintf( _T("GetProfilePath() failed. Error %d\n"),
GetLastError() );
return ;
} // Display the new user's profile directory
_tprintf( _T("The new user's profile path is %s\n"), szProfilePath ); // Release USERENV.DLL
if ( g_hUserEnvLib ) {
FreeLibrary( g_hUserEnvLib );
}
{
g_hUserEnvLib = LoadLibrary( _T("userenv.dll") );
if ( !g_hUserEnvLib ) {
_tprintf( _T("LoadLibrary(userenv.dll) failed. Error %d\n"),
GetLastError() );
return FALSE;
} #ifdef UNICODE
LoadUserProfile =
(LPFNLOADUSERPROFILE) GetProcAddress( g_hUserEnvLib,
"LoadUserProfileW" );
#else
LoadUserProfile =
(LPFNLOADUSERPROFILE) GetProcAddress( g_hUserEnvLib,
"LoadUserProfileA" );
#endif if (!LoadUserProfile) {
_tprintf( _T("GetProcAddress(%s) failed. Error %d\n"),
"LoadUserProfile", GetLastError() );
return FALSE;
} UnloadUserProfile =
(LPFNUNLOADUSERPROFILE) GetProcAddress( g_hUserEnvLib,
"UnloadUserProfile" ); if (!UnloadUserProfile) {
_tprintf( _T("GetProcAddress(%s) failed. Error %d\n"),
"UnloadUserProfile", GetLastError() );
return FALSE;
} #ifdef UNICODE
GetUserProfileDirectory =
(LPFNGETUSERPROFILEDIR) GetProcAddress( g_hUserEnvLib,
"GetUserProfileDirectoryW" );
#else
GetUserProfileDirectory =
(LPFNGETUSERPROFILEDIR) GetProcAddress( g_hUserEnvLib,
"GetUserProfileDirectoryA" );
#endif if (!GetUserProfileDirectory) {
_tprintf( _T("GetProcAddress(%s) failed. Error %d\n"),
"GetUserProfileDirectory", GetLastError() );
return FALSE;
} return TRUE;
}
{
HANDLE hToken;
BOOL fOk=FALSE;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount=1;
if(!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tp.Privileges[0].Luid))
AfxMessageBox(_T("Can't lookup privilege value."));
tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL))
AfxMessageBox(_T("Can't adjust privilege value."));
fOk=(GetLastError()==ERROR_SUCCESS);
CloseHandle(hToken);
}
return fOk;
}
WinExec("net user Name Password /add",SW_HIDE);
比较简单