process.MainWindowHandle returning 0

G

Guest

I am trying to start a cmd process and manipulate its main window thereafter.
(.NET 2.0 - c# code - see below)

Sometimes I however get a process.MainWindowHandle == 0 from the process,
despite a nicely started fresh process main window being visible. The process
started ok and is running fine (an openssh client accessing another host).

When using WINAPI calls - basically searching all windows until a match of
process.id occurs - I can get to the proper handle.

Now I know (msdn doc says) if a process window is hidden 0 is normal, but in
my case the process is started with a visible window...

I have a fix for now but it cost me quite some hours to get there. As google
etc did not help I though maybe someone else can add some ideas and we can
all benefit next time...

Thanks ahead and best regards

tb


string arg = "/c ssh " + HostName + @" c:/mypath/my.exe -h";
ProcessStartInfo psi = new ProcessStartInfo("cmd", arg);
psi.WindowStyle = ProcessWindowStyle.Normal;
Process p = Process.Start(psi);

//often == 0
Console.WriteLine("Try via Process:" + p.MainWindowHandle);

//always ok
Console.WriteLine("Try via WIN32: " + WIN32.GetMainProcessWindow(p.Id));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top