Results 1 to 20 of 20

Thread: [RESOLVED] How to start a program as a child to my program

  1. #1

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Resolved [RESOLVED] How to start a program as a child to my program

    Hi everyone ...

    I'd like to know how to stat a program ( Calc.exe , Paint.exe ,etc) and

    retrieve a Handle so I can use SetParent API function to make it a child inside

    my program .


    I'm using VisualBasic.Net 2008 Pro.
    I used to use VisualBasic 6 API , If you have a new way in .Net I'd like to

    hear it and learn it .

    Many thanks in advance.
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  2. #2

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: How to start a program as a child to my program

    I really need your help .... And I'm sure it's something easy for .Net professional.

    Many thanks in advance .
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  3. #3

    Re: How to start a program as a child to my program

    2 Things:
    1. Don't bump your topic within 24 hours. If someone has the answer and is on at the time, it will be answered. Patience is a virtue.

    2. This code example shows how you can set a PowerPoint window to your program, I'm sure you can modify it to suit your needs:
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2. Class testme
    3. * * <DllImport("user32.dll")> _
    4. * * Public Shared Function FindWindow(ByVal strClassName As String, ByVal strWindowName As String) As Integer
    5. * * End Function
    6. * * <DllImport("user32.dll")> _
    7. * * Public Shared Function SetParent(ByVal child As Integer, ByVal parent As Integer) As Integer
    8. * * End Function
    9. * *
    10. * * Private Sub somemethod()
    11. * * * * Dim m_oPPT As PowerPoint.Application = Nothing
    12. * * * * Dim temp As PowerPoint.Application = Nothing
    13. * * * * temp = New PowerPoint.Application()
    14. * * * * m_oPPT = New PowerPoint.Application()
    15. * * * * temp.Quit()
    16. * * * * temp = Nothing
    17. * * * * ' Get window handle, for use with API functions:
    18. * * * * m_oPPT.Caption = "besuretofindthisinstance"
    19. * * * * hWnd = FindWindow("PP10FrameClass", m_oPPT.Caption)
    20. * * * * SetParent(hWndPPT, frmMain.Handle.ToInt32())
    21. * * End Sub
    22. End Class

  4. #4

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: How to start a program as a child to my program

    Many thanks formlesstree for your help , but I want to retrieve the handle that come

    from running the process not based on the caption of its window
    , like we used to retrieve

    the Handle in VB6 using the Shell function .

    Many thanks in advance .
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How to start a program as a child to my program

    dim pr as new process
    pr.startinfo.filename = "paint.exe"
    pr.start

    pr.mainwindowhandle is what you're looking for

  6. #6

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: How to start a program as a child to my program

    Many thanks Paul for your help , and I got another problem with my code :

    Public Class Form1
    Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim pr As New Process
    pr.StartInfo.FileName = "calc.exe"
    pr.Start()

    Dim Re As Long
    Re= SetParent(pr.MainWindowHandle, Me.Handle)

    Me.Text = (Boo.ToString)

    End Sub
    End Class


    That's my code , but I can't set the Calculator program to be inside my form as child

    Although it return the Handle to Calculator wndow , So what's wrong with my code.

    Many thanks in advance.
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How to start a program as a child to my program

    vb Code:
    1. Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

    the long datatype (from vb6 times) which is 32bit
    in vb.net became the integer datatype which is 32bit

    so that declaration should be either:

    vb Code:
    1. Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As integer, ByVal hWndNewParent As integer) As integer

    or:

    vb Code:
    1. Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As intptr, ByVal hWndNewParent As intptr) As integer

  8. #8

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: How to start a program as a child to my program

    Hi Paul ...

    It doesn't work , even after I changed from Long Into Integer , I still can't contain the

    Calculator into my form as a child , and it return a 0 as a handle .


    I'm using vb 2008 , Operating system XP SP3
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How to start a program as a child to my program

    try changing your target cpu to x86

    Project-->Properties-->Compile-->Advanced Compile Options-->Target CPU

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How to start a program as a child to my program

    with your target cpu set to x86, this should work. i can't test it because the SetParent api function doesn't seem to work in win 7. i know that it'd work pre win 7:

    vb Code:
    1. Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer

    vb Code:
    1. Dim pr As New Process
    2. pr.StartInfo.FileName = "calc.exe"
    3. pr.Start()
    4. pr.WaitForInputIdle()
    5.  
    6. SetParent(pr.MainWindowHandle, Me.Handle)

  11. #11

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: How to start a program as a child to my program

    Many thanks Paul for your help , the code is working fine , and I think

    adding "WaitForInputIdle" is the reason , even when I changed the CPU setting back to

    "Any CPU" it's still working.
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  12. #12

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: [RESOLVED] How to start a program as a child to my program

    And by the way ....

    If I wanna terminate the processes I ran as a child into my form -Calculator- when I close

    My program - Form1 - What code do I have to write ???


    I tried Pr.Kill() , but it only terminates the last opened process , And I want it to terminate the whole

    Child processes I opened.

    Many thanks in advance.
    Last edited by Ashraf Alshahawy; Jan 14th, 2010 at 12:54 PM.
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] How to start a program as a child to my program

    declare your processes at form level instead of locally within a procedure. then they will be available throughout the form + you can close them in your formclosing event.

    are you using a 32 bit version of windows? most api functions only work in a 32 bit environment

  14. #14

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: [RESOLVED] How to start a program as a child to my program

    Hi Paul ... I declared the process at form level as you recommened , but I dont know
    what else I should do , Do I make an array at form level to contain the process and
    then at exit use a looping to close them one by one ???

    If it is , can you guide me through the code because I'm rookie at .Net.

    This the code so far

    Public Class Form1
    Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Dim pr As New Process

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    pr.StartInfo.FileName = "calc.exe"
    pr.Start()
    pr.WaitForInputIdle()
    SetParent(pr.MainWindowHandle, Me.Handle)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
    pr.Kill()
    Catch ex As Exception

    End Try


    'Me.Close()
    End Sub
    End Class

    I'm using 32 bit windows version (X86).

    Many thanks in advance.
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] How to start a program as a child to my program

    at form level:

    vb Code:
    1. Dim processes As New List(Of Process)

    heres a simple example that uses a loop to open 10 calculator's using the form level list(of process) above:

    vb Code:
    1. For x As Integer = 1 To 10
    2.     processes.Add(New Process)
    3.     processes(processes.Count - 1).StartInfo.FileName = "calc.exe"
    4.     processes(processes.Count - 1).Start()
    5.     Threading.Thread.Sleep(500)
    6. Next
    7.  
    8. 'enable the timer.
    9. 'after 15 seconds the timer will close the calculator's 1 by 1
    10. Timer1.Enabled = True

    here's the timer_tick event:

    vb Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.     For x As Integer = 1 To 10
    3.         processes(0).Kill()
    4.         processes.RemoveAt(0)
    5.         Threading.Thread.Sleep(500)
    6.     Next
    7.     Timer1.Enabled = False
    8. End Sub

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] How to start a program as a child to my program

    Quote Originally Posted by ashraf fawzy View Post
    I'm using 32 bit windows version (X86).
    thats why the API runs. "user32" functions are 32 bit

  17. #17

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: [RESOLVED] How to start a program as a child to my program

    It's almost done , but can you give the way of (For Each) instead of (For next)

    as it may happen to close some processes manually and there will be an Index error.

    and I tried too hard , but I got confused because of some new features of .Net like

    Dim processes As New List(Of Process)

    and I can't make the the (For Each) right.

    Many thanks for all your help , It's really appreciated.
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] How to start a program as a child to my program

    a reverse loop will avoid the index error:

    vb Code:
    1. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2.     For x As Integer = processes.Count - 1 To 0 Step -1
    3.         If Not processes(x).HasExited Then
    4.             processes(x).Kill()
    5.         End If
    6.         processes.RemoveAt(x)
    7.     Next
    8. End Sub

  19. #19

    Thread Starter
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Thumbs up Re: [RESOLVED] How to start a program as a child to my program

    Hi Paul , Your code is realy working fine , And I can terminate the process
    And I got two other questions :

    How can I add a running process - That has a window - as a child (MediaPlayer - NotePad -etc ) ???
    I loaded the whole system processes with there IDs into Listview but I can't

    retrieve the Handle .



    How can I make my form show the H/V ScrollBars to fit the childs I added ???

    The project in the attachment.
    Attached Files Attached Files
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] How to start a program as a child to my program

    1/ search the forum for findwindow/findwindowex api to get the window handle

    2/ set your forms autoscroll property to true

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width