www.pudn.com > 兄弟变速器的源码.zip > SetClock.cpp


// File name : SetClock.cpp 
// Function1 : SetClock9x(int) 
// Function2 : SetClockNT(int) 
// Chu Rui 2001.3.1 
 
#include "stdafx.h" 
#include "ntport.h" 
 
#define FREE_INT_NO 5 
 
void Ring0() 
{	//在Windows9x下进入ring0后进行的操作 
	__asm 
	{ 
		cli 
		mov al,34h 
		out 43h,al	//写入8253控制寄存器,设置写0号定时器 
		mov ax,bx	 
		out 40h,al	//写定时值低位 
		mov al,ah 
		out 40h,al	//写定时值高位 
		sti 
		iretd; 
	} 
} 
 
void SetClockNT(int freq) 
{	//NT下的操作 
	//这里使用了NT Port库 
	Outport(0x43,0x34);	//写入8253控制寄存器,设置写0号定时器 
	Outport(0x40,freq&0xff);	//写定时值低位 
	Outport(0x40,(freq>>8)&0xff);	//写定时值高位 
} 
 
void SetClock9x(int freq) 
{ 
	union Function_Pointer 
	{ 
		void (*pointer)(); 
		char bytes[sizeof(void *)]; 
	}OldIntAddress,NewIntAddress; 
 
	int IDTAddress;		//IDT表基地址 
	int IDTItemAddress;	//要修改的中断门所在地址 
	char *Pointer;	//要修改的中断门所在地址,指针形式 
 
	__asm 
	{ 
		push eax 
		sidt [esp-2] 
		pop eax 
		mov IDTAddress,eax	//得到IDT表基地址 
	} 
	 
	IDTItemAddress=FREE_INT_NO*8+IDTAddress; 
	Pointer=(char *)IDTItemAddress; 
	NewIntAddress.pointer=Ring0; 
	 
	OldIntAddress.bytes[0]=Pointer[0]; 
	OldIntAddress.bytes[1]=Pointer[1]; 
	OldIntAddress.bytes[2]=Pointer[6]; 
	OldIntAddress.bytes[3]=Pointer[7];	//保存旧的中断门 
 
	Pointer[0]=NewIntAddress.bytes[0]; 
	Pointer[1]=NewIntAddress.bytes[1]; 
	Pointer[6]=NewIntAddress.bytes[2]; 
	Pointer[7]=NewIntAddress.bytes[3]; //设置新的中断门 
	 
	__asm 
	{ 
		mov ebx,freq 
		int FREE_INT_NO			//产生中断,进入ring0 
	} 
 
	Pointer[0]=OldIntAddress.bytes[0]; 
	Pointer[1]=OldIntAddress.bytes[1]; 
	Pointer[6]=OldIntAddress.bytes[2]; 
	Pointer[7]=OldIntAddress.bytes[3];	//恢复旧的中断门 
}