Increase your program privileges

Share your advanced PureBasic knowledge/code with the community.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Increase your program privileges

Post by newbie »

Code updated For 5.20+
Hello,

Increasing it's own program privileges is needed for instance if you want to enumerate all running processes. With the search fucntion I found few other posts with a procedure like this, but mine checks (hopefully) all errors that can happens.

Code: Select all

Procedure.b IncreasePrivileges()
    Protected hToken, Buff
    Protected mLUID.LUID
    Protected mPriv.TOKEN_PRIVILEGES
    Protected mNewPriv.TOKEN_PRIVILEGES
    Protected CurrentProcess = GetCurrentProcess_()
    #TOKEN_ADJUST_PRIVILEGES = $20
	#TOKEN_QUERY = $8
    mPriv\PrivilegeCount = 1
    mPriv\Privileges[0]\Attributes = #SE_PRIVILEGE_ENABLED
    
    If CurrentProcess = 0
    	ProcedureReturn #False
    EndIf
    
    If OpenProcessToken_(CurrentProcess, #TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY, @hToken) = 0
    	ProcedureReturn #False
    EndIf
    
    If LookupPrivilegeValue_(#Null, "SeDebugPrivilege", @mLUID) = 0
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
        
    If IsBadWritePtr_(@mPriv\Privileges[0]\Luid, SizeOf(LUID)) <> 0
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
    
    CopyMemory(@mLUID, @mPriv\Privileges[0]\Luid, SizeOf(LUID))
                 
    If AdjustTokenPrivileges_(hToken, #False, @mPriv, SizeOf(TOKEN_PRIVILEGES), @mNewPriv, @Buff) = 0
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
    
    If GetLastError_() <> #ERROR_SUCCESS
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
    
    ; If we get here, then it worked. Returns TRUE
    CloseHandle_(hToken) 
    ProcedureReturn #True
EndProcedure
Then you can use it like this :

Code: Select all

If IncreasePrivileges() = #False
    MessageBox_(0, "Could not retrieve all privileges.", "Error :", #MB_ICONEXCLAMATION)
EndIf
It's not really a "hot stuff", but it might help someone ;)
- Registered PB user -

Using PB 4.00
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

Hi newbie,

this is the first step required on the project

http://www.planet-source-code.com/vb/sc ... 9&lngWId=1

:-)

jpd
Post Reply