Solved

Need help on CreateProcess,TerminateProcess APIs or Shell

Posted on 2004-08-11
1
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

751 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