Re: Privileges and killing a process



Wow, sure seems like a lot of code when you can run a simple script
and accomplish what you want.

watch for line wraps!

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'notepad.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

Lee


On Thu, 2 Jun 2005 06:54:01 -0700, "pithhelmet"
<pithhelmet@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

>Hi Everyone -
>
>This is kinda a crazy one, so i thought i would post it up...
>
>I have an application that is spawned if there is a fatal error in the
>primary application.
>
>This spawned program simply goes out and looks for orphaned applications
>that are spawned from the main application (activex .exes) and if there are
>any, then the application will kill them (by terminating the process by PID).
>
>I have used the code snippet that is floating around the web (kill process)
>that changes the privileges at run time....
>
>Trouble is, running in the IDE - the application runs perfect -
>running outside the IDE, the program doesn't kill the applications
>(notepad.exe) is the example that i'm using.
>
>In win2k, it just jumps right over notepad.exe,
>in win2k3, it also jumps over notepad.exe - but it also blows out the themes
>and won't allow the logged on user to log off or shutdown the computer.
>
>I've commented out the code section about changing the privileges - and it
>stopped the killing of the themes, and will allow the user to log off and
>restart the computer, but it will not kill the process (notepad).
>
>I have confirmed the PID of notepad and taskmanager, spy++ and the
>application tries to kill the right PID, so thats not the issue (wrong PID)
>
>
>but the process won't die.
>
><code>
>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
>If LookupPrivilegeValue(vbNullString, "SeImpersonatePrivilege", 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
>
></code>
>
>thanks
>
>tony

.



Relevant Pages

  • Privileges and killing a process
    ... then the application will kill them (by terminating the process by PID). ... Dim hProcess As Long ... GoTo CleanUp ...
    (microsoft.public.vb.general.discussion)
  • Re: Periodic Fedora 9 system hangs with jumpy mouse
    ... kill -TERM pid ... to do with graphics card and driver. ...
    (Fedora)
  • Re: how can I make sure only one instance of gkrellm runs
    ... It is the standard way of probing for a process given a pid. ... "man 3p kill" says: ... since the script is executing the command ... But for a single person running a single GUI ...
    (Fedora)
  • RE: threads in perl
    ... Instead of xterm I tried to invoke some other script: ... How can I kill exec after it was started in different thread? ... But threads all share the same pid, ... When life conspires against you, and no longer floats your boat, Don't waste your time with crying, just get on your back and float. ...
    (perl.beginners)
  • Re: Killing a process tree? - 1 attachment
    ... Well, you could use ps to get process and parent process pid, ... match subprocess to parent process, and kill in reverse order. ...
    (alt.os.linux)