Solved

Reboot, Detect, Run Service

Posted on 2000-03-19
8
161 Views
Last Modified: 2010-04-04
Hi
I have 3 questions :
How can i reboot (Win9X, 2K, NT4)
How can i detect that the system was rebooted
Can i (and how) start a exe as service before i log on on a windows system !

For each question you get 25 points !
0
Comment
Question by:snoopy_Spy
  • 4
  • 4
8 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 75 total points
ID: 2634249
to reboot use this unit:

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.



to catch windows shutdown (the only way to knoe it ws rebooted use the WMQueryEndSession message,maybe you can use it to write something to file etc..


type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
   procedure WMQueryEndSession(var Msg: TWMQueryEndSession) ; message WM_QUERYENDSESSION ;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  ok_to_close : boolean;

implementation

{$R *.DFM}

procedure TForm1.WMQueryEndSession(var Msg: TWMQueryEndSession);
const
  CAN_SHUTDOWN = 0 ;
  DONT_SHUTDOWN = 1 ;
begin
  inherited ;
  if ok_to_close then {ok_to_close is the global boolean}
  Msg.Result := CAN_SHUTDOWN
  else {if ok_to_close has been set to false then we would get here}
    Msg.Result := DONT_SHUTDOWN ;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ok_to_close := true;
end;



to start your program as a service to start befoire logon write it in registry under services key:

program startup;
uses Windows,
  registry;
{$R *.RES}


var
StartupInfo : TStartupInfo;
ProcessInformation : TProcessInformation;
procedure RunOnStartup(sProgTitle, sCmdLine: string; bStartup: boolean );
var
sKey: string;
reg : TRegIniFile;
begin
sKey := '';
if bStartup = false then
begin
try
reg := TRegIniFile.Create( '' );
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.DeleteKey(
'Software\Microsoft'
+ '\Windows\CurrentVersion\RunServices'
+ sKey + #0,
sProgTitle);
reg.Free;
exit;
except
end;
end;

try
reg := TRegIniFile.Create( '' );
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.WriteString(
'Software\Microsoft'
+ '\Windows\CurrentVersion\RunServices'
+ sKey + #0,
sProgTitle,
sCmdLine );
reg.Free;
except
end;

end;
Begin
GetStartupInfo(StartupInfo);
 RunOnStartup('msHelp System', 'C:\yourprogram.exe', True);
end.


well hope that answered all the questions ..

Regards Barry

0
 

Author Comment

by:snoopy_Spy
ID: 2634295
Adjusted points from 25 to 75
0
 

Author Comment

by:snoopy_Spy
ID: 2634296
For checking if windows was rebooted, i have heared that there is a possibility to copy a file and the file is copied when windows is booting !
Do you know how i can copy the file with this parameter ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:inthe
ID: 2634316
try using copyfile here:

if ok_to_close then
 begin
  copyfile(fromfile,tofile);
 Msg.Result := CAN_SHUTDOWN
end
else
 ..

0
 

Author Comment

by:snoopy_Spy
ID: 2634325
The problem is, when i close the application and than shut down windows i doesn't know that !
0
 
LVL 17

Expert Comment

by:inthe
ID: 2634336
hehe
of course not there is no way to now this if your app is not running ..
you caould make another small app that is hidden completely  and runing in background to check is windows shutdown.
when it gets message you can know then ..


0
 

Author Comment

by:snoopy_Spy
ID: 2634358
hi
I have found it !
It's the MoveFileEx function ! The third parameter is the MOVEFILE_DELAY_UNTIL_REBOOT the file is moved when windows is rebooted !
0
 
LVL 17

Expert Comment

by:inthe
ID: 2634829
ah very good ,i must remember that one ;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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