Link to home
Start Free TrialLog in
Avatar of MKrauss
MKrauss

asked on

Call up and control applications

how can i call applications e.g
notepad.exe and "CLOSE" it through
vb ????
Avatar of GivenRandy
GivenRandy

Use the Shell function.
to close an app use this:

Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

Public Const WM_CLOSE = &H10

PostMessage(winHwnd, WM_CLOSE, 0&, 0&)

where winhwnd is the handle to the window you wish to close
Avatar of MKrauss

ASKER

shell is ok, but how can I close my
application after then ???
=> Close bec. for reload situations.
ASKER CERTIFIED SOLUTION
Avatar of wsh2
wsh2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MKrauss

ASKER

I'm getting a syntax error for:
PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
whats' wrong ?
AzraSound, that does not work on NT4 here.  Not sure why.
Avatar of MKrauss

ASKER

I'm getting a syntax error for:
PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
whats' wrong ?
Avatar of MKrauss

ASKER

thanks very much wsh2, thats
exactly what I'm looking for
AzraSound, that does not work on NT4 here.  Not sure why.
AzraSound, that does not work on NT4 here.  Not sure why.
how are you getting the handle to pass to the function randy?
mkrauss...try opening a file and then closing it with the above code from wsh2...you should have taken your time
Azra:
As I was fumble thumbing on the keyboard <lol>.. you had already posted.. see my comment "(we can use an API to really tighten it down later)".. and I would agree with you.. <smile>.

As Azra so correctly points out, the shortcomings of the example I gave are.

1. The caption of the shelled application doesn't change.
2. There is only one caption active with that name.. (if there are more.. which window is closed will be unpredictable).

To be honest, I couldn't remember how to convert the ProcessID into a hWnd (its been awhile.. sheesh) or I would have coded it that way from the beginning. (Azra.. if you have a quick example it would be appreciated).
actually that escapes me as well...i was going to propose to do a "find window handle through partial window title approach"


Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "User32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetParent Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "User32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const GW_HWNDNEXT = 2

   
Private Function FindWindowPartial(Title As String) As Long
   Dim hWndTmp As Long
   Dim nRet As Long
   Dim TitleTmp As String
   '
   ' Find first window and loop through all subsequent
   ' windows in master window list.
   '
   hWndTmp = FindWindow(vbNullString, vbNullString)
   Do Until hWndTmp = 0
      '
      ' Make sure this window has no parent.
      '
      If GetParent(hWndTmp) = 0 Then
         '
         ' Retrieve caption text from current window.
         '
         TitleTmp = Space(256)
         nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))
         If nRet Then
            '
            ' Clean up return string, preparing for
            ' case-insensitive comparison.
            '
            TitleTmp = UCase(Left(TitleTmp, nRet))
            '
            ' Use appropriate method to determine if
            ' current window's caption either starts
            ' with or contains passed string.
            '
            If InStr(TitleTmp, UCase(Title)) Then
                FindWindowPartial = hWndTmp
                Exit Do
            End If
         End If
      End If
      '
      ' Get next window in master window list and continue.
      '
      hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
   Loop
End Function


Private Sub Command1_Click()
    Dim hwnd As Long
    hwnd = FindWindowPartial("Notepad")
    PostMessage(hwnd, WM_CLOSE, 0&, 0&)
End Sub
Azra:
How about this?.. <smile>

' 1. Create a new Standard.Exe project.
' 2. Add 2 command buttons to the form (Command1 and Command2).
' 3. Add a Bas Module to the Project (VB Menu -> Project -> Add Module) and Copy / Paste the following into the Module1 code window.

' <----- Code Begin ----->

Option Explicit

Private Declare Function EnumWindows Lib "user32" _
   (ByVal lpEnumFunc As Long, ByVal lParam As Long) _
   As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
   (ByVal hWnd As Long, lpdwProcessId As Long) _
   As Long

Private Declare Function PostMessageAsAny Lib "user32" Alias "PostMessageA" _
   (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) _
   As Long
   Private Const WM_CLOSE = &H10


Public Sub xApiCloseWindows(ByVal lngProcessID As Long)

   Dim lngReturn As Long
   lngReturn = EnumWindows(AddressOf xApiCloseWindows_CallBack, lngProcessID)

End Sub

Private Function xApiCloseWindows_CallBack _
(ByVal hWnd As Long, ByVal lParam As Long) _
As Long

   Dim lngProcessID As Long
   Dim lngReturn As Long
   lngReturn = GetWindowThreadProcessId(hWnd, lngProcessID)
   If lngProcessID = lParam _
   Then
      lngReturn = PostMessageAsAny(hWnd, WM_CLOSE, 0&, 0&)
   End If
   xApiCloseWindows_CallBack = True

End Function

' <----- Code End ----->

' 4. Copy / Paste the following into the Form1 code window.

' <----- Code Begin ----->

Option Explicit

Private m_lngProcess As Long

Private Sub Command1_Click()
   m_lngProcess = Shell("notepad.exe", vbNormalFocus)
End Sub

Private Sub Command2_Click()
   Call xApiCloseWindows(m_lngProcess)
End Sub

Private Sub Form_Load()
   Command1.Caption = "Load Notepad"
   Command2.Caption = "Unload Notepad"
End Sub

' <----- Code End ----->


' 5. Press F5 to run. Click on Load Notepad to start the program. Click on Unload Notepad to close it.
very nice  =)