[February , 22, 2008] Watcom C 的笔记7 Watcom C (7)

Watcom C 的笔记7 Watcom C (7)

		        判断是否在 Windows 下

    之前使用int 2Fh,ax=1600h 的方法已经失效;现在提出检测CR0 的Bit0 

(V86 enabled位)。在Windows的DOS box是 v86 mode。

#include <stdio.h>
#include <i86.h>

extern short isV86( void );
#pragma aux isV86 =       \
    "smsw  ax           ",      \
    "test  ax, 1        ",      \
    "jz    V86       ",      \
    "xor   ax, ax       ",      \
    "V86:            ",      \
    "mov   ax, 1        "       \
value [ ax ];

void main()
{
		if (isV86() ==0) 
					{printf("The program is running in pure DOS\n");}
		else 
					{printf("The program is running in Windows!\n");}	
}			

    实验表明,DOS下,上面的程序不好用,仍然会判断为在Windows下,因为DOS4GW

实现的也是要使得程序进入保护模式。

Function 0400H This function returns the version of DPMI services supported. Note that this
is not necessarily the version of any operating system that supports DPMI. It
should be used by programs to determine what calls are legal in the current
environment. Pass the following information:
AX = 0400H
The information returned is:
AH = Major version
AL = Minor version
BX = Flags Bit 0 = 1 if running under an 80386 DPMI implementation. Bit 1
= 1 if processor is returned to real mode for reflected interrupts (as
opposed to Virtual 8086 mode). Bit 2 = 1 if virtual memory is
supported. Bit 3 is reserved and undefined. All other bits are zero
and reserved for later use.
CL = Processor type
02 = 80286
03 = 80386
04 = 80486
05 = Pentium
DH = Current value of virtual master PIC base interrupt
DL = Current value of virtual slave PIC base interrupt
Carry flag clear (call cannot fail)

     返回值中的 BX Bit1 =1 表明返回到Real Mode下;Bit1 =0 表明返回的是V86模式下。

#include <stdio.h>
#include <i86.h>

void main()
{
		union	REGS r;

		r.w.ax=0x400;
		int386(0x31,&r,&r);

		if ((r.w.bx & 2)==0) 
					{printf("The program is in Windows!\n");}
		else 
					{{printf("The program is in DOS!\n");}}	
}			

     实验表明很好用~

						Z.t
						2008-1-20

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注