tags:

views:

1305

answers:

2

can someone tell me how to get the handle of a windows console app in c#? in a winForm app i would normally try this.handle..

+1  A: 

I don't think there is such a thing. The console window is not accessible to the application. You MIGHT try to iterate the process list looking for your own process name. The Process class IIRC contains a property for the program's main window handle, which might be the console window for console applications - which I'm not sure of.

Thorsten Dittmar
"iterate the process list looking for your own process name" => not a very efficient approach... you could find it by the PID, or use } ;)
Thomas Levesque
Whoops - Thomas Levesque's answer is even more elegant. While relying on the same property, he doesn't need to iterate. I forgot that you can access to current process directly...
Thorsten Dittmar
@Thomas: Sorry, didn't see your comment before. Of course, iterating is far more inefficient. I didn't remember the GetCurrentProcess() method...
Thorsten Dittmar
+2  A: 

Not sure it works, but you can try that :

IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
Thomas Levesque
Did this work for you Grant? Just curious, because I could use this in a couple places too :)
Pwninstein