Sample Code
The sample code below can be compiled as a console application for Windows NT 4.0. This code demonstrates how to programmatically create a new user account, force a profile to be created for the new user, and retrieve the new profile directory: //**********************************************************************
//
// This program creates a new user account, forces a profile to be
// created for the new user, and retrieves the new profile directory
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (C) 1998 Microsoft Corporation. All rights reserved.
// Author: Jonathan Russ (jruss)
//
//********************************************************************** // NOTE: This code must be linked with netapi32.lib #include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <lm.h> // Declarations based on USERENV.H for Windows 2000 Beta 2
#define PI_NOUI 0x00000001 // Prevents displaying of messages
#define PI_APPLYPOLICY 0x00000002 // Apply NT4 style policy typedef struct _PROFILEINFO {
DWORD dwSize; // Must be set to sizeof(PROFILEINFO)
DWORD dwFlags; // See flags above
LPTSTR lpUserName; // User name (required)
LPTSTR lpProfilePath; // Roaming profile path
LPTSTR lpDefaultPath; // Default user profile path
LPTSTR lpServerName; // Validating DC name in netbios format
LPTSTR lpPolicyPath; // Path to the NT4 style policy file
HANDLE hProfile; // Registry key handle - filled by function
} PROFILEINFO, FAR * LPPROFILEINFO; // Typedefs for function pointers in USERENV.DLL
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNLOADUSERPROFILE) (
HANDLE hToken,
LPPROFILEINFO lpProfileInfo
); typedef BOOL (STDMETHODCALLTYPE FAR * LPFNUNLOADUSERPROFILE) (
HANDLE hToken,
HANDLE hProfile
); typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
HANDLE hToken,
LPTSTR lpProfileDir,
LPDWORD lpcchSize
); HMODULE g_hUserEnvLib = NULL;
LPFNLOADUSERPROFILE LoadUserProfile = NULL;
LPFNUNLOADUSERPROFILE UnloadUserProfile = NULL;
LPFNGETUSERPROFILEDIR GetUserProfileDirectory = NULL;
//**********************************************************************
//
// FUNCTION: InitUserEnv - This function dynamically links to
// USERENV.DLL and sets up the required function pointers
//
// PARAMETERS: none
//
// RETURN VALUE: TRUE if successful. Otherwise, FALSE.
//
//********************************************************************** BOOL InitUserEnv( void ) { 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;
}
//**********************************************************************
//
// FUNCTION: _tmain - This is the entry point for the program.
//
// PARAMETERS: argc - the number of command-line arguments
// argv - an array of null-terminated strings specifying
// the command-line arguments
// envp - an array of null-terminated strings specifying
// the environment strings
//
// RETURN VALUE: Zero if successful. Otherwise, non-zero.
//
//********************************************************************** #ifdef __cplusplus
extern "C"
#endif #ifdef UNICODE
int _cdecl
#else
int
#endif _tmain(int argc, _TCHAR **argv, _TCHAR **envp) { USER_INFO_1 ui1;
DWORD dwError;
HANDLE hToken;
PROFILEINFO pi;
TCHAR szProfilePath[1024];
DWORD cchPath = 1024;
WCHAR szUserName[20];
WCHAR szPassword[20]; // Check for the required command-line arguments
if (argc < 2) {
_tprintf( _T("Usage: AddUser <user> [password]\n") );
return -1;
} // Set USERENV.DLL function pointers
if ( !InitUserEnv() ) {
_tprintf( _T("Failed to set USERENV.DLL function pointers.\n") );
return -1;
} // 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, argv[1], -1, szUserName, 20);
if (n == 0)
{
_tprintf( _T("Failed to convert username to unicode\n"));
return -1;
} if ( argc == 2 ) {
n = MultiByteToWideChar(0, 0, argv[1], -1, szPassword, 20);
} else {
n = MultiByteToWideChar(0, 0, argv[2], -1, szPassword, 20);
}
if (n == 0)
{
_tprintf( _T("Failed to convert password to unicode\n"));
return -1;
}
}
#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 -1;
} // 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
if ( !LogonUser(
argv[1], // user name
_T("."), // domain or server
(argc == 2) ? argv[1]:argv[2], // password
LOGON32_LOGON_NETWORK, // type of logon operation
LOGON32_PROVIDER_DEFAULT, // logon provider
&hToken ) ) { // pointer to token handle
_tprintf( _T("LogonUser() failed. Error %d\n"), GetLastError() );
return -1;
} // 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 = argv[1];
#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 -1;
} // 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 -1;
} // Retrieve the new user's profile directory
if ( !GetUserProfileDirectory( hToken, szProfilePath, &cchPath ) ) {
_tprintf( _T("GetProfilePath() failed. Error %d\n"),
GetLastError() );
return -1;
} // 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 );
} return 0;
}
The sample code below can be compiled as a console application for Windows NT 4.0. This code demonstrates how to programmatically create a new user account, force a profile to be created for the new user, and retrieve the new profile directory: //**********************************************************************
//
// This program creates a new user account, forces a profile to be
// created for the new user, and retrieves the new profile directory
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (C) 1998 Microsoft Corporation. All rights reserved.
// Author: Jonathan Russ (jruss)
//
//********************************************************************** // NOTE: This code must be linked with netapi32.lib #include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <lm.h> // Declarations based on USERENV.H for Windows 2000 Beta 2
#define PI_NOUI 0x00000001 // Prevents displaying of messages
#define PI_APPLYPOLICY 0x00000002 // Apply NT4 style policy typedef struct _PROFILEINFO {
DWORD dwSize; // Must be set to sizeof(PROFILEINFO)
DWORD dwFlags; // See flags above
LPTSTR lpUserName; // User name (required)
LPTSTR lpProfilePath; // Roaming profile path
LPTSTR lpDefaultPath; // Default user profile path
LPTSTR lpServerName; // Validating DC name in netbios format
LPTSTR lpPolicyPath; // Path to the NT4 style policy file
HANDLE hProfile; // Registry key handle - filled by function
} PROFILEINFO, FAR * LPPROFILEINFO; // Typedefs for function pointers in USERENV.DLL
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNLOADUSERPROFILE) (
HANDLE hToken,
LPPROFILEINFO lpProfileInfo
); typedef BOOL (STDMETHODCALLTYPE FAR * LPFNUNLOADUSERPROFILE) (
HANDLE hToken,
HANDLE hProfile
); typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
HANDLE hToken,
LPTSTR lpProfileDir,
LPDWORD lpcchSize
); HMODULE g_hUserEnvLib = NULL;
LPFNLOADUSERPROFILE LoadUserProfile = NULL;
LPFNUNLOADUSERPROFILE UnloadUserProfile = NULL;
LPFNGETUSERPROFILEDIR GetUserProfileDirectory = NULL;
//**********************************************************************
//
// FUNCTION: InitUserEnv - This function dynamically links to
// USERENV.DLL and sets up the required function pointers
//
// PARAMETERS: none
//
// RETURN VALUE: TRUE if successful. Otherwise, FALSE.
//
//********************************************************************** BOOL InitUserEnv( void ) { 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;
}
//**********************************************************************
//
// FUNCTION: _tmain - This is the entry point for the program.
//
// PARAMETERS: argc - the number of command-line arguments
// argv - an array of null-terminated strings specifying
// the command-line arguments
// envp - an array of null-terminated strings specifying
// the environment strings
//
// RETURN VALUE: Zero if successful. Otherwise, non-zero.
//
//********************************************************************** #ifdef __cplusplus
extern "C"
#endif #ifdef UNICODE
int _cdecl
#else
int
#endif _tmain(int argc, _TCHAR **argv, _TCHAR **envp) { USER_INFO_1 ui1;
DWORD dwError;
HANDLE hToken;
PROFILEINFO pi;
TCHAR szProfilePath[1024];
DWORD cchPath = 1024;
WCHAR szUserName[20];
WCHAR szPassword[20]; // Check for the required command-line arguments
if (argc < 2) {
_tprintf( _T("Usage: AddUser <user> [password]\n") );
return -1;
} // Set USERENV.DLL function pointers
if ( !InitUserEnv() ) {
_tprintf( _T("Failed to set USERENV.DLL function pointers.\n") );
return -1;
} // 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, argv[1], -1, szUserName, 20);
if (n == 0)
{
_tprintf( _T("Failed to convert username to unicode\n"));
return -1;
} if ( argc == 2 ) {
n = MultiByteToWideChar(0, 0, argv[1], -1, szPassword, 20);
} else {
n = MultiByteToWideChar(0, 0, argv[2], -1, szPassword, 20);
}
if (n == 0)
{
_tprintf( _T("Failed to convert password to unicode\n"));
return -1;
}
}
#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 -1;
} // 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
if ( !LogonUser(
argv[1], // user name
_T("."), // domain or server
(argc == 2) ? argv[1]:argv[2], // password
LOGON32_LOGON_NETWORK, // type of logon operation
LOGON32_PROVIDER_DEFAULT, // logon provider
&hToken ) ) { // pointer to token handle
_tprintf( _T("LogonUser() failed. Error %d\n"), GetLastError() );
return -1;
} // 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 = argv[1];
#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 -1;
} // 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 -1;
} // Retrieve the new user's profile directory
if ( !GetUserProfileDirectory( hToken, szProfilePath, &cchPath ) ) {
_tprintf( _T("GetProfilePath() failed. Error %d\n"),
GetLastError() );
return -1;
} // 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 );
} return 0;
}
解决方案 »
- ListCtrl如何可以显示多个Header?
- 采用VC++ 6.0 如何实现对目录(目录中有文件)的复制、命名、删除操作?
- 关于多窗口之间的坐标转换
- MFC进度条与微调器用法举例
- SetScrollSizes里面的sizeTota最大值是多少啊
- 如何取得一段代码的执行时间?
- vc中的工具栏怎么老是自动跑到左边叠起来?
- 奇怪的问题!
- 求助哪里可以下载windows CE toolkits for visual C++6.0
- 在VC下,如何通过DwgDirect打开一个Dwg文件?读取文件中的信息。。
- 只加了这一句,m_pMainWnd = &dlg;结果就不一样,
- 类的公有和私有成员函数的区别是什么?(请老兄们给小弟详细的讲讲)
LPCTSTR lpLibFileName // address of filename of executable module
); Parameters
lpLibFileName
Points to a null-terminated string that names the executable module (either a .DLL or .EXE file). The name specified is the filename of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.DEF) file.
If the string specifies a path but the file does not exist in the specified directory, the function fails.
If a path is not specified and the filename extension is omitted, the default library extension .DLL is appended. However, the filename string can include a trailing point character (.) to indicate that the module name has no extension. When no path is specified, the function searches for the file in the following sequence: 1. The directory from which the application loaded.
2. The current directory.
3. Windows 95: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.Windows NT: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is SYSTEM32. 4. Windows NT: The 16-bit Windows system directory. There is no Win32 function that obtains the path of this directory, but it is searched. The name of this directory is SYSTEM.
5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6. The directories that are listed in the PATH environment variable. The first directory searched is the one directory containing the image file used to create the calling process (for more information, see the CreateProcess function). Doing this allows private dynamic-link library (DLL) files associated with a process to be found without adding the process's installed directory to the PATH environment variable.
Once the function obtains a fully qualified path to a library module file, the path is compared (case independently) to the full paths of library modules currently loaded into the calling process. These libraries include those loaded when the process was starting up as well as those previously loaded by LoadLibrary but not unloaded by FreeLibrary. If the path matches the path of an already loaded module, the function just increments the reference count for the module and returns the module handle for that library. Return ValuesIf the function succeeds, the return value is a handle to the module.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
hDll=LoadLibrary("xxxx.Dll");
if(hDll==NULL)//不成功
{
。。
}
hmod = LoadLibrary(_T("xxxx.dll"));//...do something...FreeLibrary(hmod);
如果用我们手工的方式去加载动态库,而不使用LoadLibary()这个函数,我们该如何处理?因为我们的工程中需要将原始动态库加密,之后再用手工方式加载。所以,我需要理解动态库的加载的原理,包括映射地址空间,如何初始化等等
望各路大侠的热情帮助!!!!