|
-
Jan 29th, 2009, 02:08 PM
#1
Thread Starter
Junior Member
Embedding exe: solving the WaitForInputIdle exception
Hi,
Embedding exe's in a form, into a Panel, for example, is fine, for such apps as notepad.exe, calc.exe, etc, but NOT for cmd.exe which throws an exception, because it is not a UI. This code demonstrates:
Form with one button and one panel:
Code:
Public Class Form1
Private Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim proc As Process
proc = Process.Start("notepad.exe")'also works with calc.exe but NOT with cmd.exe
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
End Sub
End Class
It is the WaitForInputIdle that is causing the exception, because it only works on exe files with a message loop, which cmd.exe does not. See the bug report here:
http://msdn.microsoft.com/en-us/libr...e2(VS.85).aspx
Has this problem ever been solved by anyone?
Rock
-
Jan 29th, 2009, 03:46 PM
#2
Re: Embedding exe: solving the WaitForInputIdle exception
What happens if you remove the line "proc.WaitForInputIdle()"?
-
Jan 29th, 2009, 05:16 PM
#3
Thread Starter
Junior Member
Re: Embedding exe: solving the WaitForInputIdle exception
 Originally Posted by dee-u
What happens if you remove the line "proc.WaitForInputIdle()"?
The cmd window opens, but is not bound to the panel, and can be dragged around.
Replacing it with:
System.Threading.Thread.Sleep(10000)
does not work either.
Rock
-
Jan 29th, 2009, 05:23 PM
#4
Re: Embedding exe: solving the WaitForInputIdle exception
-
Jan 29th, 2009, 06:25 PM
#5
Thread Starter
Junior Member
Re: Embedding exe: solving the WaitForInputIdle exception
 Originally Posted by dee-u
How about DoEvents?
Well, replacing the proc.WaitForInputIdle() with Application.DoEvents() does not produce an exception, but the cmd window still is not bound to the Panel.
Rock
-
Jan 29th, 2009, 06:32 PM
#6
Re: Embedding exe: solving the WaitForInputIdle exception
Don't try to embed the console window in your form. Hide the console window and redirect its standard input and output streams. Display a regular TextBox in your form just have it mimic the display of the console window. There's a thread on redirecting console streams in the VB.NET CodeBank forum.
-
Jan 29th, 2009, 07:30 PM
#7
Thread Starter
Junior Member
Re: Embedding exe: solving the WaitForInputIdle exception
 Originally Posted by jmcilhinney
Don't try to embed the console window in your form. Hide the console window and redirect its standard input and output streams. Display a regular TextBox in your form just have it mimic the display of the console window. There's a thread on redirecting console streams in the VB.NET CodeBank forum.
I have been down that road for the last three weeks. It is a blind alley.
I found how to launch a cmd window using a Process, and hide it, and how to redirect its input and ouput.
I found that the business of continuously updating a textbox with the contents of the cmd window is best achieved using a thread (new ground), and then I noticed that the scrollbar was always at the top (by default) when the new stuff was at the bottom, so I learnt how to force the scrollbar to the bottom.
I then realised that the textbox would just get bigger and bigger and bigger, and that MaxLength did not mean the same as 'buffer size' for which there is none for a textbox, so I learnt how to make a rolling buffer in code.
I then realised that a TextBox, or a RichTextBox, did not replicate any colored text that the cmd window might show, so I have been working on parsing the output from the cmd window, looking for certain headers, and trying to code the text so that it does show the same colors as in the cmd window (this a LOT of work).
I (and others) then found that typing junk instead of a recognised cmd command crashes cmd (instead of giving the normal 'unrecognised command' error message).
Long thread about this route is here:
http://social.msdn.microsoft.com/For...d-8538061db76c
With the current project I am working on I just don't have the luxury of another few weeks to get the redirection solution implemented (if it can be solved that is). I, and others working on this project, have not found one fully working solution in any forum or codebank, nor any thread that showed as solved. They all just peter out.
I am not actually using cmd.exe, that is just for testing, my exe file simply runs in a cmd-like window, and it has another major bug that I have not yet had the time to explore - when it starts it produces all sorts of status messages, and then it asks a question and pauses for the reply. At the pause the app crashes with the ubiquitous "Windows has experienced a problem and needs to close" error message.
The embedded cmd window solves all my problems of scrollbar, buffer size, coloured text, junk commands, and crashing when pausing for input.
I have found a working solution to embedding cmd windows into tabbed pages (which is exactly what I am looking for anyway), unfortunately it is in c#, but I am sure it will be much less hassle converting that to VB.NET code than trying to solve all the problems assciated with the redirection route.
http://www.geekpedia.com/tutorial230...API-Calls.html
I know about the thread in the Codebank. Mine are the last two posts there. I got no answer there to either of my posts, nor in the main forum when I transferred it there.
Thanks for the suggestion though,
Rock
Last edited by Rock_Vacirca; Jan 29th, 2009 at 07:39 PM.
-
Jan 29th, 2009, 10:38 PM
#8
Re: Embedding exe: solving the WaitForInputIdle exception
There are sites out there that could translate from C# to VB.Net, SharpDevelop also has such feature.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|