Solved

Need help on CreateProcess,TerminateProcess APIs or Shell

Posted on 2004-08-11
1
204 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now