CreateProcessWithLogonW

Creates a new process and its primary thread. Then the new process runs the specified executable file in the security context of the specified credentials (user, domain, and password). It can optionally load the user profile for a specified user.

The CreateProcessWithLogonW and CreateProcessWithTokenW functions are similar to the CreateProcessAsUser function, except that the caller does not need to call the LogonUser function to authenticate the user and get a token.

--------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;


namespace MainTest
{
    class ProcessUserLogonHelper
    {
        /// <summary>
        /// 在Main函数中调用该方法的的示例代码
        /// </summary>
        private void InvokeExample()
        {
            ProcessUserLogonHelper phelper = new ProcessUserLogonHelper();
            Process pro = phelper.CreateProcessWithUserToken(@"C:\Program Files\Internet Explorer\iexplore.exe", "bestreme.com", "chdwu", "************");
            pro.Start();
        }

        /// <summary>
        /// 使用指定帐户绑定进程
        /// </summary>
        /// <param name="appPath">进程的宿主程序</param>
        /// <param name="domain">指定帐户的域名</param>
        /// <param name="userName">指定帐户的用户名</param>
        /// <param name="password">指定帐户的密码</param>
        /// <returns>绑定了特定帐户的进程</returns>
        public Process CreateProcessWithUserToken(string appPath,string domain, string userName, string password)
        {
            Process pro = new Process();
            ProcessStartInfo processInfo = new ProcessStartInfo(appPath);
            processInfo.UseShellExecute = false;
            processInfo.UserName = userName;
            processInfo.Domain = domain;
            System.Security.SecureString psw = new System.Security.SecureString();
            foreach (char c in password.ToCharArray())
            {
                psw.AppendChar(c);
            }
            processInfo.Password = psw;
            processInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(appPath);
            pro.StartInfo = processInfo;
            return pro;
        }

        /// <summary>
        /// 使用指定帐户绑定进程
        /// </summary>
        /// <param name="appPath">进程的宿主程序</param>
        /// <param name="userName">指定本地帐户的用户名</param>
        /// <param name="password">指定帐户的密码</param>
        /// <returns>绑定了特定帐户的进程</returns>
        public Process CreateProcessWithUserToken(string appPath, string userName, string password)
        {
            return CreateProcessWithUserToken(appPath, "", userName, password);
        }
    }
}

---------------

            MainTest.ProcessUserLogonHelper cmd = new MainTest.ProcessUserLogonHelper();
            Process p = cmd.CreateProcessWithUserToken("C:\\Program Files\\Internet Explorer\\iexplore.exe",
                "administrator", "clq1111");

            p.Start();

            if (p==null)
            {
                MessageBox.Show("error");
            }

posted on 2012-01-19 15:34 clq 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/-clq/archive/2012/01/19/2327333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值