03. december 2002 - 19:24Der er
14 kommentarer og 1 løsning
finde pid for et bestemt program
Er der nogen der ved hvordan man via VB finder pid for et bestemt program/processe der kører..og hvordan man så kan stoppe programmet/processen via dette pid!!???
det er vist ikke helt det jeg søger....det jeg søger er at kunne finde pid´en på en kørende processe der hedder java.exe...og så derefter lukke for java.exe processen via dens pid!!!
sjh...øhh ja det du skriver åbner jo bare for notepad....det jeg har brug for det er at kunne aflæse pid for en java class applikation som køre som baggrundsprocesse(java.exe) og så via det aflæste pid lukke for java applikationen!
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Private Sub Command1_Click() Dim lhWnd As Long 'Her skriver du Classname på dit program VB5 programmer er ThunderRT5Form 'og vbNullString hvis du ikke ved hvad Classname er. Const strClassName = vbNullString
'Her skriver du bare den title dit program har. 'eks: "Lommeregner" C:\WINDOWS\CALC.EXE Const strAppTitle = "Lommeregner"
'Finder lhWnd på et program lhWnd = FindWindow(strClassName, strAppTitle) 'Lukker lhWnd PostMessage lhWnd, WM_CLOSE, 0&, 0& End Sub
Jeg kan ikke skriver java class om til vb da java udføre nogle complekse billedeberegninger som kun JAI kan foretage.....nå men det der er problemmet det er at java programmet jeg har kørende ikke har noget vindue men blot køre i baggrunden....jeg har fundet frem til at processens sti om du vil er d:\jdk1.3.1_05\bin\java.exe...kan det hjælpe!?!
'GetExeFromHandle Private Const TH32CS_SNAPPROCESS As Long = 2& Private Const MAX_PATH As Long = 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 GetWindowThreadProcessId Lib "user32" ( _ ByVal hwnd As Long, _ lpdwProcessId As Long) As Long
Private Declare Function CreateToolhelpSnapshot Lib "Kernel32" _ Alias "CreateToolhelp32Snapshot" ( _ ByVal lFlgas As Long, _ ByVal 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 ProcessNext Lib "Kernel32" _ Alias "Process32Next" ( _ ByVal hSnapshot As Long, _ uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
'EnumWindowsProc Private Declare Function EnumWindows Lib "user32" ( _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Boolean
Private m_strPath As String Private m_lhWnd() As Long Private m_lCount As Long
Private Function EnumWindowsProc(ByVal lhWnd As Long, ByVal lParam As Long) As Boolean Dim strSave As String Dim lRet As Long If LCase$(m_strPath) = LCase$(GetExeFromHandle(lhWnd)) And m_lCount < UBound(m_lhWnd) Then m_lhWnd(m_lCount) = lhWnd m_lCount = m_lCount + 1 End If EnumWindowsProc = True End Function
Private Function GetExeFromHandle(hwnd As Long) As String Dim threadID As Long Dim processID As Long Dim hSnapshot As Long Dim uProcess As PROCESSENTRY32 Dim rProcessFound As Long Dim i As Integer Dim szExename As String threadID = GetWindowThreadProcessId(hwnd, processID) If threadID = 0 Or processID = 0 Then Exit Function hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&) If hSnapshot = -1 Then Exit Function uProcess.dwSize = Len(uProcess) rProcessFound = ProcessFirst(hSnapshot, uProcess) Do While rProcessFound If uProcess.th32ProcessID = processID Then i = InStr(1, uProcess.szexeFile, Chr(0)) If i > 0 Then szExename = Left$(uProcess.szexeFile, i - 1) Exit Do Else rProcessFound = ProcessNext(hSnapshot, uProcess) End If Loop Call CloseHandle(hSnapshot) GetExeFromHandle = szExename End Function
Public Function FindhWndPath(ByVal strPath As String, lhWnd() As Long) Dim i As Integer ReDim m_lhWnd(UBound(lhWnd)) As Long m_lCount = 0 m_strPath = strPath Call EnumWindows(AddressOf EnumWindowsProc, ByVal 0&) For i = LBound(lhWnd) To UBound(m_lhWnd) lhWnd(i) = m_lhWnd(i) Next i End Function '---------------------------- Module1 ----------------------------
Private Declare Function PostMessage Lib "user32" _ Alias "PostMessageA" ( _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long
Private Sub Command1_Click() Dim arrlhWnd(0 To 50) As Long Dim i As Integer Const WM_CLOSE = &H10 List1.Clear Call FindhWndPath("d:\jdk1.3.1_05\bin\java.exe", arrlhWnd()) For i = 0 To UBound(arrlhWnd) If arrlhWnd(i) <> 0 Then List1.AddItem arrlhWnd(i) PostMessage arrlhWnd(i), WM_CLOSE, 0&, 0& End If Next i End Sub '----------------------------- Form1 -----------------------------
Private Declare Function FindWindow Lib "user32" _ Alias "FindWindowA" ( _ ByVal lpClassName As Long, _ ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" ( _ ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _ ByVal hwnd As Long, _ lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" ( _ ByVal hwnd As Long, _ ByVal wCmd As Long) As Long
Private Declare Function PostMessage Lib "user32" _ Alias "PostMessageA" ( _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Const WM_CLOSE = &H10 Private m_lhWnd As Long
Public Function InstanceToWnd(ByVal target_pid As Long) As Long Dim m_hwnd As Long Dim m_pid As Long Dim m_thread_id As Long Const GW_HWNDNEXT = 2 m_hwnd = FindWindow(ByVal 0&, ByVal 0&) Do While m_hwnd <> 0 If GetParent(m_hwnd) = 0 Then m_thread_id = GetWindowThreadProcessId(m_hwnd, m_pid) If m_pid = target_pid Then InstanceToWnd = m_hwnd Exit Do End If End If m_hwnd = GetWindow(m_hwnd, GW_HWNDNEXT) Loop End Function
'Start dit program Private Sub Command1_Click() Dim lPid As Long Const strPath = "d:\jdk1.3.1_05\bin\java.exe"
PostMessage m_lhWnd, WM_CLOSE, 0&, 0& lPid = Shell(strPath, vbNormalFocus) m_lhWnd = InstanceToWnd(lPid) End Sub
'Luk dit program Private Sub Command2_Click() PostMessage m_lhWnd, WM_CLOSE, 0&, 0& End Sub '----------------------------- Form1 -----------------------------
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.