Thanks That worked great! Here is the Test Code I Used:
Code:
Imports System.Management
Public Class frmMain
Const WM_QUIT As UInteger = &H12
Private Declare Function PostThreadMessage Lib "user32.dll" Alias "PostThreadMessageA" (ByVal idThread As Integer, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
Private Sub EndProg()
' This version uses WMI which also gives access to the Exe file's Path
Dim ThreadID As Integer = 0
'See if MyProgram is running from External drive 'G', and shut it down gracefully
Dim Win32Procs As New ManagementObjectSearcher("SELECT * FROM Win32_Process Where (ExecutablePath = 'G:\MyProgram.exe')")
For Each Win32Proc As ManagementObject In Win32Procs.Get()
'Debug.Print("Name:" & Win32Proc("Name") & " Path:" & Win32Proc("ExecutablePath")) 'Items.Add(disk("Name").ToString())
Dim Win32Threads As New ManagementObjectSearcher("SELECT * FROM Win32_Thread Where (ProcessHandle = '" & Win32Proc("ProcessId").ToString & "')")
For Each Win32Thread As ManagementObject In Win32Threads.Get()
ThreadID = CInt(Win32Thread("Handle"))
PostThreadMessage(ThreadID, WM_QUIT, IntPtr.Zero, IntPtr.Zero)
Next
Next
End Sub
Private Sub btEndIt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEndIt.Click
EndProg()
End Sub
End Class
Notice here that you could shutdown all occurances of MyProgram.exe by changing the 'Where' Clause to look for Name = 'MyProgram.exe' instead of looking for ExecutablePath = 'G:\MyProgram.exe'