Solved

Reboot, Detect, Run Service

Posted on 2000-03-19
8
159 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create Database on Android via Delphi dbExpress 3 88
URL for downloading Google Chrome for Win XP 2 151
creating threads in delphi 1 101
Delphi inherited method 6 67
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

10 Experts available now in Live!

Get 1:1 Help Now