Solved

Reboot, Detect, Run Service

Posted on 2000-03-19
8
157 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

18 Experts available now in Live!

Get 1:1 Help Now