Solved

CreateProcess and TerminateProcess API calls

Posted on 1998-01-20
10
565 Views
Last Modified: 2008-02-01
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!
0
Comment
Question by:schworak
10 Comments
 
LVL 2

Expert Comment

by:PedroMVGomes
ID: 1452469
Do u have Dan apleman's guide to the win api 32...chap 14 has what you need!
0
 
LVL 2

Expert Comment

by:PedroMVGomes
ID: 1452470
if you don't have it..i can send the code to you by e-mail!
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1452471
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
 
LVL 3

Author Comment

by:schworak
ID: 1452472
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
 
LVL 3

Author Comment

by:schworak
ID: 1452473
I don't have that guide book. Could you send me the sample code? (findglenn@g-world.com) Thanks!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:PedroMVGomes
ID: 1452474
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
 
LVL 3

Author Comment

by:schworak
ID: 1452475
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
 
LVL 3

Author Comment

by:schworak
ID: 1452476
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
 
LVL 2

Accepted Solution

by:
PedroMVGomes earned 80 total points
ID: 1452477
Great i could help!!! ... now u can award me the points
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1452478
Bought This Question.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now