阅读:1486回复:11
贴出源码向各位高手问问题
现象:不能正确进行输入输出操作
问题:不知道下面代码有什么问题 先贴出头文件 // Driver.h // //Copyright (C) 2004.10.07 by XX // #pragma once #define TRANS_BYTE 1 #define TRANS_WORD 2 #define TRANS_DWORD 3 #define SINGLE_BYTE 4 #define SINGLE_WORD 5 #define SINGLE_DWORD 6 #define REG_BUF_BYTE 7 #define REG_BUF_WORD 8 #define REG_BUF_DWORD 9 #define REG_SINGLE_BYTE 10 #define REG_SINGLE_WORD 11 #define REG_SINGLE_DWORD 12 typedef unsigned short WORD; typedef unsigned char BYTE; extern "C" { #include <NTDDK.h> } #include "Unicode.h" typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDevice; ULONG DeviceNumber; CUString ustrDeviceName; // internal name CUString ustrSymLinkName; // external name PVOID deviceBuffer; // Reserve space for pointer to loopback buffer ULONG deviceBufferSize; } DEVICE_EXTENSION, *PDEVICE_EXTENSION; //读单个值 #define ReadChar( port ) \ ( READ_PORT_UCHAR((PUCHAR)port) ) #define ReadShort( port ) \ ( READ_PORT_USHORT((PUSHORT)port) ) #define ReadLong( port ) \ ( READ_PORT_ULONG((PULONG)port) ) #define ReadRegChar( port ) \ (READ_REGISTER_UCHAR((PUCHAR)port) ) #define ReadRegShort( port ) \ ( READ_REGISTER_USHORT((PUSHORT)port) ) #define ReadRegLong( port ) \ ( READ_REGISTER_ULONG((PULONG)port) ) //读缓冲 #define ReadBufferChar( port, data, len ) \ ( READ_PORT_BUFFER_UCHAR((PUCHAR)port, data, len) ) #define ReadBufferShort( port, data, len ) \ ( READ_PORT_BUFFER_USHORT((PUSHORT)port, data, len) ) #define ReadBufferLong( port, data, len ) \ ( READ_PORT_BUFFER_ULONG((PULONG)port, data, len) ) #define ReadRegBufferChar( port, data, len ) \ (READ_REGISTER_BUFFER_UCHAR((PUCHAR)port, data, len) ) #define ReadRegBufferShort( port, data, len ) \ ( READ_REGISTER_BUFFER_USHORT((PUSHORT)port, data, len) ) #define ReadRegBufferLong( port, data, len ) \ ( READ_REGISTER_BUFFER_ULONG((PULONG)port, data, len) ) //写单个值 #define WriteChar( port, data ) \ ( WRITE_PORT_UCHAR((PUCHAR)port, data) ) #define WriteShort( port, data ) \ ( WRITE_PORT_USHORT((PUSHORT)port, data) ) #define WriteLong( port, data ) \ ( WRITE_PORT_ULONG((PULONG)port, data) ) #define WriteRegChar( port, data ) \ (WRITE_REGISTER_UCHAR((PUCHAR)port, data) ) #define WriteRegShort( port, data ) \ ( WRITE_PORT_USHORT((PUSHORT)port, data) ) #define WriteRegLong( port, data ) \ ( WRITE_PORT_ULONG((PULONG)port, data) ) //写缓冲 #define WriteBufferChar( port, data, len ) \ ( WRITE_PORT_BUFFER_UCHAR((PUCHAR)port, data, len) ) #define WriteBufferShort( port, data, len ) \ ( WRITE_PORT_BUFFER_USHORT((PUSHORT)port, data, len) ) #define WriteBufferLong( port, data, len ) \ ( WRITE_PORT_BUFFER_ULONG((PULONG)port, data, len) ) #define WriteRegBufferChar( port, data, len ) \ (WRITE_REGISTER_BUFFER_UCHAR((PUCHAR)port, data, len) ) #define WriteRegBufferShort( port, data, len ) \ ( WRITE_PORT_BUFFER_USHORT((PUSHORT)port, data, len) ) #define WriteRegBufferLong( port, data, len ) \ ( WRITE_PORT_BUFFER_ULONG((PULONG)port, data, len) ) |
|
沙发#
发布于:2004-10-10 17:15
这里是实现文件
// // Driver.cpp // // Copyright (C) 2004.10.07 by XX // #include "Driver.h" // Forward declarations // static NTSTATUS CreateDevice( IN PDRIVER_OBJECT pDriverObject, IN ULONG DeviceNumber ); static VOID DriverUnload(IN PDRIVER_OBJECT pDriverObject); static NTSTATUS DispatchCreate( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); static NTSTATUS DispatchClose( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); static NTSTATUS DispatchWrite( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); static NTSTATUS DispatchRead( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); void WriteDatas(PDEVICE_EXTENSION pDevExt); void ReadDatas(PDEVICE_EXTENSION pDevExt, PUSHORT UserBuffer); //++ // Function: DriverEntry // // Description: // Initializes the driver, locating and claiming // hardware resources. Creates the kernel objects // needed to process I/O requests. // // Arguments: // pDriverObject - Passed from I/O Manager // pRegistryPath - UNICODE_STRING pointer to // registry info (service key) // for this driver // // Return value: // NTSTATUS signaling success or failure //-- extern "C" NTSTATUS DriverEntry( IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath ) { ULONG ulDeviceNumber = 0; NTSTATUS status; // If this driver controlled real hardware, // code would be placed here to locate it. // Using IoReportDetectedDevice, the ports, // IRQs, and DMA channels would be "marked" // as "in use" and under the control of this driver. // This Loopback driver has no HW, so... // Announce other driver entry points pDriverObject->DriverUnload = DriverUnload; // This includes Dispatch routines for Create, Write & Read pDriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate; pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose; pDriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchWrite; pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead; // For each physical or logical device detected // that will be under this Driver's control, // a new Device object must be created. status = CreateDevice( pDriverObject, ulDeviceNumber ); // This call would be repeated until all devices are created //ulDeviceNumber++; //status = // CreateDevice(pDriverObject, ulDeviceNumber); return status; } //++ // Function: CreateDevice // // Description: // Adds a new device // // Arguments: // pDriverObject - Passed from I/O Manager // ulDeviceNumber - Logical device number (zero-based) // // Return value: // None //-- NTSTATUS CreateDevice( IN PDRIVER_OBJECT pDriverObject, IN ULONG ulDeviceNumber ) { NTSTATUS status; PDEVICE_OBJECT pDevObj; PDEVICE_EXTENSION pDevExt; // Form the internal Device Name CUString devName( "\Device\PhokeIo" ); // for "loopback" device devName += CUString( ulDeviceNumber ); // Now create the device status = IoCreateDevice( pDriverObject, sizeof(DEVICE_EXTENSION), &(UNICODE_STRING)devName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevObj ); if (!NT_SUCCESS( status )) return status; // Announce that we will be working with a copy of the user's buffer pDevObj->Flags |= DO_BUFFERED_IO; // Initialize the Device Extension pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->pDevice = pDevObj; // back pointer pDevExt->DeviceNumber = ulDeviceNumber; pDevExt->ustrDeviceName = devName; pDevExt->deviceBuffer = NULL; pDevExt->deviceBufferSize = 0; // Form the symbolic link name CUString symLinkName( "\??\PhokeIoDrv" ); symLinkName += CUString(ulDeviceNumber + 1); // 1 based // Now create the link name status = IoCreateSymbolicLink( &(UNICODE_STRING)symLinkName, &(UNICODE_STRING)devName ); if (!NT_SUCCESS(status)) { // if it fails now, must delete Device object IoDeleteDevice( pDevObj ); return status; } pDevExt->ustrSymLinkName = symLinkName; // Made it return STATUS_SUCCESS; } //++ // Function: DriverUnload // // Description: // Stops & Deletes devices controlled by this driver. // Stops interrupt processing (if any) // Releases kernel resources consumed by driver // // Arguments: // pDriverObject - Passed from I/O Manager // // Return value: // None //-- VOID DriverUnload(IN PDRIVER_OBJECT pDriverObject) { PDEVICE_OBJECT pNextObj; // Loop through each device controlled by Driver pNextObj = pDriverObject->DeviceObject; while (pNextObj != NULL) { // Dig out the Device Extension from the // Device Object PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION) pNextObj->DeviceExtension; // Free up any buffer still held by this device if (pDevExt->deviceBuffer != NULL) { ExFreePool( pDevExt->deviceBuffer ); pDevExt->deviceBuffer = NULL; pDevExt->deviceBufferSize = 0; } // DevExt also holds the symbolic link name UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName; // ... which can now be deleted IoDeleteSymbolicLink( &pLinkName ); // a little trickery... // we need to delete the device object, BUT // the Device object is pointed to by pNextObj // If we delete the device object first, // we can't traverse to the next Device in the list // Rather than create another pointer, we can // use the DeviceExtension's back pointer to the device // So, first update the next pointer... pNextObj = pNextObj->NextDevice; // then delete the device using the Extension IoDeleteDevice( pDevExt->pDevice ); } // Finally, hardware that was allocated in DriverEntry // would be released here using // IoReportResourceUsage } //++ // Function: DispatchCreate // // Description: // Handles call from Win32 CreateFile request // For loopback driver, does nothing // // Arguments: // pDevObj - Passed from I/O Manager // pIrp - Passed from I/O Manager // // Return value: // NTSTATUS - success or failuer code //-- NTSTATUS DispatchCreate( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; // no bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); return STATUS_SUCCESS; } //++ // Function: DispatchClose // // Description: // Handles call from Win32 CreateHandle request // For loopback driver, frees any buffer // // Arguments: // pDevObj - Passed from I/O Manager // pIrp - Passed from I/O Manager // // Return value: // NTSTATUS - success or failuer code //-- NTSTATUS DispatchClose( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { // Dig out the Device Extension from the Device object PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; if (pDevExt->deviceBuffer != NULL) { ExFreePool( pDevExt->deviceBuffer ); pDevExt->deviceBuffer = NULL; pDevExt->deviceBufferSize = 0; }//end if pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; // no bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); return STATUS_SUCCESS; } //++ // Function: DispatchWrite // // Description: // Handles call from Win32 WriteFile request // For loopback driver, allocates new pool buffer // then xfers user buffer to pool buffer // // Arguments: // pDevObj - Passed from I/O Manager // pIrp - Passed from I/O Manager // // Return value: // NTSTATUS - success or failuer code //-- NTSTATUS DispatchWrite( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { NTSTATUS status = STATUS_SUCCESS; PVOID userBuffer; ULONG xferSize; // The stack location contains the user buffer info PIO_STACK_LOCATION pIrpStack = IoGetCurrentIrpStackLocation( pIrp ); // Dig out the Device Extension from the Device object PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; // Since we processing a new Write request, // free up any old buffer if (pDevExt->deviceBuffer != NULL) { ExFreePool( pDevExt->deviceBuffer ); pDevExt->deviceBuffer = NULL; pDevExt->deviceBufferSize = 0; } // Determine the length of the request xferSize = pIrpStack->Parameters.Write.Length; // Obtain user buffer pointer userBuffer = pIrp->AssociatedIrp.SystemBuffer; // Allocate the new buffer pDevExt->deviceBuffer = ExAllocatePool( PagedPool, xferSize ); if (pDevExt->deviceBuffer == NULL) { // buffer didn't allocate??? status = STATUS_INSUFFICIENT_RESOURCES; xferSize = 0; } else { // copy the buffer pDevExt->deviceBufferSize = xferSize; RtlCopyMemory( pDevExt->deviceBuffer, userBuffer, xferSize ); WriteDatas( pDevExt ); ExFreePool( pDevExt->deviceBuffer ); pDevExt->deviceBuffer = NULL; pDevExt->deviceBufferSize = 0; } // Now complete the IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = xferSize; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); pIrp = NULL; pDevExt = NULL; userBuffer = NULL; return status; } void WriteDatas(PDEVICE_EXTENSION pDevExt) { ULONG dwLen = 0; PVOID DataPtr = NULL; UCHAR TranType = 0; ULONG PortNo = 0x378; USHORT delLen = sizeof(USHORT) + sizeof(UCHAR); PUSHORT pShort = (PUSHORT)pDevExt->deviceBuffer; PortNo = (*pShort); pShort += 1; TranType = *((PUCHAR)pShort); DataPtr = (PUCHAR)pDevExt->deviceBuffer + delLen; switch (TranType) { case TRANS_BYTE://1 dwLen = pDevExt->deviceBufferSize - delLen; WriteBufferChar( PortNo, (PUCHAR)DataPtr, dwLen ); break; case TRANS_WORD://2 dwLen = (pDevExt->deviceBufferSize - delLen) / sizeof(USHORT); WriteBufferShort( PortNo, (PUSHORT)DataPtr, dwLen ); break; case TRANS_DWORD://3 dwLen = (pDevExt->deviceBufferSize - delLen) / sizeof(ULONG); WriteBufferLong( PortNo, (PULONG)DataPtr, dwLen ); break; case SINGLE_BYTE://4 WriteChar( PortNo, *((PUCHAR)DataPtr) ); break; case SINGLE_WORD://5 WriteShort( PortNo, *((PUSHORT)DataPtr) ); break; case SINGLE_DWORD://6 WriteLong( PortNo, *((PULONG)DataPtr) ); break; case REG_BUF_BYTE:// 7 dwLen = pDevExt->deviceBufferSize - delLen; WriteRegBufferChar( PortNo, (PUCHAR)DataPtr, dwLen ); break; case REG_BUF_WORD:// 8 dwLen = (pDevExt->deviceBufferSize - delLen) / sizeof(USHORT); WriteRegBufferShort( PortNo, (PUSHORT)DataPtr, dwLen ); break; case REG_BUF_DWORD:// 9 dwLen = (pDevExt->deviceBufferSize - delLen) / sizeof(ULONG); WriteRegBufferLong( PortNo, (PULONG)DataPtr, dwLen ); break; case REG_SINGLE_BYTE:// 10 WriteRegChar( PortNo, *((PUCHAR)DataPtr) ); break; case REG_SINGLE_WORD:// 11 WriteRegShort( PortNo, *((PUSHORT)DataPtr) ); break; case REG_SINGLE_DWORD:// 12 WriteRegLong( PortNo, *((PULONG)DataPtr) ); break; default: break; } pShort = NULL; DataPtr = NULL; } //++ // Function: DispatchRead // // Description: // Handles call from Win32 ReadFile request // For loopback driver, xfers pool buffer to user // // Arguments: // pDevObj - Passed from I/O Manager // pIrp - Passed from I/O Manager // // Return value: // NTSTATUS - success or failuer code //-- NTSTATUS DispatchRead( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { NTSTATUS status = STATUS_SUCCESS; PVOID userBuffer; ULONG xferSize; PUSHORT UserParameter; // The stack location contains the user buffer info PIO_STACK_LOCATION pIrpStack = IoGetCurrentIrpStackLocation( pIrp ); // Dig out the Device Extension from the Device object PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; // Determine the length of the request xferSize = pIrpStack->Parameters.Read.Length; // Obtain user buffer pointer userBuffer = pIrp->AssociatedIrp.SystemBuffer; UserParameter = (PUSHORT)pIrp->UserBuffer; if (pDevExt->deviceBuffer != NULL) { ExFreePool( pDevExt->deviceBuffer ); pDevExt->deviceBuffer = NULL; pDevExt->deviceBufferSize = 0; } // Allocate the new buffer pDevExt->deviceBuffer = ExAllocatePool( PagedPool, xferSize ); if (pDevExt->deviceBuffer == NULL) { // buffer didn't allocate??? status = STATUS_INSUFFICIENT_RESOURCES; xferSize = 0; } else { // copy the buffer pDevExt->deviceBufferSize = xferSize; ReadDatas( pDevExt, (PUSHORT)userBuffer ); RtlCopyMemory( userBuffer, pDevExt->deviceBuffer, xferSize ); ExFreePool( pDevExt->deviceBuffer ); pDevExt->deviceBuffer = NULL; pDevExt->deviceBufferSize = 0; } // Now complete the IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = xferSize; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); pIrp = NULL; pDevExt = NULL; userBuffer = NULL; UserParameter = NULL; return status; } void ReadDatas(PDEVICE_EXTENSION pDevExt, USHORT *UserBuffer) { ULONG dwLen; PVOID DataPtr; BYTE TranType; ULONG PortNo; PUCHAR pChar = (PUCHAR)pDevExt->deviceBuffer; PUSHORT pShort = (PUSHORT)pDevExt->deviceBuffer; PULONG pLong = (PULONG)pDevExt->deviceBuffer; PortNo = *UserBuffer; TranType = *((PUCHAR)(UserBuffer + 1)); switch (TranType) { case TRANS_BYTE://1 dwLen = pDevExt->deviceBufferSize; ReadBufferChar( PortNo, pChar, dwLen ); break; case TRANS_WORD://2 dwLen = pDevExt->deviceBufferSize / sizeof(USHORT); ReadBufferShort( PortNo, pShort, dwLen ); break; case TRANS_DWORD://3 dwLen = pDevExt->deviceBufferSize / sizeof(ULONG); ReadBufferLong( PortNo, pLong, dwLen ); break; case SINGLE_BYTE://4 *pChar = ReadChar( PortNo ); break; case SINGLE_WORD://5 *pShort = ReadShort( PortNo ); break; case SINGLE_DWORD://6 *pLong = ReadLong( PortNo ); break; case REG_BUF_BYTE:// 7 REGISTER dwLen = pDevExt->deviceBufferSize; ReadRegBufferChar( PortNo, pChar, dwLen ); break; case REG_BUF_WORD:// 8 dwLen = pDevExt->deviceBufferSize / sizeof(USHORT); ReadRegBufferShort( PortNo, pShort, dwLen ); break; case REG_BUF_DWORD:// 9 dwLen = pDevExt->deviceBufferSize / sizeof(ULONG); ReadRegBufferLong( PortNo, pLong, dwLen ); break; case REG_SINGLE_BYTE:// 10 *pChar = ReadRegChar( PortNo ); break; case REG_SINGLE_WORD:// 11 *pShort = ReadRegShort( PortNo ); break; case REG_SINGLE_DWORD:// 12 *pLong = ReadRegLong( PortNo ); break; default: break; } //UserBuffer = NULL; pChar = NULL; pShort = NULL; pLong = NULL; DataPtr = NULL; //pDevExt = NULL; } [编辑 - 10/11/04 by xx_focus] |
|
板凳#
发布于:2004-10-10 17:42
高手花点时间帮帮忙,谢了先
|
|
地板#
发布于:2004-10-11 13:13
devName symLinkName是否如下
CUString devName( "\\\\DeviceP\\\\hokeIo" ); // for "loopback" device CUString symLinkName( "\\\\??\\\\PhokeIoDrv" ); |
|
地下室#
发布于:2004-10-11 13:22
贴个完整的可编译的例子上来
|
|
|
5楼#
发布于:2004-10-11 14:54
这是我的源文件,已经作了一点小改动
下一个是操作这个驱动的文件 请多指点 |
|
|
6楼#
发布于:2004-10-11 14:55
这个是驱动操作文件
|
|
|
7楼#
发布于:2004-10-11 22:44
高手来看看啊!! :( :(
|
|
8楼#
发布于:2004-10-12 09:17
代码没法下载
|
|
|
9楼#
发布于:2004-10-12 10:04
用右键点击下载,是用falshget更好
|
|
10楼#
发布于:2004-10-12 21:27
简单看了下,有些问题,IOIRP的结构定义应用层和驱动层不一致,其次
void ReadDatas(PDEVICE_EXTENSION pDevExt, USHORT *UserBuffer)中的RtlCopyMemory( &myIrp, pDevExt->deviceBuffer, MYIRPSIZE );应该改为RtlCopyMemory( &myIrp, UserBuffer, MYIRPSIZE ); 参考我修改后的例子,我只测试了一个inp(0x71);其他的你自己修改下 |
|
|
11楼#
发布于:2004-10-12 22:49
wowocock
你好,谢谢你,你在百忙中挤出时间来为我解答问题实在感谢至极,我忘记从哪里给分了,等我找到在给,抱歉 |
|