转自http://blog.youkuaiyun.com/panpanloveruth/article/details/5728999
//以指定用户权限运行一个进程
BOOL CRunAsDlg::RunAsEx(LPWSTR lpCommandLine){
BOOL bRet = FALSE;
HINSTANCE hInst;//advapi32.dll句柄
hInst = ::LoadLibrary("advapi32.dll");
if(hInst)
{
typedef BOOL(WINAPI*MyCreateProcessWithLogonW)(LPCWSTR,LPCWSTR,LPCWSTR,DWORD,LPCWSTR,LPWSTR,DWORD,LPVOID,LPCWSTR,LPSTARTUPINFO,LPPROCESS_INFORMATION);
MyCreateProcessWithLogonW pAdd=(MyCreateProcessWithLogonW)::GetProcAddress(hInst,"CreateProcessWithLogonW");
if(pAdd)
{
// TODO: Place code here.
//LPCWSTR lpUsername = L"Administrator";
LPCWSTR lpUsername = m_sreAdmin.AllocSysString();
// user's name
LPCWSTR lpDomain = L"sc.com";
// user's domain
LPCWSTR lpPassword = m_strKey.AllocSysString(); //itAdmin*cg.03
//LPCWSTR lpPassword = L"itAdmin*cg.03"; //itAdmin*cg.03
// user's password
DWORD dwLogonFlags = 0x00000001;//LOGON_NETCREDENTIALS_ONLY;LOGON_WITH_PROFILE,
// logon option
LPCWSTR lpApplicationName = NULL;
// LPWSTR lpCommandLine = L"E://DWoW//Debug//DWoW.exe";
// command-line string
DWORD dwCreationFlags = 0x00000400;//CREATE_NEW_CONSOLE;
// creation flags
LPVOID lpEnvironment = NULL;
// new environment block
LPCWSTR lpCurrentDirectory = NULL;
// current directory name
STARTUPINFO StartupInfo;
// startup information
PROCESS_INFORMATION ProcessInfo;
// process information BOOL ret;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
//AfxMessageBox((CString)lpPassword);
bRet = pAdd( lpUsername,
lpDomain,
lpPassword,
dwLogonFlags,
lpCommandLine,
lpCommandLine,
dwCreationFlags,
lpEnvironment,
lpCurrentDirectory,
&StartupInfo,
&ProcessInfo );
}
}
return bRet;
}