以指定用户权限运行一个进程

转自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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值