Avatar billede mallemukken Nybegynder
03. marts 2004 - 10:30 Der er 5 kommentarer og
2 løsninger

Lukke en process/program/service

hej

Jeg har et program jeg ikke kan lukke med følgende kode jeg har fundet her på eksperten. programmet kører i baggrunde som en service/process. Er der nogen der har et bud hvorfor.

kodestykket finder også processen , men det lykkes ikke at lukke det.

jeg kalder programmet med killproc(-navn på process-)



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 TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Const WM_DESTROY = &H10

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
Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

Public procID As Long
Public hWndToKill As Long


Sub KillProc(exeName As String)
    hWndToKill = 0
    procID = 1
    Dim procs As PROCESSENTRY32
    Dim ans As Long, r As Long
    Dim hSnap As Long, exitCode As Long, hwnd As Long, tpID As Long
    Dim l As Integer
       
    procs.dwSize = Len(procs)
    exeName = LCase(exeName)
    l = Len(exeName)
   
    hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
    ans = Process32First(hSnap, procs)
   
    While (ans)
        procs.szExeFile = LCase(procs.szExeFile)
        If (exeName = Left(procs.szExeFile, l)) Then
            procID = procs.th32ProcessID
            r = EnumWindows(AddressOf EnumWindowsProc, ByVal 0&)
            r = SendMessage(hWndToKill, WM_DESTROY, exitCode, 0)
            CloseHandle hSnap
            Exit Sub
        End If
        ans = Process32Next(hSnap, procs)
    Wend
   
    CloseHandle hSnap
       
End Sub

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
    Dim z As Long, thisProc As Long
    z = GetWindowThreadProcessId(hwnd, thisProc)
         
    If procID = thisProc Then
        hWndToKill = hwnd
        EnumWindowsProc = False ' stop enumeration
    Else
        EnumWindowsProc = True ' fortsæt enumeration
    End If
   
End Function
Avatar billede lokespas Nybegynder
03. marts 2004 - 13:55 #1
har ikke lige tid til at kigge på det lige nu men havde denne stump code liggene


modul:

Option Explicit
Const MAX_PATH& = 260

Declare Function TerminateProcess _
    Lib "kernel32" (ByVal ApphProcess As Long, _
    ByVal uExitCode As Long) As Long
Declare Function OpenProcess Lib _
    "kernel32" (ByVal dwDesiredAccess As Long, _
    ByVal blnheritHandle As Long, _
    ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst _
    Lib "kernel32" Alias "Process32First" _
    (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext _
    Lib "kernel32" Alias "Process32Next" _
    (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot _
    Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
    (ByVal lFlags As Long, _
    lProcessID As Long) As Long
Declare Function CloseHandle _
    Lib "kernel32" (ByVal hObject As Long) As Long

Private Type LUID
  lowpart As Long
  highpart As Long
End Type

Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    LuidUDT As LUID
    Attributes As Long
End Type

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Declare Function GetVersion _
    Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess _
    Lib "kernel32" () As Long
Private Declare Function OpenProcessToken _
    Lib "advapi32" (ByVal ProcessHandle As Long, _
    ByVal DesiredAccess As Long, _
    TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue _
    Lib "advapi32" Alias "LookupPrivilegeValueA" _
    (ByVal lpSystemName As String, _
    ByVal lpName As String, _
    lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges _
    Lib "advapi32" (ByVal TokenHandle As Long, _
    ByVal DisableAllPrivileges As Long, _
    NewState As TOKEN_PRIVILEGES, _
    ByVal BufferLength As Long, _
    PreviousState As Any, _
    ReturnLength As Any) As Long

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 Function KillApp(myName As String) As Boolean
  Const TH32CS_SNAPPROCESS As Long = 2&
  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 myProcess As Long
  Dim AppKill As Boolean
  Dim appCount As Integer
  Dim i As Integer
  On Local Error GoTo Finish
  appCount = 0
 
  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
          KillApp = True
          appCount = appCount + 1
          myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)

      End If
      rProcessFound = ProcessNext(hSnapshot, uProcess)
  Loop
  Call CloseHandle(hSnapshot)
  Exit Function
Finish:
    MsgBox "Error!"
End Function

'Terminate any application and return an exit code to Windows.
Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
    Dim hToken As Long
    Dim hProcess As Long
    Dim tp As TOKEN_PRIVILEGES
   

    If GetVersion() >= 0 Then

        If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
            GoTo CleanUp
        End If

        If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
            GoTo CleanUp
        End If

        tp.PrivilegeCount = 1
        tp.Attributes = SE_PRIVILEGE_ENABLED

        If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
            GoTo CleanUp
        End If
    End If

    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    If hProcess Then

        KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
        ' close the process handle
        CloseHandle hProcess
    End If
   
    If GetVersion() >= 0 Then
        ' under NT restore original privileges
        tp.Attributes = 0
        AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
       
CleanUp:
        If hToken Then CloseHandle hToken
    End If
   
End Function
form:
Private Sub cmdKill_Click()
    Dim strExe As String
    strExe = "Notepad.Exe"
    KillApp (strExe)
End Sub
Avatar billede mallemukken Nybegynder
03. marts 2004 - 15:22 #2
tak

men det virker desværre ikke. Heller ikke hvis jeg har eks. notepad.exe åben.
den kommer godt nok ind i løkken , ??
Avatar billede lokespas Nybegynder
03. marts 2004 - 15:43 #3
undskyld jeg er lidt af en fladpande, man skal jo også kalde function killprocess



      If Right$(szExename, Len(myName)) = LCase$(myName) Then
          KillApp = True
          appCount = appCount + 1
          myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
          KillProcess uProcess.th32ProcessID, 0 'det her skal lige sættes ind
      End If
Avatar billede mallemukken Nybegynder
03. marts 2004 - 15:54 #4
istedet for at lukke notepad.exe lukker den vb6
Avatar billede lokespas Nybegynder
03. marts 2004 - 16:08 #5
æhhh!! hos mig lukker den kun notepad og ikke andet!
jeg ved desværre ikke hvad det kan være! du bliver nødt til debugge, jeg har lidt svært med generer den fejl!!!
Avatar billede mallemukken Nybegynder
04. marts 2004 - 08:47 #6
Hej Lokespas

Jeg har fået det til at virke, og det virker fint.

jeg havde lavet en lille bøf - men din kode virker

her er lidt point til weekenden
Avatar billede lokespas Nybegynder
04. marts 2004 - 09:52 #7
herligt :-)  takker og god week-end!!
Avatar billede Ny bruger Nybegynder

Din løsning...

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester