緩衝區方式讀

來源:互聯網
上載者:User
// 驅動層代碼
#ifdef __cplusplusextern "C" {#endifNTSTATUS DriverEntry(    IN OUT PDRIVER_OBJECT   DriverObject,    IN PUNICODE_STRING      RegistryPath    ){    PDEVICE_OBJECT pdoDeviceObj = 0;    NTSTATUS status = STATUS_UNSUCCESSFUL;    pdoGlobalDrvObj = DriverObject;    // Create the device object.    if(!NT_SUCCESS(status = IoCreateDevice(        DriverObject,        0,        &usDeviceName,        FILE_DEVICE_UNKNOWN,        FILE_DEVICE_SECURE_OPEN,        FALSE,        &pdoDeviceObj        )))    {        // Bail out (implicitly forces the driver to unload).        return status;    };    // Now create the respective symbolic link object    if(!NT_SUCCESS(status = IoCreateSymbolicLink(        &usSymlinkName,        &usDeviceName        )))    {        IoDeleteDevice(pdoDeviceObj);        return status;    }pdoDeviceObj->Flags=DO_BUFFERED_IO;    // NOTE: You need not provide your own implementation for any major function that    //       you do not want to handle. I have seen code using DDKWizard that left the    //       *empty* dispatch routines intact. This is not necessary at all!    DriverObject->MajorFunction[IRP_MJ_CREATE] =IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_READ]=IRPTEST_DISPATCH_READ;DriverObject->MajorFunction[IRP_MJ_WRITE]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_CLEANUP]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_SHUTDOWN]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_CLOSE] = IRPTEST_Dispatch;    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IRPTEST_DispatchDeviceControl;    DriverObject->DriverUnload = IRPTEST_DriverUnload;    return STATUS_SUCCESS;}#ifdef __cplusplus}; // extern "C"#endif

NTSTATUS IRPTEST_DISPATCH_READ(PDEVICE_OBJECT pDeviceObject,PIRP pIrp){// 確保裝置的Flag子網域設定了DO_BUFFER_IO模式KdPrint(("Driver_Read\r\n"));__try{PIO_STACK_LOCATION pIOStack=IoGetCurrentIrpStackLocation(pIrp);ULONG ulReadLen=pIOStack->Parameters.Read.Length;// ReadFile傳遞的想要讀取的位元組數if (IRP_MJ_READ==pIOStack->MajorFunction){RtlFillMemory(pIrp->AssociatedIrp.SystemBuffer,ulReadLen,0xAA);pIrp->IoStatus.Information=ulReadLen;}}__except(EXCEPTION_EXECUTE_HANDLER){pIrp->IoStatus.Information=0;KdPrint(("Read派遣常式異常\r\n"));}pIrp->IoStatus.Status=STATUS_SUCCESS;IoCompleteRequest(pIrp,IO_NO_INCREMENT);KdPrint(("Driver_Read_end\r\n"));return STATUS_SUCCESS;}
// 應用程式層調用
int _tmain(int argc, _TCHAR* argv[]){HANDLE hDev=CreateFile(_T("\\\\.\\SYMLINK_IRPTEST"),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);if (INVALID_HANDLE_VALUE==hDev){printf("裝置控制代碼無效\r\n");system("pause");return -1;}UCHAR Buf[MAX_PATH]={0};DWORD dwReaded=0;if (ReadFile(hDev,Buf,20,&dwReaded,NULL)){for (int i=0;i<dwReaded;i++){printf("%08X\r\n",Buf[i]);}}elseprintf("資料讀取失敗\r\n");CloseHandle(hDev);system("pause");return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.