Solved

Call up and control applications

Posted on 2000-05-11
16
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month6 days, 1 hour left to enroll

627 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