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
2009-08-14 12:29:42
"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
2009-08-14 12:34:41
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
2009-08-14 12:35:35
@Thomas: Sorry, didn't see your comment before. Of course, iterating is far more inefficient. I didn't remember the GetCurrentProcess() method...
Thorsten Dittmar
2009-08-14 12:36:32
+2
A:
Not sure it works, but you can try that :
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
Thomas Levesque
2009-08-14 12:33:36
Did this work for you Grant? Just curious, because I could use this in a couple places too :)
Pwninstein
2009-08-14 12:39:25