⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ave2kutil.c

📁 driver wdk
💻 C
字号:
//
//File name: ave2kutil.c
//This file contains some utilities
#include "ave2k.h"
#include "ave2kregs.h"
//This function stall current thread a give time in microsecond.
void ave2kDelay(ULONG delay)
{
   	LARGE_INTEGER Interval, Interval2;
	Interval.u.HighPart=0;
	Interval.u.LowPart=10*delay;
	//Interval = RtlLargeIntegerNegate(Interval); //Relative time
	KeQuerySystemTime(&Interval2);
	Interval2=RtlLargeIntegerAdd(Interval2, Interval);
	while(1){
		KeQuerySystemTime(&Interval);
		if(RtlLargeIntegerGreaterThan(Interval, Interval2))
			break;
	}
    //KeDelayExecutionThread(KernelMode,FALSE, &Interval);
}
void ave2kMicroDelay(PDEVICE_EXTENSION pDE, ULONG delay)
{
	ULONG i;
	delay*=30;
	if(delay<1)
		delay=1;
	for(i=0; i<delay; i++)
		Ave2kReadRegister(pDE, MC1);
}
NTSTATUS OpenFile(
	 OUT PHANDLE pFileHandle,
	 IN  PUNICODE_STRING fileName
	 )
{
   NTSTATUS ntStatus;
   IO_STATUS_BLOCK IoStatus;
//   HANDLE NtFileHandle;
   OBJECT_ATTRIBUTES ObjectAttributes;
//   ULONG LengthOfFile;
   WCHAR PathPrefix[] = L"\\SystemRoot\\system32\\drivers\\";
   UNICODE_STRING FullFileName;
   ULONG FullFileNameLength;
//   PVOID FileImage;

  // FILE_STANDARD_INFORMATION StandardInfo;

   //
   // Insert the correct path prefix.
   //

   FullFileNameLength = sizeof(PathPrefix) + fileName->MaximumLength;

   FullFileName.Buffer = ExAllocatePool(NonPagedPool,
                                       FullFileNameLength);

   if (FullFileName.Buffer == NULL) {
       ntStatus = STATUS_INSUFFICIENT_RESOURCES;
       return ntStatus;
   }

   FullFileName.Length = sizeof(PathPrefix) - sizeof(WCHAR);
   FullFileName.MaximumLength = (USHORT)FullFileNameLength;
   RtlMoveMemory (FullFileName.Buffer, PathPrefix, sizeof(PathPrefix));

   RtlAppendUnicodeStringToString (&FullFileName, fileName);
   InitializeObjectAttributes ( &ObjectAttributes,
                                &FullFileName,
                                OBJ_CASE_INSENSITIVE,
                                NULL,
                                NULL );

   ntStatus = ZwCreateFile( pFileHandle,
                            SYNCHRONIZE | FILE_READ_DATA,
                            &ObjectAttributes,
                            &IoStatus,
                            NULL,                          // alloc size = none
                            FILE_ATTRIBUTE_NORMAL,
                            FILE_SHARE_READ,
                            FILE_OPEN,
                            FILE_SYNCHRONOUS_IO_NONALERT,
                            NULL,  // eabuffer
                            0 );   // ealength

    if ( !NT_SUCCESS( ntStatus ) )
        ntStatus = STATUS_NO_SUCH_FILE;

   ExFreePool(FullFileName.Buffer);
   return ntStatus;
}

ULONG ave2kGetFileLength(
	  HANDLE FileHandle
	  )
{
   IO_STATUS_BLOCK IoStatus;
   OBJECT_ATTRIBUTES ObjectAttributes;
   NTSTATUS ntStatus;
   FILE_STANDARD_INFORMATION StandardInfo;
	if(FileHandle == NULL)
	   return 0;

   ntStatus = ZwQueryInformationFile( FileHandle,
                                      &IoStatus,
                                      &StandardInfo,
                                      sizeof(FILE_STANDARD_INFORMATION),
                                      FileStandardInformation );

   if (!NT_SUCCESS(ntStatus)) 
		return 0;
   return StandardInfo.EndOfFile.LowPart;
	
}

NTSTATUS ave2kReadFile(
		HANDLE FileHandle,
		PVOID Buffer,
		ULONG BufferLen
		)
{
   NTSTATUS ntStatus;
   IO_STATUS_BLOCK IoStatus;
   ntStatus = ZwReadFile( FileHandle,
                          NULL,
                          NULL,
                          NULL,
                          &IoStatus,
                          Buffer,
                          BufferLen,
                          NULL,
                          NULL );

   if( (!NT_SUCCESS(ntStatus)) || (IoStatus.Information != BufferLen) )
        ntStatus = STATUS_UNSUCCESSFUL;
   return ntStatus;
}

void PrintCurrentTime(const char *pPrompt)
{
	LARGE_INTEGER llTime;
	int nMin;
	int nSec;
	int nTime;
	KeQuerySystemTime(&llTime);
	nTime=llTime.LowPart;
	nTime/=10000;
	nSec=nTime/1000;
	nTime%=1000;
	nMin=nSec/60;
	nSec%=60;
	KdPrint(("%s-- %d:%d.%03d\r\n", pPrompt, nMin, nSec, nTime));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -