windows驱动之WDF---CharSample 分类: windows驱动程序WDF 2015-01-09 08:34 384人阅读 评论(0) 收藏
2015-01-09 08:34
501 查看
驱动程序部分:
应用部分:对于习惯了ds编译的程序区别还是比较大的。
NTSTATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath ) /*++ Routine Description: DriverEntry initializes the driver and is the first routine called by the system after the driver is loaded. Parameters Description: DriverObject - represents the instance of the function driver that is loaded into memory. DriverEntry must initialize members of DriverObject before it returns to the caller. DriverObject is allocated by the system before the driver is loaded, and it is released by the system after the system unloads the function driver from memory. RegistryPath - represents the driver specific path in the Registry. The function driver can use the path to store driver related data between reboots. The path does not store hardware instance specific data. Return Value: STATUS_SUCCESS if successful, STATUS_UNSUCCESSFUL otherwise. --*/ { WDF_DRIVER_CONFIG config; NTSTATUS status; // // Initiialize driver config to control the attributes that // are global to the driver. Note that framework by default // provides a driver unload routine. If you create any resources // in the DriverEntry and want to be cleaned in driver unload, // you can override that by manually setting the EvtDriverUnload in the // config structure. In general xxx_CONFIG_INIT macros are provided to // initialize most commonly used members. // WDF_DRIVER_CONFIG_INIT(&config, CharSample_EvtDeviceAdd); // // Create a framework driver object to represent our driver. // status = WdfDriverCreate( DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, // Driver Attributes &config, // Driver Config Info WDF_NO_HANDLE // hDriver ); return status; }DeviceAdd也可以自定义了,呵呵
NTSTATUS CharSample_EvtDeviceAdd( IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit ) { NTSTATUS status; WDFDEVICE device; WDF_IO_QUEUE_CONFIG ioQueueConfig; //例程的首句PAGED_CODE,表示该例程的代码占用分页内存。 //只能在PASSIVE_LEVEL中断级别调用该例程,否则会蓝屏。 //如不说明,则占用系统的非分页内存,要珍惜使用。 PAGED_CODE(); //创建设备,没有对象属性和设备对象环境变量结构 status = WdfDeviceCreate(&DeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &device); if (!NT_SUCCESS(status)) { return status; } //初始化缺省队列配置,设置I/O请求分发处理方式为串行。 //对这个实例而言,选择串行或并行都可以,但不能选手工。 WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&ioQueueConfig, WdfIoQueueDispatchSequential); //设置EvtIoDeviceControl例程,处理应用程序的DeviceIoControl()函数调用 ioQueueConfig.EvtIoDeviceControl = CharSample_EvtIoDeviceControl; //创建队列 status = WdfIoQueueCreate(device, &ioQueueConfig, WDF_NO_OBJECT_ATTRIBUTES, NULL); if (!NT_SUCCESS(status)) { return status; } //创建设备GUID接口 status = WdfDeviceCreateDeviceInterface(device, (LPGUID) &CharSample_DEVINTERFACE_GUID, NULL); if (!NT_SUCCESS(status)) { } return status; }
VOID CharSample_EvtIoDeviceControl( IN WDFQUEUE Queue, IN WDFREQUEST Request, IN size_t OutputBufferLength, IN size_t InputBufferLength, IN ULONG IoControlCode ) { NTSTATUS status; PVOID buffer; CHAR n,c[]="零一二三四五六七八九"; PAGED_CODE(); switch(IoControlCode) { case CharSample_IOCTL_800: if (InputBufferLength == 0 || OutputBufferLength < 2) { //检查输入、输出参数有效性 WdfRequestComplete(Request, STATUS_INVALID_PARAMETER); } else { //输入缓冲区地址可通过调用WdfRequestRetrieveInputBuffer函数获得 //输出缓冲区地址可通过调用WdfRequestRetrieveOutputBuffer函数获得 //获取输入缓冲区地址buffer //要求1字节空间 status = WdfRequestRetrieveInputBuffer(Request, 1, &buffer, NULL); if (!NT_SUCCESS(status)) { WdfRequestComplete(Request, STATUS_UNSUCCESSFUL); break; } //这里buffer表示输入缓冲区地址 //输入n=应用程序传给驱动程序的数字ASCII码 n = *(CHAR *)buffer; if ((n>='0') && (n<='9')) { //若为数字,则处理 n-='0'; //n=数字(0-9) //获取输出缓冲区地址buffer status = WdfRequestRetrieveOutputBuffer(Request, 2, &buffer, NULL); if (!NT_SUCCESS(status)) { WdfRequestComplete(Request, STATUS_UNSUCCESSFUL); break; } //这里buffer表示输出缓冲区地址 //输出:从中文数组c[]中取出对应的数字的中文码,拷贝到输出缓冲区 strncpy((PCHAR)buffer,&c[n*2],2); //完成I/O请求,驱动程序传给应用程序的数据长度为2字节(一个中文) WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, 2); } else //否则返回无效参数 WdfRequestComplete(Request, STATUS_INVALID_PARAMETER); } break; default : status = STATUS_INVALID_DEVICE_REQUEST; WdfRequestCompleteWithInformation(Request, status, 0); break; } return; }
应用部分:对于习惯了ds编译的程序区别还是比较大的。
DevicePath = GetDevicePath((LPGUID)&CharSample_DEVINTERFACE_GUID); hDevice = CreateFile(DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); if (hDevice == INVALID_HANDLE_VALUE) { printf("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError()); return 0; } printf("OK.\n"); CHAR bufInput[1]; // Input to device CHAR bufOutput[2]; // Output from device ULONG nOutput; // Count written to bufOutput printf("请输入数字(0-9)\n"); l0: bufInput[0] = _getch(); if ((bufInput[0]<'0') || (bufInput[0]>'9')) goto l0; _putch(bufInput[0]); // Call device IO Control interface (CharSample_IOCTL_800) in driver if (!DeviceIoControl(hDevice, CharSample_IOCTL_800, bufInput, 1, bufOutput, 2, &nOutput, NULL) ) { printf("ERROR: DeviceIoControl returns %0x.", GetLastError()); goto exit; }
PCHAR GetDevicePath( IN LPGUID InterfaceGuid ) { HDEVINFO HardwareDeviceInfo; SP_DEVICE_INTERFACE_DATA DeviceInterfaceData; PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData = NULL; ULONG Length, RequiredLength = 0; BOOL bResult; HardwareDeviceInfo = SetupDiGetClassDevs( InterfaceGuid, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); if (HardwareDeviceInfo == INVALID_HANDLE_VALUE) { printf("SetupDiGetClassDevs failed!\n"); exit(1); } DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); bResult = SetupDiEnumDeviceInterfaces(HardwareDeviceInfo, 0, InterfaceGuid, 0, &DeviceInterfaceData); if (bResult == FALSE) { printf("SetupDiEnumDeviceInterfaces failed.\n"); SetupDiDestroyDeviceInfoList(HardwareDeviceInfo); exit(1); } SetupDiGetDeviceInterfaceDetail( HardwareDeviceInfo, &DeviceInterfaceData, NULL, 0, &RequiredLength, NULL ); DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LMEM_FIXED, RequiredLength); if (DeviceInterfaceDetailData == NULL) { SetupDiDestroyDeviceInfoList(HardwareDeviceInfo); printf("Failed to allocate memory.\n"); exit(1); } DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); Length = RequiredLength; bResult = SetupDiGetDeviceInterfaceDetail( HardwareDeviceInfo, &DeviceInterfaceData, DeviceInterfaceDetailData, Length, &RequiredLength, NULL); if (bResult == FALSE) { printf("Error in SetupDiGetDeviceInterfaceDetail\n"); SetupDiDestroyDeviceInfoList(HardwareDeviceInfo); LocalFree(DeviceInterfaceDetailData); exit(1); } return DeviceInterfaceDetailData->DevicePath; }
相关文章推荐
- WDF驱动的编译、调试、安装 分类: windows驱动程序WDF 2015-01-09 08:53 457人阅读 评论(0) 收藏
- DriverStudio 和 WDF驱动 通过GUID获取设备句柄的差别 分类: windows驱动程序WDM 2013-10-30 08:56 812人阅读 评论(0) 收藏
- 在驱动和应用程序间共享内存 分类: windows驱动程序WDM 2013-09-25 14:37 650人阅读 评论(0) 收藏
- 用DDK开发的9054驱动 . 分类: windows驱动程序WDM 2013-09-14 17:24 625人阅读 评论(0) 收藏
- Windows驱动开发工具 WDK 学习笔记(1) 分类: windows驱动程序WDM 2013-08-01 11:08 566人阅读 评论(0) 收藏
- USB设备驱动概述 分类: USB OTG驱动 windows驱动程序WDM 2015-06-10 18:15 426人阅读 评论(0) 收藏
- 过滤驱动加密文件(代码) 分类: windows驱动程序WDM 2013-09-25 14:40 611人阅读 评论(0) 收藏
- windows驱动程序wdf--KMDF获取应用程序数据缓冲区地址 分类: windows驱动程序WDM 2015-01-04 16:19 353人阅读 评论(0) 收藏
- 一种基于主板BIOS的身份认证方案及实现 分类: arm-linux-Ubuntu windows驱动程序WDF 2015-05-11 11:02 372人阅读 评论(1) 收藏
- windows驱动程序wdf--KMDF大致框架 分类: windows驱动程序WDM 2015-01-04 16:18 338人阅读 评论(0) 收藏
- 使用DirectDraw直接显示YUV视频数据 分类: windows驱动程序WDM 2013-08-15 11:44 1075人阅读 评论(0) 收藏
- MFC下用sdl 显示bmp、rgb、yuv 分类: windows驱动程序WDM VC++ ffmpeg-SDL-VLC-Live555 DirectX 2013-08-16 18:11 1114人阅读 评论(0) 收藏
- 文件监控(教学版) 分类: windows驱动程序WDM 2013-09-25 14:41 666人阅读 评论(0) 收藏
- 对内核的直接挂钩 分类: windows驱动程序WDM 2013-09-25 14:50 377人阅读 评论(0) 收藏
- Windows NT 驱动程序开发人员提示 -- 应注意避免的事项 分类: windows驱动程序WDM 2013-09-25 14:38 490人阅读 评论(0) 收藏
- xp+WinDBG+VMware调试内核 分类: windows驱动程序WDM 2013-09-25 14:43 431人阅读 评论(0) 收藏
- RTP 协议 分类: arm-linux-Ubuntu windows驱动程序WDM 2013-08-16 10:53 442人阅读 评论(0) 收藏
- 实战DeviceIoControl 之六:访问物理端口 分类: windows驱动程序WDM 2013-09-25 14:49 332人阅读 评论(0) 收藏
- 如何构造一个简单的USB过滤驱动程序 分类: windows驱动程序WDM 2013-09-25 14:45 477人阅读 评论(0) 收藏
- Windows PowerShell漫谈-win7下没有超级终端 分类: windows驱动程序WDM 2013-07-22 16:30 684人阅读 评论(0) 收藏