关于驱动程序的问题

leftbackfielder 2013-09-02 11:41:40
刚刚接触到驱动程序,需要写一个键盘过滤的驱动,遇到一些问题,希望各位大侠帮忙解答一下.

驱动程序启动之后,附加到所有的键盘设备之上,然后,监测键盘输入
如果,键盘被拔出,再插入,这个时候,就监测不到输入了.
我添加了AddDevice例程,希望当键盘插入的时候,可以在这里面,重新附加,但是,这个例程却一直都没有
响应过.但是,拔出的时候,可以收到一个PNP的IRP,在里面,可以处理拔出操作
1.AddDevice需要满足什么条件才能响应?
2.在我写的驱动中,要实时监测键盘的插拔,能实现吗?
下面是一部分代码

NTSTATUS DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
{
ULONG i;
NTSTATUS status;
KdPrint (("c2p.SYS: entering DriverEntry\n"));

// 填写所有的分发函数的指针
for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
{
DriverObject->MajorFunction[i] = c2pDispatchGeneral;
}

DriverObject->DriverExtension->AddDevice = MyAddDevice;

// 单独的填写一个Read分发函数。因为要的过滤就是读取来的按键信息
// 其他的都不重要。这个分发函数单独写。
DriverObject->MajorFunction[IRP_MJ_READ] = c2pDispatchRead;

// 单独的填写一个IRP_MJ_POWER函数。这是因为这类请求中间要调用
// 一个PoCallDriver和一个PoStartNextPowerIrp,比较特殊。
DriverObject->MajorFunction [IRP_MJ_POWER] = c2pPower;

// 我们想知道什么时候一个我们绑定过的设备被卸载了(比如从机器上
// 被拔掉了?)所以专门写一个PNP(即插即用)分发函数
DriverObject->MajorFunction [IRP_MJ_PNP] = c2pPnP;

// 卸载函数。
DriverObject->DriverUnload = c2pUnload;
gDriverObject = DriverObject;
// 绑定所有键盘设备f
status = c2pAttachDevices(DriverObject, RegistryPath);

return status;
}

DRIVER_ADD_DEVICE MyAddDevice;

NTSTATUS MyAddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pPhysicalDevice)
{
KdPrint(("In MYAddDevice"));
NTSTATUS status = STATUS_SUCCESS;
PDEVICE_OBJECT pFilterDevice = NULL;
PDEVICE_OBJECT pLowerDevice = NULL;
// 创建过滤设备
/*IoCreateDevice(
IN DriverObject,
IN sizeof(C2P_DEV_EXT),
IN NULL,
IN pTargetDeviceObject->DeviceType,
IN pTargetDeviceObject->Characteristics,
IN FALSE,
OUT &pFilterDeviceObject
);*/

status = IoCreateDevice(DriverObject,sizeof(C2P_DEV_EXT),NULL,
pPhysicalDevice->DeviceType, pPhysicalDevice->Characteristics, FALSE, &pFilterDevice);

if(!NT_SUCCESS(status))
{
KdPrint(("-----Fail ==> KeyFilter::CY_KeyFilte::AddDevice->IoCreateDevice(%d)",status));
return status;
}

C2P_DEV_EXT *pDevExt = (C2P_DEV_EXT *)pFilterDevice->DeviceExtension;
pDevExt->NodeSize = sizeof(C2P_DEV_EXT);
pDevExt->pFilterDeviceObject = pFilterDevice;
pLowerDevice = IoAttachDeviceToDeviceStack(pFilterDevice, pPhysicalDevice); // 绑定
if(!pLowerDevice)
{
KdPrint(("-----Fail ==> KeyFilter::CY_KeyFilte::AddDevice->IoAttachDeviceToDeviceStack(%d)",status));
return STATUS_UNSUCCESSFUL;
}

pFilterDevice->Flags |= pLowerDevice->Flags & (DO_BUFFERED_IO | DO_DIRECT_IO | DO_POWER_PAGABLE) ;
pFilterDevice->Flags &= ~DO_DEVICE_INITIALIZING;
pDevExt->LowerDeviceObject = pLowerDevice;
pDevExt->TargetDeviceObject = pPhysicalDevice;

return status;
}

NTSTATUS c2pPnP(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
{
KdPrint(("PNP in\n"));
PC2P_DEV_EXT devExt;
PIO_STACK_LOCATION irpStack;
NTSTATUS status = STATUS_SUCCESS;
KIRQL oldIrql;
KEVENT event;

// 获得真实设备。
devExt = (PC2P_DEV_EXT)(DeviceObject->DeviceExtension);
irpStack = IoGetCurrentIrpStackLocation(Irp);

switch (irpStack->MinorFunction)
{
case IRP_MN_START_DEVICE:
KdPrint(("IDR_MN_START_DEVICE\n"));
IoSkipCurrentIrpStackLocation(Irp);
status = IoCallDriver(devExt->LowerDeviceObject, Irp);
break;
case IRP_MN_REMOVE_DEVICE:
KdPrint(("IRP_MN_REMOVE_DEVICE\n"));

// 首先把请求发下去
IoSkipCurrentIrpStackLocation(Irp);
IoCallDriver(devExt->LowerDeviceObject, Irp);
// 然后解除绑定。
IoDetachDevice(devExt->LowerDeviceObject);
// 删除我们自己生成的虚拟设备。
IoDeleteDevice(DeviceObject);
status = STATUS_SUCCESS;
break;

default:
// 对于其他类型的IRP,全部都直接下发即可。
KdPrint(("Other IRP"));
IoSkipCurrentIrpStackLocation(Irp);
status = IoCallDriver(devExt->LowerDeviceObject, Irp);
}
return status;
}

...全文
323 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
leftbackfielder 2013-09-19
  • 打赏
  • 举报
回复
引用 12 楼 jha334201553 的回复:
你可以百度搜索下国外写的 ctrl2cap 的代码
好的~
「已注销」 2013-09-13
  • 打赏
  • 举报
回复
你可以百度搜索下国外写的 ctrl2cap 的代码
leftbackfielder 2013-09-13
  • 打赏
  • 举报
回复
引用 9 楼 my_live_123 的回复:
在windows下还是linux下?
WINDOWS下的.
独孤的根号3 2013-09-10
  • 打赏
  • 举报
回复
当时老师说的一句原话:天下驱动一大抄,看你会抄不会抄。楼下认为呢
一根烂笔头 2013-09-10
  • 打赏
  • 举报
回复
在windows下还是linux下?
leftbackfielder 2013-09-09
  • 打赏
  • 举报
回复
引用 6 楼 u011717322 的回复:
我也是刚刚学驱动! 要识别插拔,可以加一个判断标示符!
加什么标识符.能留个联系方式吗?可以一起学习
To_be_sky 2013-09-09
  • 打赏
  • 举报
回复
引用 7 楼 leftbackfielder 的回复:
[quote=引用 6 楼 u011717322 的回复:] 我也是刚刚学驱动! 要识别插拔,可以加一个判断标示符!
加什么标识符.能留个联系方式吗?可以一起学习[/quote] 具体的程序我暂时也写不出来。我也只是写过串口的驱动。感觉应该差不多的。我也是初学者。 要不加我QQ吧:645078241 有问题可以一起探讨下。
To_be_sky 2013-09-05
  • 打赏
  • 举报
回复
我也是刚刚学驱动! 要识别插拔,可以加一个判断标示符!
leftbackfielder 2013-09-05
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
仅供参考: WM_DEVICECHANGE The WM_DEVICECHANGE device message notifies an application or device driver of a change to the hardware configuration of a device or the computer. Event = (UINT) wParam; dwData = (DWORD) lParam; Parameters Event Event type. This parameter can be one of the following values: Value Meaning DBT_CONFIGCHANGECANCELED A request to change the current configuration (dock or undock) has been canceled. DBT_CONFIGCHANGED The current configuration has changed, due to a dock or undock. DBT_DEVICEARRIVAL A device has been inserted and is now available. DBT_DEVICEQUERYREMOVE Permission is requested to remove a device. Any application can deny this request and cancel the removal. DBT_DEVICEQUERYREMOVEFAILED A request to remove a device has been canceled. DBT_DEVICEREMOVECOMPLETE A device has been removed. DBT_DEVICEREMOVEPENDING A device is about to be removed. Cannot be denied. DBT_DEVICETYPESPECIFIC A device-specific event has occurred. DBT_QUERYCHANGECONFIG Permission is requested to change the current configuration (dock or undock). DBT_USERDEFINED The meaning of this message is user-defined. dwData Address of a structure that contains event-specific data. Its meaning depends on the given event. Return Values Return TRUE to grant a requested action. Return BROADCAST_QUERY_DENY to deny a requested action. Remarks For devices that offer software-controllable features, such as ejection and locking, the system typically sends a DBT_DEVICEREMOVEPENDING message to let applications and device drivers end their use of the device gracefully. If the system forcibly removes a device, it may not send a DBT_DEVICEQUERYREMOVE message before doing so. DBT_CONFIGCHANGECANCELED, DBT_CONFIGCHANGED, DBT_DEVICEARRIVAL, DBT_DEVICEQUERYREMOVE, DBT_DEVICEQUERYREMOVEFAILED, DBT_DEVICEREMOVECOMPLETE, DBT_DEVICEREMOVEPENDING, DBT_DEVICETYPESPECIFIC, DBT_QUERYCHANGECONFIG, DBT_USERDEFINED QuickInfo Windows NT: Requires version 4.0 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winuser.h. See Also System Messages Overview, System Message Messages
恩.谢谢.这个是在上层获取USB设备的插拔.
leftbackfielder 2013-09-04
  • 打赏
  • 举报
回复
自己UP...
赵4老师 2013-09-04
  • 打赏
  • 举报
回复
仅供参考: WM_DEVICECHANGE The WM_DEVICECHANGE device message notifies an application or device driver of a change to the hardware configuration of a device or the computer. Event = (UINT) wParam; dwData = (DWORD) lParam; Parameters Event Event type. This parameter can be one of the following values: Value Meaning DBT_CONFIGCHANGECANCELED A request to change the current configuration (dock or undock) has been canceled. DBT_CONFIGCHANGED The current configuration has changed, due to a dock or undock. DBT_DEVICEARRIVAL A device has been inserted and is now available. DBT_DEVICEQUERYREMOVE Permission is requested to remove a device. Any application can deny this request and cancel the removal. DBT_DEVICEQUERYREMOVEFAILED A request to remove a device has been canceled. DBT_DEVICEREMOVECOMPLETE A device has been removed. DBT_DEVICEREMOVEPENDING A device is about to be removed. Cannot be denied. DBT_DEVICETYPESPECIFIC A device-specific event has occurred. DBT_QUERYCHANGECONFIG Permission is requested to change the current configuration (dock or undock). DBT_USERDEFINED The meaning of this message is user-defined. dwData Address of a structure that contains event-specific data. Its meaning depends on the given event. Return Values Return TRUE to grant a requested action. Return BROADCAST_QUERY_DENY to deny a requested action. Remarks For devices that offer software-controllable features, such as ejection and locking, the system typically sends a DBT_DEVICEREMOVEPENDING message to let applications and device drivers end their use of the device gracefully. If the system forcibly removes a device, it may not send a DBT_DEVICEQUERYREMOVE message before doing so. DBT_CONFIGCHANGECANCELED, DBT_CONFIGCHANGED, DBT_DEVICEARRIVAL, DBT_DEVICEQUERYREMOVE, DBT_DEVICEQUERYREMOVEFAILED, DBT_DEVICEREMOVECOMPLETE, DBT_DEVICEREMOVEPENDING, DBT_DEVICETYPESPECIFIC, DBT_QUERYCHANGECONFIG, DBT_USERDEFINED QuickInfo Windows NT: Requires version 4.0 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winuser.h. See Also System Messages Overview, System Message Messages
max_min_ 2013-09-02
  • 打赏
  • 举报
回复
硬件驱动版块 这里问问吧
leftbackfielder 2013-09-02
  • 打赏
  • 举报
回复
引用 1 楼 max_min_ 的回复:
硬件驱动版块 这里问问吧
之前就在那里问的,没有人回答.

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧