公司的电脑全部用域进行管理,不相同的域用户有不同的权限,有时在一些用户权限下要以另一个用户的运行一个进程,这时就有了现在这 个应用场景,解决方法如下: //以指定用户权限运行一个进程 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; }