Page 1 of 1

protect process from closing with taskmanager

Posted: Sun Apr 14, 2013 5:37 pm
by codeprof
This prevents access by other processes. So you cannot terminate the process with the task manager. Of course with administrator rights you can still do it.

Code: Select all

; Licence: Public domain
; Autor: codeprof

#OWNER_SECURITY_INFORMATION    =   $00000001
#GROUP_SECURITY_INFORMATION    =   $00000002
#DACL_SECURITY_INFORMATION     =   $00000004
#SACL_SECURITY_INFORMATION     =   $00000008
#LABEL_SECURITY_INFORMATION    =   $00000010
 
#PROTECTED_DACL_SECURITY_INFORMATION   =  $80000000
#PROTECTED_SACL_SECURITY_INFORMATION   =  $40000000
#UNPROTECTED_DACL_SECURITY_INFORMATION =  $20000000
#UNPROTECTED_SACL_SECURITY_INFORMATION =  $10000000
 
*pACL.ACL
cbACL = 1024;
 
; Initialize a security descriptor.
*pSD.SECURITY_DESCRIPTOR = AllocateMemory(#SECURITY_DESCRIPTOR_MIN_LENGTH)
InitializeSecurityDescriptor_(*pSD, #SECURITY_DESCRIPTOR_REVISION)
*pACL = AllocateMemory(cbACL);
InitializeAcl_(*pACL, cbACL, #ACL_REVISION2)
SetSecurityDescriptorDacl_(*pSD, #True, *pACL, #False)
 
;SetFileSecurity_("C:\TEST.TXT",#DACL_SECURITY_INFORMATION, *pSD) ; <-- remove all rights from a certain file
SetKernelObjectSecurity_(GetCurrentProcess_(), #DACL_SECURITY_INFORMATION, *pSD) ; <-- now you cannot close the process with the task manager



OpenWindow(1,0,0,500,300,"Try to close me with task manager")
ButtonGadget(0,0,0,100,20,"close")
Repeat
Until WaitWindowEvent() = #PB_Event_Gadget And EventGadget() = 0

Re: protect process from closing with taskmanager

Posted: Sun Apr 14, 2013 9:22 pm
by jassing
cool -- but only works on x86; when I run it as a 64bit app, I can kill it w/o issue. I noticed when I ran it as 32bit app, the user name was blank in task manager, but as a 64bit app, the user name was there.

Re: protect process from closing with taskmanager

Posted: Mon Apr 15, 2013 12:07 pm
by PureGuy
Thanks, codeprof.

@jassing it works here as x64 exe on Windows 7.