給個usb的DriverEntry範例
时间: 2023-11-19 18:04:34 浏览: 164
以下是一个简单的 USB 驱动程序的 DriverEntry 函数示例:
```c
#include <ntddk.h>
#include <usb.h>
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
NTSTATUS status = STATUS_SUCCESS;
PDEVICE_OBJECT deviceObject = NULL;
UNICODE_STRING deviceName;
UNICODE_STRING symbolicLinkName;
RtlInitUnicodeString(&deviceName, L"\\Device\\MyUSBDevice");
RtlInitUnicodeString(&symbolicLinkName, L"\\DosDevices\\MyUSBDevice");
status = IoCreateDevice(DriverObject,
0,
&deviceName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&deviceObject);
if (NT_SUCCESS(status))
{
status = IoCreateSymbolicLink(&symbolicLinkName, &deviceName);
if (NT_SUCCESS(status))
{
// 在这里进行 USB 初始化操作,例如打开 USB 控制器、初始化 USB 栈等
// ...
// 注册 USB 设备接口
USB_DEVICE_DESCRIPTOR deviceDescriptor;
USBD_INTERFACE_LIST_ENTRY interfaceList[2];
interfaceList[0].InterfaceClassGuid = GUID_CLASS_USB_DEVICE;
interfaceList[0].InterfaceReference = NULL;
interfaceList[0].InterfaceStructSize = sizeof(USBD_INTERFACE_LIST_ENTRY);
interfaceList[0].InterfaceSubClass = 0;
interfaceList[0].InterfaceProtocol = 0;
interfaceList[0].Reserved = 0;
interfaceList[1].InterfaceClassGuid = GUID_NULL;
interfaceList[1].InterfaceReference = NULL;
interfaceList[1].InterfaceStructSize = sizeof(USBD_INTERFACE_LIST_ENTRY);
interfaceList[1].InterfaceSubClass = 0;
interfaceList[1].InterfaceProtocol = 0;
interfaceList[1].Reserved = 0;
USB_DEVICE_CREATE_PARAMS createParams;
RtlZeroMemory(&createParams, sizeof(createParams));
createParams.SelfSize = sizeof(createParams);
createParams.Version = USB_DEVICE_CREATE_PARAMS_VERSION_1;
createParams.DeviceDescriptor = &deviceDescriptor;
createParams.InterfaceList = interfaceList;
createParams.MiniportDriverResources = NULL;
createParams.MiniportDriverResourcesLength = 0;
// 填充设备描述符
deviceDescriptor.bLength = sizeof(deviceDescriptor);
deviceDescriptor.bDescriptorType = USB_DEVICE_DESCRIPTOR_TYPE;
deviceDescriptor.bcdUSB = 0x0200;
deviceDescriptor.bDeviceClass = 0;
deviceDescriptor.bDeviceSubClass = 0;
deviceDescriptor.bDeviceProtocol = 0;
deviceDescriptor.bMaxPacketSize0 = 64;
deviceDescriptor.idVendor = 0x1234;
deviceDescriptor.idProduct = 0x5678;
deviceDescriptor.bcdDevice = 0x0100;
deviceDescriptor.iManufacturer = 0;
deviceDescriptor.iProduct = 0;
deviceDescriptor.iSerialNumber = 0;
deviceDescriptor.bNumConfigurations = 1;
status = IoRegisterDeviceInterface(deviceObject,
&GUID_CLASS_USB_DEVICE,
NULL,
&createParams.InterfaceList[0].InterfaceClassGuid);
if (NT_SUCCESS(status))
{
// 在这里进行 USB 设备初始化操作,例如打开 USB 设备、分配 USB 端点等
// ...
// 注册 I/O 处理程序
DriverObject->MajorFunction[IRP_MJ_CREATE] = MyUsbCreate;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = MyUsbClose;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = MyUsbDeviceControl;
}
else
{
IoDeleteSymbolicLink(&symbolicLinkName);
IoDeleteDevice(deviceObject);
}
}
else
{
IoDeleteDevice(deviceObject);
}
}
return status;
}
```
需要注意的是,这只是一个简单的示例,实际的 USB 驱动程序可能需要更复杂的初始化和设备管理操作。此外,还需要实现 MyUsbCreate、MyUsbClose 和 MyUsbDeviceControl 等 I/O 处理程序来处理设备请求。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)