delphi1
asked on
Restart Windows?
In 16bit Delphi1 program, calling ExitWindows(EW_RestartWind ows, 0) will cause Windows to restart itself. The computer will not reboot.
In 32bit Delphi4 program , calling ExitWindows(EW_RestartWind ows, 0) can only logoff current user. Because ExitWindows is actually function resides in 16bit User.exe, so that cannot be called directly in 32bit programs. Following line is source code extracted from Delphi4 RTL source file Windows.pas:
function ExitWindows(dwReserved: DWORD; Code: Word): BOOL;
begin
Result := ExitWindowsEx(EWX_LOGOFF, 0);
end;
So, we can only turn to THUNK: calling 16bit DLL functions from 32bit program, Such as following code:
{$StackFrames On} //QT_Thunk needs a stack frame, Thunking call to 16-bit USER.EXE. The ThunkTrash argument allocates space on the stack for QT_Thunk
function LoadLibrary16(LibraryName: PChar): THandle; stdcall; external kernel32 index 35;
procedure FreeLibrary16(HInstance: THandle); stdcall; external kernel32 index 36;
function GetProcAddress16(Hinstance : THandle; ProcName: PChar): Pointer; stdcall; external kernel32 index 37;
procedure QT_Thunk; cdecl; external kernel32 name 'QT_Thunk';
var
hInst16: THandle;
GFSR: Pointer;
function RestartWindows: WordBool;
var
ThunkTrash: array[0..$20] of Word;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Result := False;
Exit;
end;
ThunkTrash[0] := hInst16; //Prevent the optimizer from getting rid of ThunkTrash
hInst16 := LoadLibrary16('user.exe');
if hInst16 < 32 then
raise Exception.Create('Cannot load USER.EXE');
FreeLibrary16(hInst16); //Decrement the usage count. This doesn't really free the library, since USER.EXE is always loaded
GFSR := GetProcAddress16(hInst16, 'ExitWindows'); //Get the function pointer for the 16-bit function in USER.EXE
if GFSR = nil then
raise Exception.Create('Cannot get address of ExitWindows');
asm //Thunk down to USER.EXE
push EW_RestartWindows { push arguments }
push 0
mov edx, GFSR { load 16-bit procedure pointer }
call QT_Thunk { call thunk }
mov Result, ax { save the result }
end;
end;
{$StackFrames Off}
Guess what happened. calling above RestartWindows function will cause computer be shutdown. The same function as:
ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0); I have tested it on several computer running Windows95 and Windows98.
I don't know what is wrong and why this happened. We know, in Windows95 shutdown dialog, select option "Reboot computer", then press Shift+Enter(or press Shift then click "Yes") , Windows DO restart itself. Obviously, the SHELL make it. but how?
In 32bit Delphi4 program , calling ExitWindows(EW_RestartWind
function ExitWindows(dwReserved: DWORD; Code: Word): BOOL;
begin
Result := ExitWindowsEx(EWX_LOGOFF, 0);
end;
So, we can only turn to THUNK: calling 16bit DLL functions from 32bit program, Such as following code:
{$StackFrames On} //QT_Thunk needs a stack frame, Thunking call to 16-bit USER.EXE. The ThunkTrash argument allocates space on the stack for QT_Thunk
function LoadLibrary16(LibraryName:
procedure FreeLibrary16(HInstance: THandle); stdcall; external kernel32 index 36;
function GetProcAddress16(Hinstance
procedure QT_Thunk; cdecl; external kernel32 name 'QT_Thunk';
var
hInst16: THandle;
GFSR: Pointer;
function RestartWindows: WordBool;
var
ThunkTrash: array[0..$20] of Word;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Result := False;
Exit;
end;
ThunkTrash[0] := hInst16; //Prevent the optimizer from getting rid of ThunkTrash
hInst16 := LoadLibrary16('user.exe');
if hInst16 < 32 then
raise Exception.Create('Cannot load USER.EXE');
FreeLibrary16(hInst16); //Decrement the usage count. This doesn't really free the library, since USER.EXE is always loaded
GFSR := GetProcAddress16(hInst16, 'ExitWindows'); //Get the function pointer for the 16-bit function in USER.EXE
if GFSR = nil then
raise Exception.Create('Cannot get address of ExitWindows');
asm //Thunk down to USER.EXE
push EW_RestartWindows { push arguments }
push 0
mov edx, GFSR { load 16-bit procedure pointer }
call QT_Thunk { call thunk }
mov Result, ax { save the result }
end;
end;
{$StackFrames Off}
Guess what happened. calling above RestartWindows function will cause computer be shutdown. The same function as:
ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0); I have tested it on several computer running Windows95 and Windows98.
I don't know what is wrong and why this happened. We know, in Windows95 shutdown dialog, select option "Reboot computer", then press Shift+Enter(or press Shift then click "Yes") , Windows DO restart itself. Obviously, the SHELL make it. but how?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Regards, Yang.