Jan 11th, 2010, 10:28 AM
#1
Thread Starter
Addicted Member
[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.
Jan 11th, 2010, 07:03 PM
#2
Thread Starter
Addicted Member
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.
Jan 11th, 2010, 07:07 PM
#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:
Imports System.Runtime.InteropServices
Class testme
* * <DllImport("user32.dll")> _
* * Public Shared Function FindWindow(ByVal strClassName As String, ByVal strWindowName As String) As Integer
* * End Function
* * <DllImport("user32.dll")> _
* * Public Shared Function SetParent(ByVal child As Integer, ByVal parent As Integer) As Integer
* * End Function
* *
* * Private Sub somemethod()
* * * * Dim m_oPPT As PowerPoint.Application = Nothing
* * * * Dim temp As PowerPoint.Application = Nothing
* * * * temp = New PowerPoint.Application()
* * * * m_oPPT = New PowerPoint.Application()
* * * * temp.Quit()
* * * * temp = Nothing
* * * * ' Get window handle, for use with API functions:
* * * * m_oPPT.Caption = "besuretofindthisinstance"
* * * * hWnd = FindWindow("PP10FrameClass", m_oPPT.Caption)
* * * * SetParent(hWndPPT, frmMain.Handle.ToInt32())
* * End Sub
End Class
Jan 12th, 2010, 04:56 PM
#4
Thread Starter
Addicted Member
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.
Jan 12th, 2010, 06:44 PM
#5
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
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 13th, 2010, 05:35 PM
#6
Thread Starter
Addicted Member
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.
Jan 13th, 2010, 05:41 PM
#7
Re: How to start a program as a child to my program
vb Code:
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:
Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As integer, ByVal hWndNewParent As integer) As integer
or:
vb Code:
Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As intptr, ByVal hWndNewParent As intptr) As integer
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 13th, 2010, 08:04 PM
#8
Thread Starter
Addicted Member
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.
Jan 13th, 2010, 08:11 PM
#9
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
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 13th, 2010, 08:29 PM
#10
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:
Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
vb Code:
Dim pr As New Process
pr.StartInfo.FileName = "calc.exe"
pr.Start()
pr.WaitForInputIdle()
SetParent(pr.MainWindowHandle, Me.Handle)
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 14th, 2010, 12:37 PM
#11
Thread Starter
Addicted Member
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.
Jan 14th, 2010, 12:49 PM
#12
Thread Starter
Addicted Member
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.
Jan 14th, 2010, 05:57 PM
#13
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
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 14th, 2010, 11:10 PM
#14
Thread Starter
Addicted Member
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.
Jan 15th, 2010, 12:25 AM
#15
Re: [RESOLVED] How to start a program as a child to my program
at form level:
vb Code:
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:
For x As Integer = 1 To 10
processes.Add(New Process)
processes(processes.Count - 1).StartInfo.FileName = "calc.exe"
processes(processes.Count - 1).Start()
Threading.Thread.Sleep(500)
Next
'enable the timer.
'after 15 seconds the timer will close the calculator's 1 by 1
Timer1.Enabled = True
here's the timer_tick event:
vb Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
For x As Integer = 1 To 10
processes(0).Kill()
processes.RemoveAt(0)
Threading.Thread.Sleep(500)
Next
Timer1.Enabled = False
End Sub
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 15th, 2010, 12:38 AM
#16
Re: [RESOLVED] How to start a program as a child to my program
Originally Posted by
ashraf fawzy
I'm using 32 bit windows version (X86).
thats why the API runs. "user32" functions are 32 bit
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 15th, 2010, 02:24 AM
#17
Thread Starter
Addicted Member
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.
Jan 15th, 2010, 08:31 AM
#18
Re: [RESOLVED] How to start a program as a child to my program
a reverse loop will avoid the index error:
vb Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
For x As Integer = processes.Count - 1 To 0 Step -1
If Not processes(x).HasExited Then
processes(x).Kill()
End If
processes.RemoveAt(x)
Next
End Sub
Coding Examples:
Features:
Online Games:
Compiled Games:
Jan 17th, 2010, 05:25 PM
#19
Thread Starter
Addicted Member
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
Some times when you make a step forward ...You can't get back. Be sure where you make your steps.
Jan 17th, 2010, 05:44 PM
#20
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
Coding Examples:
Features:
Online Games:
Compiled Games:
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