zoukankan      html  css  js  c++  java
  • 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 FilesInternet Exploreriexplore.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");
                }

    http://www.cnblogs.com/-clq/archive/2012/01/19/2327333.html

  • 相关阅读:
    SharePoint每日小贴士Web部件
    韦东山设备树课程-环境搭建【学习笔记】
    韦东山视频第3课第2节_JNI_C调用JAVA_P【学习笔记】
    韦东山视频第3课第1节_JNI_P【学习笔记】
    高通qxdm抓取sensor的log【学习笔记】
    sensor【学习笔记】
    linux驱动由浅入深系列:高通sensor架构实例分析之二(驱动代码结构)【转】
    linux驱动由浅入深系列:高通sensor架构实例分析之三(adsp上报数据详解、校准流程详解)【转】
    Android Sensor 架构深入剖析【转】
    Android Sensor详解(1)简介与架构【转】
  • 原文地址:https://www.cnblogs.com/findumars/p/5812183.html
Copyright © 2011-2022 走看看