以指定用户权限运行一个进程
2013-04-19 22:39
495 查看
转自http://blog.csdn.net/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;
}
//以指定用户权限运行一个进程
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;
}
相关文章推荐
- 以指定用户权限运行一个进程
- :让一个运行在SYSTEM权限下的进程与当前用户的桌面进行交互 代码:
- 转--让一个运行在SYSTEM权限下的进程与当前用户的桌面进行交互
- Linux上创建一个用户,并给该用户指定目录的权限,修改时区
- WIN32API函数CreateProcess创建一个新的进程和它的主线程,新进程运行指定的可执行文件
- 如何将所有的进程运行在一个指定的CPU上
- 《Windows核心编程》学习笔记(9)– 在win7或者vista系统下提升一个进程的运行权限
- QT单实例应用,使用QLocalSocket进行进程通信,不同运行权限下运行的同一个进程通讯失败,报错 “QLocalSocket::SocketAccessError”
- linux 下如何给一个普通用户有在指定目录下有创建文件的权限
- 如何将所有的进程运行在一个指定的CPU上
- 用户是否是用管理员权限运行本进程
- 如何将所有的进程运行在一个指定的CPU上
- Delphi CreateProcess WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程运行指定的可执行文件
- mysql 创建一个用户赋予指定权限
- VC 判断进程是否是以管理员权限运行,并且判断是否是用户进程而非服务进程
- 《Windows核心编程》学习笔记(9)– 在win7或者vista系统下提升一个进程的运行权限
- 子进程 保护地址空间 运行独立的子进程 管理员以标准用户权限运行时
- 如何将所有的进程运行在一个指定的CPU上
- System.UnauthorizedAccessException: 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“NT AUTHORITY\NETWORK SERVICE”没有访问 temp 目录的足够权限。CodeDom 将使用进程正在使用的用户帐户进行编译,这样,如
- System.UnauthorizedAccessException: 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“NT AUTHORITY\NETWORK SERVICE”没有访问 temp 目录的足够权限。CodeDom 将使用进程正在使用的用户帐户进行编译,这样,如