Solved

A componnent to shutdown Windows NT

Posted on 2002-03-07
5
203 Views
Last Modified: 2010-04-05
Hello,
Does anyone knows about a componnent for Delphi 4 to shutdown Windows NT?
Thanks a lot in advance,  Libby
0
Comment
Question by:libbysharf
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6849462
here is a unit you can use with win9* and nt ,it will check and set the right privileges etc if os is nt
just stick unit in lib or your project dir so delphi cna find it and add unit name to uses and call

ExitWindowsF(flags)
//same flags as exitwindowsex see win32.hlp for details



unit ExitWin;

interface

uses
Windows;

function PlatformNT : boolean;
{ Betriebssystemplattform ermitteln
Funktionsergebnis false: Windows 95
true : Windows NT }

function ExitWindowsF(uFlags : word) : boolean;
{ Windows (95/NT) herunterfahren
uFlags: siehe Windows-SDK ExitWindowsEx }

implementation

const
{ in Delphi nicht deklariertes }
ANYSIZE_ARRAY = 1;
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';

var
IsNT : boolean;

function PlatformNT : boolean;
begin
PlatformNT:=IsNT;
end;

function ExitWindowsNT(uFlags : word) : boolean;
// Routine für Windows NT
var
hToken : THandle;
ptkp, ptkpold : PTokenPrivileges;
r : dword;
begin
// Token Handle des aktuellen Prozesses ermitteln
if OpenProcessToken(GetCurrentProcess,
{$IFDEF DELPHI2}
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, @hToken) then
{$ELSE}
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
{$ENDIF}
begin
// LUID für shut down ermitteln und Privileg setzen
GetMem(ptkp,sizeof(TTOKENPRIVILEGES) +
(1-ANYSIZE_ARRAY) * sizeof(TLUIDANDATTRIBUTES));
LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME,
ptkp^.Privileges[0].Luid);
ptkp^.PrivilegeCount:=1; // Anzahl zu setzender Privilegien ptkp^.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
// Privileg für diesen Prozess setzen
r:=0;
ptkpold:=nil;
if AdjustTokenPrivileges(hToken, false, ptkp^, 0, ptkpold^, r) then ExitWindowsNT:=ExitWindowsEx(uFlags,0);
end;
ExitWindowsNT:=GetLastError=ERROR_SUCCESS;
end;

function ExitWindowsF(uFlags : word) : boolean;
// Windows (95/NT) beenden
begin
if IsNT then ExitWindowsF:=ExitWindowsNT(uFlags)
else ExitWindowsF:=ExitWindowsEx(uFlags,0);
end;

procedure CheckOS;
// Betriebssystemplattform ermitteln
var
VerInfo : TOSVersionInfo;
begin
IsNT:=false;
VerInfo.dwOSVersionInfoSize:=sizeof(VerInfo);
if (GetVersionEx(VerInfo)) then
IsNT:=VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT;
end;

begin
CheckOS;
end.
0
 

Author Comment

by:libbysharf
ID: 6854283
Thanks inthe, I'm sure it works, but it doesn't work on my computer.... People sent me some programs which are supposed to work on NT but it doesn't work when I try it .

Maybe it has something to do with the version? I have a
NT 4 Service Package 6 (workstation).
Regards, Libby
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 6854648
hi,]
It doesnt matter what version of nt your using it will / should still work (also on win2k etc)

i just tested and noticed that if you paste the above unit into a new unit then the following line:
ptkp^.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
is commented out when it shouldnt be it is important line so i would double check that its not commented out.

or better just paste this version below into new unit and delete any dcu you already have of Exitwin so a new version is compiled then try :


unit ExitWin;

interface

uses
Windows;

function PlatformNT : boolean;

function ExitWindowsF(uFlags : word) : boolean;

implementation

const
ANYSIZE_ARRAY = 1;
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';

var
IsNT : boolean;

function PlatformNT : boolean;
begin
PlatformNT:=IsNT;
end;

function ExitWindowsNT(uFlags : word) : boolean;
var
hToken : THandle;
ptkp, ptkpold : PTokenPrivileges;
r : dword;
begin
if OpenProcessToken(GetCurrentProcess,
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
GetMem(ptkp,sizeof(TTOKENPRIVILEGES) +
(1-ANYSIZE_ARRAY) * sizeof(TLUIDANDATTRIBUTES));
LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME,
ptkp^.Privileges[0].Luid);
ptkp^.PrivilegeCount:=1;
ptkp^.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
r:=0;
ptkpold:=nil;
if AdjustTokenPrivileges(hToken, false, ptkp^, 0, ptkpold^, r) then
ExitWindowsNT:=ExitWindowsEx(uFlags,0);

end;
ExitWindowsNT:=GetLastError=ERROR_SUCCESS;
end;

function ExitWindowsF(uFlags : word) : boolean;
begin
if IsNT then ExitWindowsF:=ExitWindowsNT(uFlags)
else ExitWindowsF:=ExitWindowsEx(uFlags,0);
end;

procedure CheckOS;
var
VerInfo : TOSVersionInfo;
begin
IsNT:=false;
VerInfo.dwOSVersionInfoSize:=sizeof(VerInfo);
if (GetVersionEx(VerInfo)) then
IsNT:=VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT;
end;

begin
CheckOS;
end.



useage :

uses Exitwin;

procedure TForm1.Button1Click(Sender: TObject);
begin
ExitWindowsF(EWX_POWEROFF or EWX_SHUTDOWN or EWX_FORCE);
end;
0
 
LVL 17

Expert Comment

by:inthe
ID: 6854651
note,
build the project save and close it and delphi then try running the exe .

(was just thinking that delphi may stop it from running if you run from inside the ide)
0
 

Author Comment

by:libbysharf
ID: 6927502
In the end I just used a program someone wrote me in C++, and called it within my application. Thanks anyway for the effort..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now