Solved

Call up and control applications

Posted on 2000-05-11
16
167 Views
Last Modified: 2010-05-02
how can i call applications e.g
notepad.exe and "CLOSE" it through
vb ????
0
Comment
Question by:MKrauss
  • 5
  • 4
  • 4
  • +1
16 Comments
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2801340
Use the Shell function.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2801367
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
0
 
LVL 1

Author Comment

by:MKrauss
ID: 2801368
shell is ok, but how can I close my
application after then ???
=> Close bec. for reload situations.
0
Independent Software Vendors: 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!

 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2801396
A real simple VB example to get you started.. (we can use an API to really tighten it down later).

' 1. Create a new Standard.Exe project.
' 2. Add 2 command buttons to the form (Command1 and Command2).
' 3. Copy / Paste the following into the Form1 code window.
' 4. Press F5 to run. Click on Load Notepad to start the program. Click on Unload Notepad to close it.

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

Option Explicit

Private Sub Command1_Click()
   Dim lngprocess As Long
   lngprocess = Shell("notepad.exe", vbNormalFocus)
End Sub

Private Sub Command2_Click()
   AppActivate ("Untitled - Notepad")
   SendKeys "%FX"
End Sub

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

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




0
 
LVL 1

Author Comment

by:MKrauss
ID: 2801398
I'm getting a syntax error for:
PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
whats' wrong ?
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2801400
AzraSound, that does not work on NT4 here.  Not sure why.
0
 
LVL 1

Author Comment

by:MKrauss
ID: 2801411
I'm getting a syntax error for:
PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
whats' wrong ?
0
 
LVL 1

Author Comment

by:MKrauss
ID: 2801419
thanks very much wsh2, thats
exactly what I'm looking for
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2801421
AzraSound, that does not work on NT4 here.  Not sure why.
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2801450
AzraSound, that does not work on NT4 here.  Not sure why.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2801453
how are you getting the handle to pass to the function randy?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2801473
mkrauss...try opening a file and then closing it with the above code from wsh2...you should have taken your time
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2801509
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).
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2801525
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
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2801768
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.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2801807
very nice  =)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

730 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