anyone know?
such as ..
vb6.exe
explorer.exe
svchost.exe
etc
Printable View
anyone know?
such as ..
vb6.exe
explorer.exe
svchost.exe
etc
Here goes, noobie
VB Code:
Public Function KillAppByName(MyName As String) As Boolean 'kills applications by name Const PROCESS_ALL_ACCESS = 0 Dim uProcess As PROCESSENTRY32 Dim rProcessFound As Long Dim hSnapshot As Long Dim szExename As String Dim exitCode As Long Dim AppKill As Long Dim i As Integer Dim lProcHnd As Long Dim hWnd As Long On Local Error GoTo ErrTrap If winVersion = "WNT3" Or winVersion = "WNT4" Then Exit Function Const TH32CS_SNAPPROCESS As Long = 2& uProcess.dwSize = Len(uProcess) hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&) rProcessFound = ProcessFirst(hSnapshot, uProcess) Do While rProcessFound i = InStr(1, uProcess.szexeFile, Chr(0)) szExename = LCase$(Left$(uProcess.szexeFile, i - 1)) If Right$(szExename, Len(MyName)) = LCase$(MyName) Then lProcHnd = OpenProcess(PROCESS_TERMINATE, 0&, uProcess.th32ProcessID) AppKill = TerminateProcess(lProcHnd, exitCode) Call CloseHandle(lProcHnd) End If rProcessFound = ProcessNext(hSnapshot, uProcess) Loop Call CloseHandle(hSnapshot) Exit Function End Function
Declarations:
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Const PROCESS_TERMINATE = &H1
Private Const SC_CLOSE = &HF060&
Private Const WM_SYSCOMMAND = &H112
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
First off, you'll need to know the Apps title. Use the FindWindow API to get the handle of the app. Next, PostMessage API to the app WM_CLOSE. Both of these APIs and examples of how to use them are at www.allapi.net.Quote:
Originally posted by n00bie
anyone know?
such as ..
vb6.exe
explorer.exe
svchost.exe
etc
My function simply needs the name of the program, such as word.exe,...
doesnt work (im on xp)
bumpy!
I thought I'd posted this before, but couldn't find it, anyway...
Try the following in a Standard Code Module:Example Usage:VB Code:
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 * 260 End Type Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Const PROCESS_TERMINATE = &H1 Private Const VER_PLATFORM_WIN32_WINDOWS = 1 Private Const PROCESS_QUERY_INFORMATION = 1024 Private Const PROCESS_VM_READ = 16 Private Const TH32CS_SNAPPROCESS = &H2 Private Function CheckVersion() As Long Dim tOS As OSVERSIONINFO tOS.dwOSVersionInfoSize = Len(tOS) Call GetVersionEx(tOS) CheckVersion = tOS.dwPlatformId End Function Public Function GetEXEProcessID(ByVal sEXE As String) As Long Dim aPID() As Long Dim lProcesses As Long Dim lProcess As Long Dim lModule As Long Dim sName As String Dim iIndex As Integer Dim bCopied As Long Dim lSnapShot As Long Dim tPE As PROCESSENTRY32 Dim bDone As Boolean If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then 'Windows 9x 'Create a SnapShot of the Currently Running Processes lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) If lSnapShot < 0 Then Exit Function tPE.dwSize = Len(tPE) 'Buffer the First Processes Info.. bCopied = Process32First(lSnapShot, tPE) Do While bCopied 'While there are Processes List them.. sName = Left$(tPE.szExeFile, InStr(tPE.szExeFile, Chr(0)) - 1) sName = Mid(sName, InStrRev(sName, "\") + 1) If InStr(sName, Chr(0)) Then sName = Left(sName, InStr(sName, Chr(0)) - 1) End If bCopied = Process32Next(lSnapShot, tPE) If StrComp(sEXE, sName, vbTextCompare) = 0 Then GetEXEProcessID = tPE.th32ProcessID Exit Do End If Loop Else 'Windows NT 'The EnumProcesses Function doesn't indicate how many Process there are, 'so you need to pass a large array and trim off the empty elements 'as cbNeeded will return the no. of Processes copied. ReDim aPID(255) Call EnumProcesses(aPID(0), 1024, lProcesses) lProcesses = lProcesses / 4 ReDim Preserve aPID(lProcesses) For iIndex = 0 To lProcesses - 1 'Get the Process Handle, by Opening the Process lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex)) If lProcess Then 'Just get the First Module, all we need is the Handle to get 'the Filename.. If EnumProcessModules(lProcess, lModule, 4, 0&) Then sName = Space(260) Call GetModuleFileNameExA(lProcess, lModule, sName, Len(sName)) If InStr(sName, "\") > 0 Then sName = Mid(sName, InStrRev(sName, "\") + 1) End If If InStr(sName, Chr(0)) Then sName = Left(sName, InStr(sName, Chr(0)) - 1) End If If StrComp(sEXE, sName, vbTextCompare) = 0 Then GetEXEProcessID = aPID(iIndex) bDone = True End If End If 'Close the Process Handle lRet = CloseHandle(lProcess) If bDone Then Exit For End If Next End If End Function Public Function TerminateEXE(ByVal sEXE As String) As Boolean Dim lPID As Long Dim lProcess As Long lPID = GetEXEProcessID(sEXE) If lPID = 0 Then Exit Function lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID) Call TerminateProcess(lProcess, 0&) Call CloseHandle(lProcess) TerminateEXE = True End FunctionVB Code:
TerminateEXE "Notepad.exe"
Both our friends, Davos and Aaron Young, have written code that will do the trick, but they are for different platforms. While Davos's code was for Win9x platforms, Aron's code is only for NT based platforms and will not work on Win 9x since Win 9x platforms do not support PSAPI.dll (Process Status API). However, Davos's code will work on both Win 9x as well as Win 2000.
Thanks for this. I did not realise.
The code I posted will work on both Win9x and NT based platforms.
Would this link help with what you are trying to do...?
http://www.vbforums.com/showthread.p...55#post1480755
Woka
Thanks Aaron Young,
Works great!
When I try this code on Windows 98, it causes a crash on the line:
lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
or possibly the next line. The process does not terminate. I'm using this on an ActiveX EXE btw - need to close it during an install. Works great on WinXP though. Any ideas?
The documentation of TerminateProcess says:The terms terminate and kill should be used carefully; they usually imply unconditional and immediate removal from the system, as is done by TerminateProcess, which should be avoided. Many problems can be caused by TerminateProcess, including loss of data.Quote:
Use it only in extreme circumstances.
Any time someone says they want to terminate a process but they are not clear about the necessity to actually terminate a process, I ask for clarification.
The first solution that should be tried/suggested is to simply send a WM_CLOSE message to the application's main window. The WM_CLOSE message is essentially the equivalent to clicking the "X" at the top right of the window.
Use of TerminateProcess should be the last resort. It should never be used if there is another way to get the application to end.