0

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

    }
}  

}

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
techy
  • 63
  • 1
  • 8
  • 1
    The way you import "T32_ResetCPU", you use a filename including a tab character instead of a backslash and t... – NineBerry Feb 16 '18 at 13:02
  • When Jenkins is running as a Windows service, there is no easy way to start an application in an active Windows user session. There might not even be an active user session or there can even be multiple active user sessions at the same time... – NineBerry Feb 16 '18 at 13:03
  • Jenkins is running on my local system and not from server. No other user on Jenkins as well. Only techy and system process are possible. – techy Feb 16 '18 at 13:05
  • I am not talking about Jenkins running on a server, but Jenkins running as a Windows Service. – NineBerry Feb 16 '18 at 13:07
  • The Jenkins is running as system process. – techy Feb 16 '18 at 13:14
  • If by "system process" you mean a Windows service, then the duplicate https://stackoverflow.com/questions/4278373/how-to-start-a-process-from-windows-service-into-currently-logged-in-users-sess is the answer. – NineBerry Feb 16 '18 at 13:26

1 Answers1

0

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?

Bruno Moreira
  • 175
  • 3
  • 15
  • Still it asks for username and password. Is there any way like running a .exe as a non system process ? Also it should not ask for username and password. – techy Feb 16 '18 at 13:23