Page 1 of 1

Proccess.start (mdi) Help Please

Posted: Fri Jan 04, 2013 7:16 pm
by muttley1968
Hello i am trying to boot an external program inside an MDI as a child form so that i may add handlers to button presses
to suspend the use of them i am using this code
Code: Select all
        Try
            proc = Process.Start(My.Settings.App_Path, String.Format("{0} {1} {2}", "", "12345", My.Settings.Server_Path))
            proc.WaitForInputIdle()
            SetParent(proc.MainWindowHandle, Me.Handle)
            SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
        Catch ex As Exception
        End Try
and it boots it Fine but it is no a child its booting over the top of my program and acting tottaly independent any ideas on a fix please

Re: Proccess.start (mdi) Help Please

Posted: Fri Jan 04, 2013 7:26 pm
by bisnes_niko
Removed old post, here's the new post:

I guess this is what you want to do:
1.png

Heres the full code:
Code: Select all
Public Class Form1
    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 Const WM_SYSCOMMAND As Integer = 274
    Private Const SC_MAXIMIZE As Integer = 61488
    Dim proc As Process

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim form As New Form
        form.MdiParent = Me

        form.Text = "Notepad inside the Form!"

        form.BackColor = Color.Green


        form.Show()

        proc = Process.Start("C:\WINDOWS\notepad.exe")
        proc.WaitForInputIdle()

        SetParent(proc.MainWindowHandle, form.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)


    End Sub
End Class

Re: Proccess.start (mdi) Help Please

Posted: Fri Jan 04, 2013 7:54 pm
by Scottie1972
Add to Code:
Code: Select all
form.TopLevel = false
this allow the new "form" to be added to an existing form.

and BTW,

if you add a MsgBox() to your Tyr Catch block.
you will underswtand more what type of error you atr getting.
Code: Select all
Try

Catch ex As Exception
     MsgBox(ex.ToString)
End Try
on a error or not, the MsgBox() will tell you what is going on.

Re: Proccess.start (mdi) Help Please

Posted: Fri Jan 04, 2013 8:03 pm
by muttley1968
Thank you both but that code give me this error but loads both the program and a blank form system.invalidoperationexception: Process has exitedm, so the requested
information is not avaiable

Re: Proccess.start (mdi) Help Please

Posted: Fri Jan 04, 2013 8:07 pm
by bisnes_niko
muttley1968 wrote:
Thank you both but that code give me this error but loads both the program and a blank form system.invalidoperationexception: Process has exitedm, so the requested
information is not avaiable
#muttley1968 did you try the updated code I posted or the older one? This works fine on notepad.exe, but not working properly on some programs.

EDIT: Actually it works with everything I tested, just edit the WaitForIdle to:
Code: Select all
      Threading.Thread.Sleep(5000) 

Re: Proccess.start (mdi) Help Please

Posted: Fri Jan 04, 2013 9:59 pm
by muttley1968
Same thing is still hapening i am trying to make it start minecraft i dont know if that makes a diffrence but that is what i need to start

Re: Proccess.start (mdi) Help Please

Posted: Sat Jan 05, 2013 12:19 pm
by muttley1968
Sorry for double post but anyone got any ideas cant move on in my project until this part is working :(