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

📄 readwrite.cpp

📁 一本在讲述USB驱动程式的书 及其范例原码
💻 CPP
字号:
// Read/Write request processors for fileio driver
// Copyright (C) 1999 by Walter Oney
// All rights reserved

#include "stddcls.h"
#include "driver.h"

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS DispatchCreate(PDEVICE_OBJECT fdo, PIRP Irp)
	{							// DispatchCreate
	PAGED_CODE();
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;

	PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);

	NTSTATUS status = STATUS_SUCCESS;
	InterlockedIncrement(&pdx->handles);
	return CompleteRequest(Irp, status, 0);
	}							// DispatchCreate

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS DispatchClose(PDEVICE_OBJECT fdo, PIRP Irp)
	{							// DispatchClose
	PAGED_CODE();
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
	InterlockedDecrement(&pdx->handles);
	return CompleteRequest(Irp, STATUS_SUCCESS, 0);
	}							// DispatchClose

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS StartDevice(PDEVICE_OBJECT fdo, PCM_PARTIAL_RESOURCE_LIST raw, PCM_PARTIAL_RESOURCE_LIST translated)
	{							// StartDevice
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	NTSTATUS status;

	// A likely time to want to perform file I/O operations is during configuration of
	// the device, in order to read a firmware file for downloading to the device. This
	// stage is problematic in Win98 because the init order for CONFIGMG is before that
	// of IFSMgr. The portable file I/O library solves that particular problem.

	HANDLE hfile;
	status = OpenFile(L"\\SystemRoot\\System32\\Drivers\\fileio.jnk", TRUE, &hfile);
	if (!NT_SUCCESS(status))
		{
		KdPrint((DRIVERNAME " - OpenFile failed - %X\n", status));
		return status;
		}

	PVOID buffer = NULL;

	__try
		{
		ULONG size = (ULONG) GetFileSize(hfile);
		if (!size)
			__leave;			// file is empty

		buffer = ExAllocatePool(PagedPool, size);
		if (!buffer)
			{
			KdPrint((DRIVERNAME " - Unable to allocate %d bytes for data buffer\n", size));
			status = STATUS_INSUFFICIENT_RESOURCES;
			__leave;
			}
		
		ULONG numread;
		status = ReadFile(hfile, buffer, size, &numread);

		if (!NT_SUCCESS(status))
			{
			KdPrint((DRIVERNAME " - ReadFile failed - %X\n", status));
			__leave;
			}

		KdPrint((DRIVERNAME " - ReadFile succeeded and read %d bytes\n", numread));

		// Preserve the data buffer for the associated test program

		pdx->RandomJunk = buffer;
		pdx->RandomJunkSize = numread;
		buffer = NULL;
		}
	__finally
		{
		CloseFile(hfile);
		if (buffer)
			ExFreePool(buffer);
		}

	return status;
	}							// StartDevice

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

VOID StopDevice(IN PDEVICE_OBJECT fdo, BOOLEAN oktouch /* = FALSE */)
	{							// StopDevice
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	if (pdx->RandomJunk)
		ExFreePool(pdx->RandomJunk);
	pdx->RandomJunk = NULL;
	}							// StopDevice

⌨️ 快捷键说明

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