Link to home
Start Free TrialLog in
Avatar of kolistivra
kolistivra

asked on

killing a process

i would like to kill a process and i've tried many codes for this. most of the codes work properly for most programs BUT none of them was successful with notepad. open notepad and write something in it, and try to kill it. notepad will ask you if you want to save changes. i don't want such message, it should be closed without anything like most programs. i would like to be able to close any program i want without any message or something..

thanks in advance...
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Option Explicit

Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess _
    As Long, ByVal uExitCode As Long) As Long

Private pid As Long

Private Sub Command1_Click()
    pid = Shell("notepad.exe", vbNormalFocus)
End Sub

Private Sub Command2_Click()
    Dim lngProcess As Long
   
    If pid <> 0 Then
        lngProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, pid)
        If lngProcess <> 0 Then
            Call TerminateProcess(lngProcess, 0&)
        End If
    End If
End Sub
SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kolistivra
kolistivra

ASKER

i will split points but before,
idle mind, can you also write a code for finding the "pid" of any program ?
EDDKT, i didn't understand your code much but it works fine, can you explain it a bit?

thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial