高精度延时
VOID USleep(LONGLONG us)
{
LARGE_INTEGER start, sFreq, now, nFreq;
LONGLONG sTime, nTime;
start = KeQueryPerformanceCounter(&sFreq);
sTime = (start.QuadPart * 1000000) / sFreq.QuadPart;
do {
now = KeQueryPerformanceCounter(&nFreq);
nTime = (now.QuadPart * 1000000) / nFreq.QuadPart;
} while (nTime - sTime < us);
}
低精度延时
VOID UDelay(ULONG us)
{
LARGE_INTEGER timeOut = RtlConvertLongToLargeInteger(-10*us);
KeDelayExecutionThread(KernelMode, FALSE, &timeOut);
}
VOID UDelay(ULONG us)
{
KEVENT kEvent;
KeInitializeEvent(&kEvent, SynchronizationEvent, FALSE);
LARGE_INTEGER timeOut = RtlConvertLongToLargeInteger(-10*us);
KeWaitForSingleObject(&kEvent, Executive, KernelMode,FALSE,&timeOut);
}
// KeWaitForSingleObject例程将当前线程置为等待状态,
// 直到提供的分发器对象被设置为已处理状态,或者第五个参数给出的时间Timeout走完
VOID UDelay(ULONG us)
{
KTIMER kTimer;
KeInitializeTimer(&kTimer);
LARGE_INTEGER timeOut = RtlConvertLongToLargeInteger(-10*us);
KeSetTimer(&kTimer, timeOut, NULL);
KeWaitForSingleObject(&kTimer, Executive, KernelMode,FALSE, NULL);
}