Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
512to516
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
512to516
512to516
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

Fremdanwendung per VB schliessen

Fremdanwendung per VB schliessen
11.11.2004 11:15:50
Nike
Hi,
wie ist es moeglich eine andere Anwendung per VB zu schliessen?
Extra nicht VBA, sondern VB...
Irgendwie mit der shell?
Das unten angehaengte hab ich bisher gefunden,
aber leider hab ich wohl die falsche Caption,
kennt jemand ne Moeglichkeit, wie ich die Caption aller
zu Zeit geoeffneten Fenster auflisten lassen kann?
Kurze info waere nett...
Bye
Nikolai
' Closing another Application
' *************************************************************
' *********************************************************
'compat:vb3,vb4-16
'Close_External_Application
' Description:To have a program programmatically close anoth
' er program, use this code
' By: VB Pro'
' Inputs:strCaptionTitle--caption title of window to close
' Returns:true on success, else false
' Assumes:None
' Side Effects:None'
' Close an existing program
' *********************************************************
Declare

Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
Declare 

Function GetWindowTask Lib "User" (ByVal hWnd As Integer) As Integer
Declare 

Function PostAppMessage Lib "User" (ByVal hTask As Integer, ByVal wMsg _
As Integer, ByVal wParam As Integer, lParam As Any) As Integer
Public Const WM_QUIT = &H12
' ***************** The command button would look like this:

Sub Test()
Close_External_Application "SAP Logon 620"
End Sub


Function Close_External_Application(ByVal strCaptionTitle As String) _
As Boolean
Dim intWindowHandle As Integer
Dim intTaskHandle As Integer
Dim intPostReturnValue As Integer
' *************** set defaults
Close_External_Application = False
' *************** get handle of window matching caption
intWindowHandle = FindWindow(0&, strCaptionTitle)
If (intWindowHandle <> 0) Then
' *************** window found
intTaskHandle = GetWindowTask(intWindowHandle)
intPostReturnValue = PostAppMessage(intTaskHandle, WM_QUIT, 0, 0&)
' *************** set return value
Close_External_Application = True
End If
End Function

6
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Fremdanwendung per VB schliessen
K.Rola
Hallo,
das ist VB- Code. Erforderlich ein Form mit einer Listbox namens List1:
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
Private Sub Form_Load()
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
uProcess.dwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess)
Do While r
List1.AddItem Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
r = Process32Next(hSnapShot, uProcess)
Loop
CloseHandle hSnapShot
End Sub

Gruß K.Rola
Anzeige
AW: Fremdanwendung per VB schliessen
Nike
Hi,
danke Dir.
Angepasst wuerde es so aussehen:
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

Private Sub UserForm_Initialize()
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
uProcess.dwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess)
Do While r
Me.ListBox1.AddItem Left(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
r = Process32Next(hSnapShot, uProcess)
Loop
CloseHandle hSnapShot
End Sub

Ich weiss nur noch nicht, ob das was da jetzt in der Liste angezeigt wird
auch wirklich mit meinem anderen Code funkt ;-)
Bye
Nike
Anzeige
AW: Fremdanwendung per VB schliessen
K.Rola
Hallo,
manchmal ist es einfacher zu beschreiben, was man eigentlich will, ich habe den anderen Code nicht weiter analysiert.
Gruß K.Rola
AW: Fremdanwendung per VB schliessen
Nike
Hi,
ich moechte eigentlich einfach nur die Anwendung saplogon.exe schliessen.
Ich wuesste auch den genauen Pfad zur .exe, wenn man das dafuer benoetigen sollte...
Wenn du nen Trick wuesstest ;-)
Reines VB is nicht so meine Staerke.
Waere nett von Dir...
Bye
Nike
AW: Fremdanwendung per VB schliessen
K.Rola
Hallo,
das geht heutzutage ohne API wesentlich einfacher:
Option Explicit
Sub Programm_abschiessen()
Const STRPC As String = "."
Dim objWMI As Object, objProcesses As Object, objProcess As Object
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & STRPC & "\root\cimv2")
Set objProcesses = objWMI.ExecQuery _
("Select * from Win32_Process Where Name = 'saplogon.exe'")
For Each objProcess In objProcesses
objProcess.Terminate
Next
End Sub

Gruß K.Rola
Anzeige
AW: Fremdanwendung per VB schliessen
Nike
Hi,
ja super!
Jetzt muss ich es nur noch aus der anderen Anwendung heraus gestartet bekommen,
dann bin ich aus dem Schneider.
Danke dir auf jeden Fall.
Bye
Nikolai

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige