部分代码由DDKWizard生成..代码风格跟我的不一样..可能看的有点蛋疼
请勿吐槽,小弟新手,正在学习,我相信一步一个脚印,可以走到理想
这个是应用程序部分
#include <WINDOWS.H> #include <winioctl.h> #include <STDIO.h> #define IOCTL_TEST1 CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) int main(){ HANDLE hDevice=CreateFile("\\\\.\\DRIVER12_DeviceName",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hDevice==INVALID_HANDLE_VALUE) { printf("fck! CreateFile() failed\n"); return -1; } DWORD dwOP; char *pBufferIn=(char*)malloc(10); memset(pBufferIn,0xAA,10); char *pBufferOut=(char*)malloc(10); memset(pBufferOut,0,10); printf("IOCTL_TEST1 %X\n",IOCTL_TEST1); DeviceIoControl(hDevice,IOCTL_TEST1,pBufferIn,10,pBufferOut,10,&dwOP,NULL); printf("实际返回大小%d\n",dwOP); for (int i=0;i<(int)dwOP;i++) printf("%X ",*(pBufferOut+i) & 0XFF); CloseHandle(hDevice); getchar(); return 0; }
下面是驱动部分
#define IOCTL_TEST1 CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) #define DEVICE_NAME "\\Device\\DRIVER12_DeviceName" #define SYMLINK_NAME "\\DosDevices\\DRIVER12_DeviceName" PRESET_UNICODE_STRING(usDeviceName, DEVICE_NAME); PRESET_UNICODE_STRING(usSymlinkName, SYMLINK_NAME);
NTSTATUS DRIVER12_DispatchDeviceControl(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp) { KdPrint(("DRIVER12_DispatchDeviceControl() hahahahah ternsoft.com")); NTSTATUS status = STATUS_SUCCESS; PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp); KdPrint(("IoControlCode %X",(irpSp->Parameters.DeviceIoControl.IoControlCode))); UCHAR *pInput=(UCHAR*)Irp->AssociatedIrp.SystemBuffer; switch(irpSp->Parameters.DeviceIoControl.IoControlCode) { // case IOCTL_DRIVER12_OPERATION: // // status = SomeHandlerFunction(irpSp); // break; case IOCTL_TEST1: KdPrint(("fck IOCTL_TEST1")); for (ULONG i=0;i<irpSp->Parameters.DeviceIoControl.InputBufferLength;i++){ KdPrint(("%X",pInput[i])); } RtlFillMemory(Irp->AssociatedIrp.SystemBuffer,10,0x3C); Irp->IoStatus.Status=status; Irp->IoStatus.Information=8;//设置实际可返回数据 break; default: Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST; Irp->IoStatus.Information = 0; break; } status = Irp->IoStatus.Status; IoCompleteRequest(Irp, IO_NO_INCREMENT); return status; }