#include <userenv.h> #include<windows.h> #pragma comment (lib,"userenv.lib") DWORD dwSize; HANDLE hToken; LPVOID lpvEnv; PROCESS_INFORMATION pi = {0}; STARTUPINFOW si = {0}; WCHAR szUserProfile[256] = L""; si.cb = sizeof(STARTUPINFO); // TO DO: change NULL to '.' to use local account database if (!LogonUser("username", ".", "password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hToken))//登陆管理员帐户,第二个参数为域,本地帐户用.表示 { MessageBox("失败1"); } else MessageBox("成功1"); if (!CreateEnvironmentBlock(&lpvEnv, hToken, TRUE)) { MessageBox("失败2"); } else MessageBox("成功2"); dwSize = sizeof(szUserProfile)/sizeof(WCHAR); if (!GetUserProfileDirectoryW(hToken, szUserProfile, &dwSize)) { MessageBox("失败3"); } e