Solved

Call up and control applications

Posted on 2000-05-11
16
163 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now