Ben is a founder and partner in Spirit of Performance, a Harvard, Mass. firm that develops application performance and resource utilization software. He also designs and programs custom benchmarks of hardware and software. Ben Myers can be reached at MCI Mail ID 357-1400.
In its User's Guide for Microsoft Windows 3.0, Microsoft passes on some strong advice regarding hard disk utilities. For instance, page 54 states that you should exit Windows before running CHKDSK/F or any other utility that modifies hard disk allocation tables. If launched from within Windows, hard disk optimizing utilities and programs that change the interleave of a hard disk can wreak havoc with the disk. Then, too, TSR programs may not exactly be good for the health of a PC system when run from within the Windows environment.
Surprisingly, Microsoft does not provide clear and complete advice to software developers about how to prevent software from being started up within Windows. Appendix D of the Virtual Device Adaptation Guide, part of the Device Driver Kit (DDK), does explain how to detect whether Windows is running in enhanced mode. If you set the AX register to 1600h and make an interrupt multiplexer call, interrupt 2Fh, the returned values tell whether Windows is running in enhanced mode (see Table 1). Even a non-Windows application, such as a disk defragmenter or a TSR, can make this call to see if it has been launched from inside the enhanced Windows environment. However, this is only half of the battle, because there is nothing that tells you how to find out if Windows 3.0 is running in either real or standard mode in the first place.
Table 1: Values returned by calling int 2Fh with AX=1600h
AL{*} Value Meaning ---------------------------------------------------------------------- 00h or 80h Enhanced Windows 3.x or Windows/386, Version 2.xx is not running. 01h or FFh Windows/386 Version 2.xx is running. Anything else Enhanced Windows 3.x is running. {*}AL = Major Version Number of Windows
In our case, we didn't want users of our performance measurement package (Personal Measure) to run its TSR (PMEASURE) from within Windows in any mode; consequently we took the following approach.
First we used the DOS DEBUG program to uncover how Windows 3.0 behaved in real or standard mode. By first obtaining the interrupt 2Fh pointer from low DOS memory and then examining the code at the interrupt 2Fh handler inside Windows 3.0, it is easy to see what happens in both real and standard modes -- they both check for the same multiplexer value, 4680h, and return (see Table 2).
Table 2: Values returned by calling int 2Fh with AX=4680h
AX Value Meaning ----------------------------------------------------------- 00h Real or standard Windows 3.x is running. Anything else Real or standard Windows 3.x is not running.
Together, the interrupt multiplexer calls 1600h and 4680h provide a fairly certain way to see if a program is running within the Windows environment. At present, Microsoft support does not officially acknowledge that the multiplexer value 4680h exists, a la all of the undocumented DOS calls. And, of course, this mux call may not be available in future DOS releases.
The program WINTHERE (see Listing One) incorporates the two interrupt multiplexer calls to display a message that reflects the state of Windows as accurately as possible. An exit code of one from WINTHERE indicates that Windows is running. The 4680h multiplexer call is insufficient to tell apart real and standard Windows, which may be important for certain kinds of applications.
WINTHERE needs little explanation. Display is a utility macro that makes it easier to display messages on the PC console through DOS. The program looks for an operating system extension that responds to interrupt multiplexer value 1600h, then for one that answers to 4680h.
If you have a commercial TSR or hard disk utility product that must not run within Windows, it is easy to make it more foolproof: Simply insert some of the code from WINTHERE into the startup logic of the program, changing the messages displayed to suit your own conventions. By so doing, you will rest easier at night, knowing that users of your products are less likely to make errors that have unpredictable and possibly catastrophic effects.
Reference
Microsoft Windows Device Driver Adaptation Kit, Virtual Device Adaptation Guide, Version 3.00. Redmond, Wash.: Microsoft Corp., 1990.
_WINTHERE_ by Ben Myers
[LISTING ONE]
<a name="0057_0008"> page 58,132 title WINTHERE, A program to test for the presence of Windows 3.0 subttl (C)Copyright 1990 Spirit of Performance, Inc. ; All Rights Reserved. .list ; You may use any portion of this program for any purpose whatsoever, but ; you must include the above copyright in any program into which portions of ; this program are incorporated. ; Use Microsoft MASM 5.1 or later and Borland TLINK to build WINTHERE.COM. ; masm %1,%1.obj; ; tlink /x /t %1.obj,%1.com ; You may also use LINK and EXE2BIN to build WINTHERE.COM. MASM local ; reference operators @f, @b, and @@ are not handled correctly by Borland TASM. ; Equates used in this program Multiplexor equ 2Fh ; DOS multiplexor interrupt KbdIO equ 16h ; BIOS Keyboard interrupt DOS equ 21h ; DOS function call interrupt Terminate equ 4Ch ; DOS terminate function PrintString equ 09h ; DOS print string function CR equ 0dh ; Carriage Return. LF equ 0ah ; Line Feed. ; Simple macro to display a text string with the DOS print string function Display macro message local amsg,around mov dx,offset amsg ; Load offset of message mov ah,PrintString ; DOS function code int DOS jmp short around ; jump around message text amsg: .errb <message> ; generate assembler error if no message irp y,<message> ; repeat for each of y args in message list db y endm db '$' ; terminate message with '$' as required around: endm cseg segment public 'code' assume cs:cseg org 100h Begin: Display <"WINTHERE - (C)Copyright 1990 Spirit of Performance, Inc.",CR,LF> ; See if being executed from Windows 3.0 in enhanced mode. mov ax,1600h ; Enhanced Windows multiplex signature. int Multiplexor test al,7fh ; Windows 386? jnz Win_Enhanced ; Yes. ; See if being executed from Windows 3.0 in real or standard mode. mov ax,4680h ; Multiplex signature... int Multiplexor ; apparently when Win3 is not enhanced. or ax,ax ; Windows 3.0 /r or /s? jz @f ; Yes. jmp Not_Enhanced_Win ; No. @@: Display <"WINTHERE has been run from Windows real or standard mode.",CR,LF> jmp WrapUp Win_Enhanced: Display <"WINTHERE has been run from within Windows in enhanced mode.",CR,LF> WrapUp: Display <"Press any key to continue. . .",CR,LF> xor ah,ah ; Read a keystroke. int KbdIO or ah,ah ; Extended scan code? jnz @f ; No. int KbdIO ; Read second half of extended character. @@: mov ah,Terminate ; Quit. mov al,1 ; DOS exit code 1 to indicate error. int DOS Not_Enhanced_Win: Display <"WINTHERE has not been run from within MS Windows.",CR,LF> mov ah,Terminate ; Quit. xor al,al ; Exit code 0, no error. int DOS ; The interrupt mux call with ax=4680h is the one that Microsoft refuses to ; acknowledge, but it sure is there every time Windows is run in real or ; standard mode, and the mux interrupt vector points dead square in the middle ; of the Windows kernel, which then chains the mux interrupt elsewhere. cseg ends end Begin
Copyright © 1991, Dr. Dobb's Journal