Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Call up and control applications

Posted on 2000-05-11
16
Medium Priority
?
171 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Accepted Solution

by:
wsh2 earned 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses

926 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