Skip to content

Close Application Using VBA

Featured Replies

This is driving me mad, can anyone help?

I have a batch file that opens a vba script runner, this then opens the application (a terminal 5250 app), this then logs on to our system and does some sendkey stuff to automate a process, the problem i'm having is that at the end i need to terminate the application. I can obtain the pid but can't terminate the app

Here is the vba or the bit you require to get an idea

Sub Main()

Emulation "TN5250"

Dim Row As Integer

Dim Col As Integer

Dim pID As Long

Dim i As Integer

Dim strExe As String

Timeout=60

pID = Shell("C:\Progra~1\NETMANAG.32\TN5250.EXE -t tcpip ukcwasB 23")

Timeout = 20

EMWaitCursor Timeout, 21, 53

Timeout = 60

EMSENDKEY "w703105"

EMSENDKEY "Password"

end sub

Really stuck on this one so any help or thoughts are appreciated

:thumbup:

Mike

Can't you just send it Alt+F4 ? (% + {F4} in SendKeys)

alternatively since you have the pid you could call a kill utility using the pid - www.sysinternals.com has one, or there's one in the Windows Resource Kits. Tidier would be to use a proper Windows API call imported from a DLL but I can't be *rsed looking for the right one just now :D

i dont think it's possible to do that using vb becuase once the shell function has been used the program which is being executed passes over to the task manager. it it not possible to edit the program which logs you on so that it terminates when its finished loggin you on.

also there is lots of unnecessary code in the above post. you dont have to explicitly declare all of the variables!

  • Author

I cannot get the keys to register within the application.

I think the netmanage script runner doesnt like them for some reason. As you say, external program being called using shell command looks like the way forward, just not that tidy.

Thanks anyway

Mike

  • Author
also there is lots of unnecessary code in the above post. you dont have to explicitly declare all of the variables!

I know, just oversight on my part, this is something i was working with three years ago and havent used it lately

Ive done some intensive searching in google to find a way to terminate the app but all i can find are external programs that you call using shell, i think i'll have to do this unless theres another way?

Thanks :thumbup:

Mike

OK I got of my @rse and looked. Not sure if you can do all this from VBA though - external declarations might not be possible - certainly works from VB.

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 SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent 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 LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
   Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
   'Find the first window
   test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
   Do While test_hwnd <> 0
       'Check if the window isn't a child
       If GetParent(test_hwnd) = 0 Then
           'Get the window's thread
           test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
           If test_pid = target_pid Then
               InstanceToWnd = test_hwnd
               Exit Do
           End If
       End If
       'retrieve the next window
       test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
   Loop
End Function
Private Sub Form_Load()
   'KPD-Team 1999
   'URL: http://www.allapi.net/
   'E-Mail: [email protected]
   Dim Pid As Long
   'Lock the window update
   LockWindowUpdate GetDesktopWindow
   'Execute notepad.Exe
   Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
   If Pid = 0 Then MsgBox "Error starting the app"
   'retrieve the handle of the window
   mWnd = InstanceToWnd(Pid)
   'Set the notepad's parent
   SetParent mWnd, Me.hwnd
   'Put the focus on notepad
   Putfocus mWnd
   'Unlock windowupdate
   LockWindowUpdate False
End Sub
Private Sub Form_Unload(Cancel As Integer)
   'Unload notepad
   DestroyWindow mWnd
   'End this program
   TerminateProcess GetCurrentProcess, 0
End Sub

Note that the PID is NOT the same as the handle.

Example from AllAPI.net

Another possible solution using the Windows API.

1) Make sure the batch process has a unique window title (probably the EXE name)

2) Use the API FindWindow() function with the title string and a NULL windows class to get a Windows handle (HWND)

3) Send that handle a WM_CLOSE signal using PostMessage() or SendMessage()

Dirty, but doable. Nick's is more complete.

Si

You're so kind - if only any of it were "mine" :P

  • Author

cheers guys, really appreciate your time, Ive got some other stuff to do this morning but i'll get it done this afternoon.

Thanks

Mike

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Important Information

Welcome to BRISKODA. Please note the following important links Terms of Use. We have a comprehensive Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.