Solved

Need help on CreateProcess,TerminateProcess APIs or Shell

Posted on 2004-08-11
1
210 Views
Last Modified: 2013-12-25
Okay... I've never used the CreateProcess, Terminate API before. But I have read several posts on this subject.

What I am doing is creating a GUI for a exe that runs in the background. So far I can start the exe using shell. But the problem is that when I close the exe from outside of VB I get a memory error.

So I am looking for 1 of 2 things...

1) An exit method that closes what you started using shell() so I can terminate the exe when I close my GUI
2) An alternative method of createing and terminating an exe process.

Thanks!
0
Comment
Question by:really24
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 11775679
>> 1) An exit method that closes what you started using shell() so I can terminate the exe when I close my GUI

The example below will open Notepad via Shell and then closes Notepad when the application is closed.

Idle_Mind

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Const GW_HWNDNEXT = 2
Const WM_CLOSE = &H10

Dim mWnd As Long

Private Sub Form_Load()
    Dim Pid As Long
       
    Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
    If Pid = 0 Then
        MsgBox "Error starting the app"
    Else
        mWnd = InstanceToWnd(Pid)
    End If
End Sub

Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
   
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    Do While test_hwnd <> 0
        If GetParent(test_hwnd) = 0 Then
            test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
            If test_pid = target_pid Then
                InstanceToWnd = test_hwnd
                Exit Do
            End If
        End If
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
End Function

Private Sub Form_Unload(Cancel As Integer)
    PostMessage mWnd, WM_CLOSE, 0, 0
End Sub
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

832 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