c# – Embed form in command prompt (cmd.exe)

Question: Question:

environment
OS: Windows7 (64Bit)
VisualBasic (.NetFrameWork4.0)

Nice to meet you, I'm stuck on the subject, can you help me?

I'm trying to create a windows form application in Visual Basic and run a DOS window inside a form.

There is some information on the web and I tried to implement it for reference, but it does not work as expected.

Reference URL
http://geekswithblogs.net/gyoung/archive/2006/04/26/76521.aspx

On the sample program, if the program to be executed is Notepad (notepad.exe), it will operate as an MDI child form, but if it is a command prompt or calculator, the application will be executed, but it will start up independently instead of the child form. increase.

Does anyone know a good solution?

Thank you.

Answer: Answer:

In that sample, WaitForInputIdle() waits for the window to be created, but depending on the implementation of the target program, this wait may end before the window is created. calc and cmd seem to fall into this case, and he can get the handle properly if he waits for about 1 second and then gets it.

var proc = Process.Start("calc");
proc.WaitForInputIdle();

// 取得できてなければもう少し待ってみる
while(proc.MainWindowHandle == IntPtr.Zero)
    Thread.Sleep(500);

Console.WriteLine(proc.MainWindowHandle);

I wrote the above example briefly, but I think it is necessary to consider the case where a timeout is set in case the acquisition still fails, or the target program ends while waiting.

Reference What to do if you cannot get the handle of the main window of the process started by Process.Start (): Slotware Blog

Scroll to Top