Está en la página 1de 5

Module: Option Explicit 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) A s 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 Lon g, 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) A s Long Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As L ong, 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" (lpVe rsionInformation As OSVERSIONINFO) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long , ByVal uExitCode As Long) As Long 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 Private Private Private Private Const Const Const Const Const PROCESS_TERMINATE = &H1 VER_PLATFORM_WIN32_WINDOWS = 1 PROCESS_QUERY_INFORMATION = 1024 PROCESS_VM_READ = 16 TH32CS_SNAPPROCESS = &H2

Private Function CheckVersion() As Long Dim tOS As OSVERSIONINFO tOS.dwOSVersionInfoSize = Len(tOS) Call GetVersionEx(tOS) CheckVersion = tOS.dwPlatformId End Function

Private 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, aP ID(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 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 Function ***************************************************************************** Private Sub Command5_Click() Dim blnRet As Boolean blnRet = TerminateEXE("vlc.exe") If Not blnRet Then MsgBox "VLC Player is not running.", vbInformation, "Error" End If End Sub ***************************************************************************** Option Explicit Private Declare Function FINDWINDOW Lib "user32" Alias "FindWindowA" (ByVal lpCl assName As String, ByVal lpWindowName As String) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String ) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hw nd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const WM_CLOSE = &H10 Private Sub Command5_Click() 'check if VLC media player is open 'if it is, close it. If not, tell me it's not open. Dim lVLCwindowhandle As Long Dim lVLCplaying As Long lVLCwindowhandle = FINDWINDOW("VLC media player", vbNullString) lVLCplaying = FindWindowEx(lVLCwindowhandle, 0&, "QWidget", vbNullString)

If lVLCplaying Then SendMessage lVLCplaying, WM_CLOSE, vbNull, vbNull '<--- this is where the proble m was, I had forgotten to write "vbNull" x2 MsgBox "VLC has been closed.", vbInformation, "Error" Else MsgBox "VLC is not open.", vbInformation, "Info" End If End Sub

To get a list of all applications/proceses currently running, start a new projec t, add a command button and a list box to Form1. This first solution was found at vb-world.net (I can't take credit for it!) '<><><><><><><><><><><><><><><><> ' Solution 1 '<><><><><><><><><><><><><><><><> Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLe ngthA" (ByVal hwnd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVa l hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Const GW_HWNDFIRST = 0 Const GW_HWNDNEXT = 2 Private Sub Command1_Click() LoadTaskList End Sub Sub LoadTaskList() Dim CurrWnd As Long Dim Length As Long Dim TaskName As String Dim Parent As Long List1.Clear CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST) While CurrWnd <> 0 Parent = GetParent(CurrWnd) Length = GetWindowTextLength(CurrWnd) TaskName = Space$(Length + 1) Length = GetWindowText(CurrWnd, TaskName, Length + 1) TaskName = Left$(TaskName, Len(TaskName) - 1) If Length > 0 Then

If TaskName <> Me.Caption Then List1.AddItem TaskName End If End If CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT) DoEvents Wend End Sub

'<><><><><><><><><><><><><><><><><><> 'Add this one to your tool box. It returns the physical path and filename of any EXE or DLL running on a system. 'You 'll need a command button and a list box: Public Const TH32CS_SNAPPROCESS As Long = 2& Public Const MAX_PATH As Integer = 260 Public 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 Public Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias _ "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As L ong Public Declare Function ProcessFirst Lib "Kernel32" Alias "Process32First" _ () '(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Public Declare Function ProcessNext Lib "Kernel32" Alias "Process32Next" _ () '(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Public Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long) Private Sub Command1_Click() Dim hSnapShot As Long Dim uProcess As PROCESSENTRY32 Dim r As Long hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&) If hSnapShot = 0 Then Exit Sub End If uProcess.dwSize = Len(uProcess) r = ProcessFirst(hSnapShot, uProcess) Do While r List1.AddItem uProcess.szExeFile r = ProcessNext(hSnapShot, uProcess) Loop Call CloseHandle(hSnapShot) End Sub

También podría gustarte