Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have windows 11 . I have a console application in VS2019(C#).
I am trying to launch calculator exe thru system.Diagnostics.Process.

code snippet:
C#
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\calc.exe");

            //startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.WindowStyle = ProcessWindowStyle.Normal;

            var myProcess = Process.Start(startInfo);

            myProcess.WaitForInputIdle();

            Console.Write("Main window Title : " + myProcess.MainWindowTitle.ToString());


After running above code, calculator exe is getting launched but myProcess.MainWindowTitle returns a empty string. I am not able to understand this fact. Please help.

What I have tried:

I have tried running above code in order to fetch the the value of MainWidowsTitle. But even though the calculator is getting launched, the value of 'MainWindowTitle' is empty string.
Posted
Updated 13-Feb-24 4:52am
v2
Comments
charles henington 13-Feb-24 17:29pm    
From my understanding the process being called has to have a graphical interface, running ffmpeg returns a empty string as well because it has no graphical interface.

C:\Windows\System32\calc.exe is just a stub which starts another process. If you look at your process object it will have exited.

On my windows 10 system calc.exe actually starts the calculator "app" which is
C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.2103.8.0_x64__8wekyb3d8bbwe\Calculator.exe

It cannot be started directly.
 
Share this answer
 
Comments
charles henington 13-Feb-24 17:26pm    
+5 To add to that, the process that is being called has to have a graphical interface as well which is why I'm assumming that it cannot be started directly?
Your issue might be related to the timing between running your code and calling the Title of the window. You need to check for the main window handle before attempting to retrieve the title after the process has entered the idle state.

I have added a method for you called 'GetMainWindowTitle' to check for the main window handle -

C#
using System;
using System.Diagnostics;

class RunandReadMyProgram
{
    static void Main()
    {
        //Create a new process start info...
        ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\calc.exe");

        //Configure your startInfo properties...
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Normal;

        //Start your processing...
        var myProcess = Process.Start(startInfo);

        //Wait for your process to enter the idle state before reading the title...
        myProcess.WaitForInputIdle();

        //Retrieve the main window title once your process is idle...
        string mainWindowTitle = GetMainWindowTitle(myProcess);

        //Display the main window title where you need it...
        Console.WriteLine("Calculator Window Title: " + mainWindowTitle);
    }

    //The method to get the main window title of your process...
    static string GetMainWindowTitle(Process process)
    {
        string title = string.Empty;

        //Check if your process has a main window handle...
        if (process.MainWindowHandle != IntPtr.Zero)
        {
            //Read the title of the main window...
            title = process.MainWindowTitle;
        }

        return title;
    }
}
 
Share this answer
 
Comments
merano99 14-Feb-24 7:46am    
I have tested the code with VS under Win10. The Calc app is started in the background and the string title is empty. The launched app Calculator is located in the directory "C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_11.2311...." and thus corresponds to what Alan N describes. So far, this suggestion does not fulfill the wish to determine the title of the started app.
Andre Oosthuizen 14-Feb-24 12:22pm    
Thanks for the heads-up, worked fine in Win 11, will adjust code to run Win 10 as well. The only way I could research was to make use of shell32 using windows api, then try to find the title string and manipulate that via a window handle... quite involved, I will post an update once I have played with this challenge.

With all respect to Alan's solution, it just pointed to the problem, same as what the OP is experiencing and no solution to overcome this is given. The calculator app can be manipulated but it is quite involved.
merano99 14-Feb-24 13:03pm    
The point that Alan is not offering a solution, but is helping to narrow down the problem is correct. My comment was not meant as criticism. I was interested in whether the code actually behaves differently under Win11 than under Win10.
To find the responsible window handle, I would suggest testing whether you could follow the process hierarchy. The process that you have started yourself is known and you might be able to find the child that starts it. This may be time-critical, as the original process could end prematurely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900