阅读:1312回复:2
Device IO的问题,大侠乱入
一个APP(比如一个路由程序),一个SYS(比如一个网络驱动)。
APP更新一次它维护的一段内存以后,通知SYS,SYS将这段内存拷贝到自己分配的一段内存中使用。如此反复。请问通过DEVICE_IO能完成这个操作么?如何实现?目前正在做,遇到不少困难,多谢各位帮助! |
|
最新喜欢:baoyib... |
沙发#
发布于:2003-05-29 14:45
完全可以,应用层调用deveiceiocontrol,sys处理IRP,
你先找个例子看看就明白了。 |
|
|
板凳#
发布于:2003-05-29 15:00
luo,又是你帮我,感动中。真是多谢多谢!
我已经潜水好几天看帖子,也自己写了一个小程序(大部分是拷贝的) 在APP中使用如下代码: HANDLE hDevice; DWORD dwDev; TCHAR szOutputBuffer[20]; PVOID psharememory; hDevice = CreateFile(\"\\\\\\\\.\\\\My_Passthru\", GENERIC_READ| GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if(hDevice == ((HANDLE)-1)) MyGetLastError(); if(!DeviceIoControl(hDevice, IOCTL_SHAREMEMORY_GETVADDR, NULL, NULL, szOutputBuffer, sizeof(szOutputBuffer), &dwDev, NULL )) { MyGetLastError(); return; } psharememory = *((PVOID *)szOutputBuffer); MessageBox((char *)psharememory, \"ShareMemory\", MB_OK); strcpy((char *)psharememory, \"Hi, ShareMemory!\"); if(!DeviceIoControl(hDevice, IOCTL_SHAREMEMORY_SHOWCONTENT, NULL, NULL, NULL, NULL, &dwDev, NULL )) MyGetLastError(); MessageBox((char *)psharememory, \"ShareMemory\", MB_OK); CloseHandle(hDevice); return; 、、、、、、、、、、、、、、、、、 在sys中我采用如下代码: 具体是在PassThru.c中: DbgPrint(\"begin sharing memory work...\"); RtlInitUnicodeString(&nameString, L\"\\\\Device\\\\My_Passthru\" ); status = IoCreateDevice(DriverObject, 0, &nameString, FILE_DEVICE_UNKNOWN, 0, FALSE, &deviceObject ); if (!NT_SUCCESS(status)) return status; deviceObject->Flags |= DO_BUFFERED_IO; RtlInitUnicodeString(&linkString, L\"\\\\??\\\\My_Passthru\"); status = IoCreateSymbolicLink (&linkString, &nameString); if (!NT_SUCCESS(status)) { IoDeleteDevice (DriverObject->DeviceObject); return status; } DriverObject->MajorFunction[IRP_MJ_CREATE] = MydrvDispatch; DriverObject->MajorFunction[IRP_MJ_CLOSE] = MydrvDispatch; DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = MydrvDispatchIoctl; SystemVirtualAddress = ExAllocatePool(NonPagedPool, 1024); Mdl = IoAllocateMdl(SystemVirtualAddress, 1024, FALSE, FALSE, NULL); MmBuildMdlForNonPagedPool(Mdl); strcpy(SystemVirtualAddress, \"In Driver\"); DbgPrint((char *)SystemVirtualAddress ); DbgPrint(\"\\n\"); return STATUS_SUCCESS; } static NTSTATUS MydrvDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { NTSTATUS status; PIO_STACK_LOCATION irpSp; // UNREFERENCED_PARAMETER(DeviceObject); //µÃµ½µ±Ç°IRP (I/OÇëÇó°ü) irpSp = IoGetCurrentIrpStackLocation(Irp); switch (irpSp->MajorFunction) { case IRP_MJ_CREATE: Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = 0L; break; case IRP_MJ_CLOSE: Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = 0L; break; case IRP_MJ_CLEANUP: MmUnmapLockedPages(UserVirtualAddress, Mdl); break; default: break; } IoCompleteRequest(Irp, IO_NO_INCREMENT); return STATUS_SUCCESS; static NTSTATUS MydrvDispatchIoctl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PIO_STACK_LOCATION IrpStack; NTSTATUS status; ULONG ControlCode; ULONG InputLength, OutputLength; TCHAR wInputBuffer[200]; TCHAR OutMsg[] = \"Hello, Application!\"; //µÃµ½µ±Ç°IRP (IOÇëÇó°ü) IrpStack = IoGetCurrentIrpStackLocation( Irp ); //µÃµ½DeviceIoControl´«À´µÄ¿ØÖÆÂë ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode; //µÃµ½DeviceIoControl´«À´µÄÊäÈ뻺³åÇø³¤¶È InputLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength; //µÃµ½DeviceIoControlµÄÊä³ö»º³åÇø³¤¶È OutputLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength; switch (ControlCode) { case IOCTL_SHAREMEMORY_GETVADDR: try { UserVirtualAddress = MmMapLockedPages(Mdl, UserMode); *((PVOID *)(Irp->AssociatedIrp.SystemBuffer)) = UserVirtualAddress; Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = sizeof(PVOID); } except(EXCEPTION_EXECUTE_HANDLER){} DbgPrint(\"in ioctl_sharememory_getvaddr.\"); break; case IOCTL_SHAREMEMORY_SHOWCONTENT: DbgPrint(\"in ioctl_sharememory_showcontent.\"); DbgPrint(\"Now the shared memory is : \"); DbgPrint((char *)SystemVirtualAddress ); DbgPrint(\"\\n\"); Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = 0L; break; default: break; } status = Irp->IoStatus.Status; //Íê³ÉIRPÇëÇó IoCompleteRequest(Irp, 0); return status; } 我运行的时候是这样: 在APP里点击一个按钮,我上面的代码开始执行。然后能弹出in driver和share memory的信息框,不过立刻softice弹出,是pagefault错误。然后应用程序就死掉了。请你看看有什么毛病? 实际上我对这些代码也不是很熟悉,没想到好不容易把收。发什么的弄好以后又出来这么个大头。你能劳烦给我讲讲么? |
|