http://news.xbwin.com/ot4xb.examples/5/
This sample will check first if the current user have administrative privileges
if lIsUserAdmin()
? "Current user has administrative privileges"
...
If the privilege is not admin, a logon screen appears to get a user/pwd with admin privileges.
Then the program will try to run the same exe with the provided user/pwd account and close itself if success full.
So you will get a new instance of the same exe but this time running with the privileges of the provided account.
#include "ot4xb.ch"
proc DbeSys() ; return
// ---------------------------------------------------------------------------
proc main()
local nKey := 0
if nGetProcAddress( "AdvApi32","CreateProcessWithLogonW") == 0
? "This sample requires one of the following platforms:"
? ""
? " - Windows 2000 Professional "
? " - Windows 2000 Server "
? " - Windows XP "
? " - Windows Server 2003 "
? " - Windows Vista "
? " - Requires Windows Server 2008 "
? ""
? "Press ESC to exit ...."
inkey(0)
return
end
if lIsUserAdmin()
? "Current user has administrative privileges"
? " ------------------------------------------"
? "Press ESC to exit ..."
while ( nKey := inkey(0) ) != 27 ; end
else
while ! lLogonAsAdmin()
? "Log on failed"
? "Press ESC to exit or any other key to retry"
if ( inkey(0) == 27 ) ; return ; end
end
end
return
// ---------------------------------------------------------------------------
function lIsUserAdmin()
local result := 0
local cSia := ChrR(0,0,0,0,0,5,1)
local pSid := 0
if( @Advapi32:AllocateAndInitializeSid(cSia,2,0x20,0x220,0,0,0,0,0,0,@pSid) != 0 )
if( @Advapi32:CheckTokenMembership(0,pSid,@result) == 0 )
result := 0
end
@Advapi32:FreeSid(pSid)
end
return (result != 0)
// ---------------------------------------------------------------------------
function lLogonAsAdmin()
local cUser := Space(60)
local cPwd := Space(60)
local aSI := AFill(Array(17),0)
local aPI := AFill(Array(4),0)
local cWApp := cSzAnsi2Wide(AppName(.F.))
local result := .F.
aSI[1] := 68
if ! lLogOnDialog(@cUser,@cPwd)
return .F.
end
cUser := cSzAnsi2Wide(cUser)
cPwd := cSzAnsi2Wide(cPwd)
result := ( @AdvApi32:CreateProcessWithLogonW(cUser,cSzAnsi2Wide("."),;
cPwd,1,cWApp,0,0,0,0,aSI,@aPI) != 0 )
if result
if aPI[1] != 0 ; @kernel32:CloseHandle(aPI[1]) ; end
if aPI[2] != 0 ; @kernel32:CloseHandle(aPI[2]) ; end
end
return result
// ---------------------------------------------------------------------------
function lLogOnDialog(cUser,cPwd)
local GetList := {}
local result
cls
SET CONFIRM ON
SetCursor(1)
? "The current user has no administrative privileges"
? "Enter the login information."
? "------------------------------------------------ "
? "UserName: " ; @ row() , col() GET cUser
? "------------------------------------------------ "
? "Password: " ; @ row() , col() GET cPwd
? "------------------------------------------------ "
result := ReadModal(GetList)
SetCursor(0)
cls
cUser := AllTrim(cUser)
cPwd := AllTrim(cPwd)
return result
// ---------------------------------------------------------------------------