Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: VB6: Prosessin sulkeminen

Meitsi [17.12.2004 21:35:38]

#

Yritin väsätä ohjelmaa jolla saa suljettua windowsin päivityksen "käynnistä kone uudelleen, jotta päivitykset tulevat voimaan"-popupin joka pomppaa säännöllisin väliajoin ruudulle. Se näkyy taskmanagerin listassa nimellä "wuauclt.exe".

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) 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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_QUIT = &H12
Private running As Boolean
Private Const PROCESS_TERMINATE = &H1

Private Sub Command1_Click()
running = True
Command2.Enabled = True
Command1.Enabled = False
End Sub

Private Sub Command2_Click()
running = False
Command1.Enabled = True
Command2.Enabled = False
End Sub

Private Sub Form_Load()
Timer1.Interval = 10
running = False
End Sub

Private Sub Timer1_Timer()
If running = True Then
    Dim turhahwnd As Long, pid As Long, ph As Long
    ' etsitään hwnd, huomaa vbNullString (0 ei toimi tässä)
    turhahwnd = FindWindow(vbNullString, "wuauclt.exe")
    ' hankitaan pid
    GetWindowThreadProcessId turhahwnd, pid
    ' prosessin kahva
    ph = OpenProcess(PROCESS_TERMINATE, 0, pid)
    ' terminoidaan prosessi
    turhahwnd = TerminateProcess(ph, 0)
End If
End Sub

Otin tuon täältä putkan vb alueelta, tuo koodi ei kuitenkaan tee mitään. Prosessi ei häviä listalta minnekkään... Miten saisi toimimaa?

EDIT: TUossa on vähän ylimääräisiä declareita...

Antti Laaksonen [17.12.2004 21:51:00]

#

Tämä koodi tuntui toimivan ainakin minulla:
http://www.andreavb.com/tip020021.html

Tosin PROCESS_ALL_ACCESSin arvoksi piti muuttaa 4095.

Katso myös:
http://www.experts-exchange.com/Programming/Q_21133037.html

Meitsi [17.12.2004 23:46:40]

#

Hmm... ei toiminut ainakaan mulla:
moduuliin laitoin:

 Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long

Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long

Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long

Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
ByVal lFlags As Long, lProcessID As Long) As Long

Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long

Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Const PROCESS_ALL_ACCESS = 4095
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 * 260
End Type
 Public Sub KillProcess(NameProcess As String)

Const TH32CS_SNAPPROCESS As Long = 2&
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
Dim WinDirEnv As String

       If NameProcess <> "" Then
          AppCount = 0

          uProcess.dwSize = Len(uProcess)
          hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
          RProcessFound = ProcessFirst(hSnapshot, uProcess)

          Do
            i = InStr(1, uProcess.szexeFile, Chr(0))
            SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            WinDirEnv = Environ("Windir") + "\"
            WinDirEnv = LCase$(WinDirEnv)

            If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
               AppCount = AppCount + 1
               MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
               AppKill = TerminateProcess(MyProcess, ExitCode)
               Call CloseHandle(MyProcess)
            End If
            RProcessFound = ProcessNext(hSnapshot, uProcess)
          Loop While RProcessFound
          Call CloseHandle(hSnapshot)
       End If

End Sub

Formilla on:

Private running As Boolean

'-------------------------------------------------------


Private Sub Command1_Click()
running = True
Command2.Enabled = True
Command1.Enabled = False
End Sub

Private Sub Command2_Click()
running = False
Command1.Enabled = True
Command2.Enabled = False
End Sub

Private Sub Form_Load()
Timer1.Interval = 10
running = False
End Sub

Private Sub Timer1_Timer()
If running = True Then
    'Dim turhahwnd As Long, pid As Long, ph As Long
    ' etsitään hwnd, huomaa vbNullString (0 ei toimi tässä)
    'turhahwnd = FindWindow(vbNullString, "wuauclt.exe")
    ' hankitaan pid
    'GetWindowThreadProcessId turhahwnd, pid
    ' prosessin kahva
    'ph = OpenProcess(PROCESS_TERMINATE, 0, pid)
    ' terminoidaan prosessi
    'turhahwnd = TerminateProcess(ph, 0)
    'PostMessage turhahwnd, WM_QUIT, 0, 0
    KillProcess "wuauclt.exe"
End If
End Sub

Ja eihän tuota experts exchangen vastausta voi lukea ellei maksa tuota maksua... kai?

EDIT: eiku kyl ne kommentit voi lukea. Tossa vaa mainostettii jotai sign up to see solution...

Gaxx [17.12.2004 23:55:52]

#

Kyllä tuo koodisi ainakin minulla toimii (ainakin) tuon KillProcess:n osalta.

Edit: Käyttis on 98se

Edit2: Pistä formille pelkästään KillProcess "VB6.exe"(tai mikä vb sulla nyt onkaan...), niin ei ainakaan riipu muusta koodista ;)

Meitsi [17.12.2004 23:56:29]

#

Mulla ei tapahdu mitään. Se prosessi pysyy siellä task managerin listassa...

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta