控制台应用程序启动.exe作为当前用户

时间:2018-02-16 12:58:38

标签: c# jenkins console-application

我正在使用trance32,并希望通过Jenkins启动此过程。 当我启动T32mppc.exe时,它作为系统进程运行,因为它在后台运行。 我想与当前用户一起启动此过程,以便在不插入用户名和密码的情况下在前台查看。 我的系统只有两种过程。系统和用户。没有其他用户在那里。

附上我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO;
using System.Globalization;
using System.Xml.Linq;
using System.Diagnostics;
using System.Threading;

using System.Configuration;

namespace Console_Startapps
{
    class Program
    {

    [System.Runtime.InteropServices.DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(IntPtr handle);

    [System.Runtime.InteropServices.DllImport("User32.dll")]
    private static extern bool ShowWindow(IntPtr handle, int nCmdShow);

    [System.Runtime.InteropServices.DllImport("User32.dll")]
    private static extern bool IsIconic(IntPtr handle);

    [DllImport("User32.dll", SetLastError = true)]
    static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);


    [DllImport("TRACE32\t32api64.dll")]
    public static extern int T32_ResetCPU();

    private void startT32app()
    {
        IntPtr handle;
        try
        {
            Console.WriteLine("T32 launching");

            string path = @"C:\T32\bin\windows64\t32mppc.exe";
            string args = @"C:\T32\config.t32";
            ProcessStartInfo procInfo = new ProcessStartInfo(path, args);
            procInfo.CreateNoWindow = false;
            procInfo.UseShellExecute = true;
            procInfo.WindowStyle = ProcessWindowStyle.Normal;

            Process[] targetProcess = Process.GetProcessesByName("t32mppc.exe");
            //if (targetProcess.Length > 1)
            if (this.IsProcessOpen("t32mppc") == 1)
            {
                Console.WriteLine("TC32 already running");
            }
            else
            {
                Process procRun = Process.Start(procInfo);
                handle = procRun.MainWindowHandle;
                SwitchToThisWindow(handle, true);
            }

            //SetForegroundWindow(handle);
        }
        catch
        {
            Console.WriteLine("Failed to launch T32");
        }
    }

    private int IsProcessOpen(string name)
    {
        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (clsProcess.ProcessName.ToLower().Contains(name.ToLower()))
            {
                return 1;
            }
        }
        return 0;
    }

    static void Main(string[] args)
    { 

        Program Beginapps = new Program();
        Beginapps.startT32app();

    }
}  

}

1 个答案:

答案 0 :(得分:0)

您是否曾尝试冒充其他用户? https://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.110).aspx

基本上,您可以使用给定的凭据运行另一个进程 How do you do Impersonation in .NET?