Solved

Need help on CreateProcess,TerminateProcess APIs or Shell

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 127
VB6 - Compare and highlight cell not the same 3 56
TT Auto Dashboard 13 100
which modules are active in VB6 project? 6 51
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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