Novell is now a part of Micro Focus

On Exiting Windows

Articles and Tips: tip

Steven Jones
Systems Research Engineer

01 Mar 1996

Have you ever wondered how installation programs can leave Windows, reboot the system, run DOS applications, and restart Windows? Sounds tough, right? Not anymore....

Here at Systems Research we often have the need to do such tasks, and on a massive scale. In the past, we have stooped to having a lab assistant or a desperate research engineer physically move from machine to machine, typing commands, restarting machines, and so on. But now we have found a new and easy answer to this problem, and we wanted to share it with you.

In the interest of time, we often use "higher-level" tools to develop our test benches, especially visual tools and event-driven tools such as Borland's Delphi and Microsoft's Visual Basic. Many of the test programs that monitor performance and capacity in our labs have been built with such tools. When we were recently looking to further automate our tests, we were delighted to stumble onto what turned out to be a "one-liner" that saves our staff hours of time.

The Windows API has two functions calls that offer the ability to leave and restart Windows: ExitWindows and ExitWindowsExec.

The ExitWindows function can restart Windows, terminate Windows and return control to MS-DOS, or terminate Windows and restart the system. Windows sends the WM_QUERYENDSESSION message to notify all applications that a request has been made to restart or terminate Windows. If all applications "agree" to terminate, Windows sends the WM_ENDSESSION message to all applications before terminating.

The ExitWindowsExec function terminates Windows, runs a specified MS-DOS application, and then restarts Windows.

Delphi offers easy access to the Windows API. No coding other than direct calls to the API are necessary. Borland provides two units for this purpose: WinProcs and WinTypes. The WinProcs unit defines function and procedure headers for the Windows API. Every routine provided by the standard Windows libraries can be accessed through WinProcs. The WinTypes unit defines the Delphi version of all the types used by the Windows API routines, including simple types and data structures (records) and all the standard Windows constants, including styles, messages, and flags. Together with the WinProcs unit, WinTypes defines the Delphi implementation of the Windows API.

Delphi generates code for each form that includes a Uses clause that includes a reference to WinTypes and WinProcs. Consequently, no code other than the Windows API function calls are necessary.

Declarations for the function calls are as follows:

function ExitWindowsExec(Exe: PChar; Params: PChar): Bool;

function ExitWindows(Reserved: LongInt; ReturnCode: Word): Bool;

Figure 1 shows some sample Delphi code that illustrates the use of the ExitWindows function. You will need some specifics on the parameters in order to know how to use the functions.

Figure 1: Sample code from the one-liner Delphi application.

unit Kill1;


  SysUtils, WinTypes, WinProcs, Messages, Classes,
 Graphics, Controls, Forms, Dialogs, StdCtrls;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);

    { Private declarations }
    { Public declarations }

  Form1: TForm1;


{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);


For the ExitWindows call, the ReturnCode specifies whether Windows should restart, terminate and return control to MS-DOS, or terminate and restart the system. The high-order word of this parameter should be zero. The low-order word specifies the return value to be passed to MS-DOS when Windows terminates. The low-order word can be one of the values listed in the first two rows of the following table:

The function returns zero if one or more applications refuses to terminate. The function does not return a value if all applications agree to be terminated.

On one occasion we had a need to not return to Windows, and we were curious to know if any other hexadecimal values for the ReturnCode would do the trick. We "shoved" a zero in for the ReturnCode parameter and it worked!

Our Delphi Application closes Windows and returns the machine to DOS. The only line of code that wasn't generated by Delphi is the call to the ExitWindows function. It is placed in the form's create event handler. As soon as the application loads and creates the form (if all running Window's applications respond appropriately), Windows is closed. We hope you find it useful.

* Originally published in Novell AppNotes


The origin of this information may be internal or external to Novell. While Novell makes all reasonable efforts to verify this information, Novell does not make explicit or implied claims to its validity.

© Copyright Micro Focus or one of its affiliates