Examples Delphi

> Last week someone from this list was kind enough to send me a unit which
> allowed for OS shutdownRobert,
Hi Robert,
Here is the sample app again. The Key points are:
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
if SetPrivilege(SE_SHUTDOWN_NAME, True) then
ExitWindowsEx(EWX_SHUTDOWN,0)
Regards,
Anthony Richardson
anthony.richardson@sageautomation.com
*************** Start of ShutDown.DPR ***************
program ShutDown;
uses
Forms,
Windows,
Dialogs,
Priv in 'Priv.pas';
{$R *.RES}
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
Application.Initialize;
Application.Run;
if SetPrivilege(SE_SHUTDOWN_NAME, True) then
ExitWindowsEx(EWX_SHUTDOWN,0)
else
MessageDlg('Failed to set privilege', mtError, [mbOK],0);
end.
*************** Start of Priv.pas ***************
unit Priv;
interface
uses Windows;
function SetPrivilege(sPrivilegeName : string; bEnabled : boolean ): Boolean;
implementation
function SetPrivilege(sPrivilegeName : string; bEnabled : boolean ): boolean;
var
TPPrev,
TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWord;
WinVersion : TOSVersionInfo;
begin
Result := False;
{ These API calls only work on a WIndows NT machine, return true
automatically if running on Windows 95/98 }
WinVersion.dwOSVersionInfoSize := SizeOf(OSVERSIONINFO);
if GetVersionEx(WinVersion) then
begin
if WinVersion.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
if OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY, Token) then
begin
TP.PrivilegeCount := 1;
if(LookupPrivilegeValue(Nil, PChar(sPrivilegeName),
TP.Privileges[ 0 ].LUID))then
begin
if( bEnabled )then
begin
TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
end else
begin
TP.Privileges[ 0 ].Attributes := 0;
end;
dwRetLen := 0;
Result := AdjustTokenPrivileges(Token, False, TP, SizeOf( TPPrev ),
TPPrev, dwRetLen );
end;
CloseHandle(Token);
end;
end
else
result := True;
end;
end;
end.
_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://elists.org/mailman/listinfo/delphi
********************************************************************
Re ShutDown: I once got this some time ago.
Maybe it helps
function GetShutDownPriv : boolean;
var
htoken : THandle;
tkp, p : TTokenPrivileges;
retlen : dword;
reply : dword;
begin
Result := False;
if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES OR
TOKEN_QUERY, htoken) then
begin
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege',
tkp.Privileges[0].luid) then
begin
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := Se_Privilege_Enabled;
AdjustTokenPrivileges(htoken, False, tkp, SizeOf(tkp), p, retlen);
reply := GetLastError;
if reply = Error_Success then
Result := True;
end;
end;
end;
begin
Randomize;
if GetShutDownPriv then
InitiateSystemShutdown(nil, pchar('This is it - the very end!'), 10,
True, False);
end;
*****************************************************************************
robert - i am one of the people that sent you code - i was the one who wrote
and sent you TShutDownEX - it will work on 98,95,NT