简体   繁体   中英

Console application launch .exe as current user

I am using trance32, and want to launch this process through Jenkins. When I am launching T32mppc.exe, it is running as system process because of which it is running in background. I want to launch this process with current user to see it on foreground without inserting username and password. My system is having only 2 kind of process. System and User. No other users are there.

Attaching my code:

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();

    }
}  

}

Have you tried to impersonate another user? https://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.110).aspx

Basically, you can run another process with a given credential How do you do Impersonation in .NET?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM