CreateProcess and TerminateProcess API calls

Could someone show me a simple example of starting then terminating a progream using the CreateProcess and TerminateProcess API calls? I have no idea on how to get them working properly.

Something simple would be greate. Like two click buttons. One to start the calculator (or what ever) and the other to terminate that process.

Thanks!
LVL 3
schworakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PedroMVGomesCommented:
Do u have Dan apleman's guide to the win api 32...chap 14 has what you need!
0
PedroMVGomesCommented:
if you don't have it..i can send the code to you by e-mail!
0
MikeP090797Commented:
Why do you need CreateProccess for that, you can use the Shell function. To terminate the calculator, you can use something like:

SendMessage(FindWindow("", Calculator), WM_CLOSE, 0, 0)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

schworakAuthor Commented:
PedroMVGomes, I don't have that guide. Could you EMAil it to me? My address is findglenn@g-world.com and I would really appreciate it.


MikeP, I don't need it for calculator really. That was a simple example. Find window won't work if the process that is started doesn't open a window.
0
schworakAuthor Commented:
I don't have that guide book. Could you send me the sample code? (findglenn@g-world.com) Thanks!
0
PedroMVGomesCommented:
sorry i don't know why whasn't i informed of your comments, i will send the samples of chap 14 to u rith now
0
schworakAuthor Commented:
For some reason, the createprocess doesn't seem to work quite right. I pass paramaters and nothing happens. But I did get most of the info I needed. Post as an answer so you can get your points.

0
schworakAuthor Commented:
AH HA!!!! I got it figured out. Thanks for sending me this sample. I now have my project working.


VERSION 5#
Begin VB.Form frmLaunch
   Caption = "Launch Shelled1.exe"
   ClientHeight = 2880
   ClientLeft = 1095
   ClientTop = 1515
   ClientWidth = 4140
   LinkTopic = "Form1"
   PaletteMode = 1        'UseZOrder
   ScaleHeight = 2880
   ScaleWidth = 4140
   Begin VB.CommandButton cmdShell2
      Caption = "Shell with Callback"
      Height = 495
      Left = 240
      TabIndex = 6
      Top = 1560
      Width = 1755
   End
   Begin VB.CommandButton cmdShellExecute
      Caption = "Using ShellExecute"
      Height = 495
      Left = 240
      TabIndex = 3
      Top = 2220
      Width = 1755
   End
   Begin VB.CommandButton cmdCreateProcess
      Caption = "Using CreateProcess"
      Height = 495
      Left = 240
      TabIndex = 2
      Top = 900
      Width = 1755
   End
   Begin VB.CommandButton cmdShell
      Caption = "Using Shell"
      Height = 495
      Left = 240
      TabIndex = 0
      Top = 240
      Width = 1755
   End
   Begin VB.Label lblStatus
      Height = 255
      Index = 2
      Left = 2100
      TabIndex = 5
      Top = 1680
      Width = 1875
   End
   Begin VB.Label lblStatus
      Height = 255
      Index = 1
      Left = 2100
      TabIndex = 4
      Top = 1020
      Width = 1875
   End
   Begin VB.Label lblStatus
      Height = 255
      Index = 0
      Left = 2100
      TabIndex = 1
      Top = 300
      Width = 1875
   End
End




Option Explicit
' Copyright © 1997 by Desaware Inc. All Rights Reserved

Dim DemoDirectory$
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF       '  Infinite timeout
Private Const DEBUG_PROCESS = &H1
Private Const DEBUG_ONLY_THIS_PROCESS = &H2

Private Const CREATE_SUSPENDED = &H4

Private Const DETACHED_PROCESS = &H8

Private Const CREATE_NEW_CONSOLE = &H10

Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const IDLE_PRIORITY_CLASS = &H40
Private Const HIGH_PRIORITY_CLASS = &H80
Private Const REALTIME_PRIORITY_CLASS = &H100

Private Const CREATE_NEW_PROCESS_GROUP = &H200

Private Const CREATE_NO_WINDOW = &H8000000

Private Const WAIT_FAILED = -1&
Private Const WAIT_OBJECT_0 = 0
Private Const WAIT_ABANDONED = &H80&
Private Const WAIT_ABANDONED_0 = &H80&
Private Const WAIT_TIMEOUT = &H102&

Private Const SW_SHOW = 5

Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type

Private Type STARTUPINFO
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
End Type

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessBynum Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long



Private Sub cmdCreateProcess_Click()
    Dim res&
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    sinfo.cb = Len(sinfo)
    sinfo.lpReserved = vbNullString
    sinfo.lpDesktop = vbNullString
    sinfo.lpTitle = vbNullString
    sinfo.dwFlags = 0
   
    lblStatus(1).Caption = "Launching"
    lblStatus(1).Refresh
    res = CreateProcessBynum(DemoDirectory & "Shelled1.exe", vbNullString, 0, 0, True, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, sinfo, pinfo)
    If res Then
        lblStatus(1).Caption = "Launched"
        WaitForTerm2 pinfo
    End If
    lblStatus(1).Caption = "Terminated"

End Sub

Private Sub cmdShell_Click()
    Dim pid&
    lblStatus(0).Caption = "Launching"
    lblStatus(0).Refresh
    pid = Shell(DemoDirectory & "Shelled1.exe", vbNormalFocus)
    If pid <> 0 Then
        lblStatus(0).Caption = "Launched"
        lblStatus(0).Refresh
        WaitForTerm1 pid
    End If
    lblStatus(0).Caption = "Terminated"
End Sub

' This wait routine freezes the application
' It's clearly not a good way to wait for process
' termination - though if you hid the application
' first it could be very effective.
Private Sub WaitForTerm1(pid&)
    Dim phnd&
    phnd = OpenProcess(SYNCHRONIZE, 0, pid)
    If phnd <> 0 Then
        lblStatus(0).Caption = "Waiting for termination"
        lblStatus(0).Refresh
        Call WaitForSingleObject(phnd, INFINITE)
        Call CloseHandle(phnd)
    End If
End Sub

' This wait routine allows other application events
' to be processed while waiting for the process to
' complete.
Private Sub WaitForTerm2(pinfo As PROCESS_INFORMATION)
    Dim res&
    ' Let the process initialize
    Call WaitForInputIdle(pinfo.hProcess, INFINITE)
    ' We don't need the thread handle
    Call CloseHandle(pinfo.hThread)
    ' Disable the button to prevent reentrancy
    cmdCreateProcess.Enabled = False
    lblStatus(1).Caption = "Waiting for termination"
    lblStatus(1).Refresh
    Do
        res = WaitForSingleObject(pinfo.hProcess, 0)
        If res <> WAIT_TIMEOUT Then
            ' No timeout, app is terminated
            Exit Do
        End If
        DoEvents
    Loop While True
   
    cmdCreateProcess.Enabled = True
    ' Kill the last handle of the process
    Call CloseHandle(pinfo.hProcess)
End Sub

Private Sub cmdShell2_Click()
    Dim pid&
    Dim obj As Object
    lblStatus(2).Caption = "Launching"
    lblStatus(2).Refresh
    pid = Shell(DemoDirectory & "Shelled1.exe", vbNormalFocus)
    If pid <> 0 Then
        Set obj = CreateObject("dwWatcher.dwAppWatch")
        obj.SetAppWatch pid
        obj.SetAppCallback Me
        lblStatus(2).Caption = "Waiting for termination"
        cmdShell2.Enabled = False
    End If
End Sub

Private Sub cmdShellExecute_Click()
    Dim res&
    Dim obj As Object
    res& = ShellExecute(hwnd, "open", DemoDirectory & "Shelled1.exe", vbNullString, CurDir$, SW_SHOW)
    If res < 32 Then
        MsgBox "Unable to shell applicatin"
    End If
End Sub

Public Sub dwAppTerminated(obj As Object)
    lblStatus(2).Caption = "Terminated"
    cmdShell2.Enabled = True
End Sub

Private Sub Form_Load()
    DemoDirectory = InputBox$("Enter path of directory containing Shelled1.exe", , "d:\zdbook3\sourcev5\ch14\")
End Sub

0
PedroMVGomesCommented:
Great i could help!!! ... now u can award me the points
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MirkwoodCommented:
Bought This Question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.