windows 普通用户 使用管理员权限运行程序

#include #include #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"); } else MessageBox("成功3"); // // TO DO: change NULL to '.' to use local account database // //使用管理员创建进程,注意如果是加入域的计算机,域名请使用计算机名 if (!CreateProcessWithLogonW(L"username",L".", L"password", LOGON_WITH_PROFILE, NULL, L"c://1.exe", CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfile, &si, &pi)) { MessageBox("失败4"); } MessageBox("成功4"); if (!DestroyEnvironmentBlock(lpvEnv)) MessageBox("失败5"); CloseHandle(hToken); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); 

 

获取计算机名

http://blog.csdn.net/twtzw/archive/2010/04/25/5528039.aspx

userenv.h userenv.lib userenv.dll下载

http://download.csdn.net/source/2296880

你可能感兴趣的:(C/C++)