阅读:4206回复:29
我实在是很急,高分奉送!
我的程序是在2000DDK中kbfiltr基础上进行修改的(改得不多),可以CreateFile()取得驱动handle,但始终无法用DeviceIoControl()与驱动取得联系,我仔细看了看实在发现不了任何问题,烦请哪位高手帮我看看代码,我愿把我所有分奉上!有意者请留下E-mail,我把代码给您发过去。
|
|
最新喜欢:yeaipi... |
沙发#
发布于:2002-08-02 13:05
仔细描述一下
用softice跟一下,看deviceiocontrol传下去的ioctrl码对不对 如果是这个问题,我有解决方法 |
|
|
板凳#
发布于:2002-08-02 13:09
softice我不大会用,能不能麻烦你帮我看看程序,我已经精疲力竭了,搞了好几天了,拜托您!
|
|
地板#
发布于:2002-08-02 13:10
那好吧
把你的ioctl定义贴上来看看 |
|
|
地下室#
发布于:2002-08-02 13:17
#define IOCTL_LOCK_SYSTEMKEY \\
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_NEITHER, FILE_ANY_ACCESS) #define IOCTL_UNLOCK_SYSTEMKEY \\ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_NEITHER, FILE_ANY_ACCESS) #include \"kbfiltr.h\" NTSTATUS DriverEntry (PDRIVER_OBJECT, PUNICODE_STRING); #ifdef ALLOC_PRAGMA #pragma alloc_text (INIT, DriverEntry) #pragma alloc_text (PAGE, KbFilter_AddDevice) #pragma alloc_text (PAGE, KbFilter_CreateClose) #pragma alloc_text (PAGE, KbFilter_IoCtl) #pragma alloc_text (PAGE, KbFilter_InternIoCtl) #pragma alloc_text (PAGE, KbFilter_Unload) #pragma alloc_text (PAGE, KbFilter_DispatchPassThrough) #pragma alloc_text (PAGE, KbFilter_PnP) #pragma alloc_text (PAGE, KbFilter_Power) #endif NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) { ULONG k; UNREFERENCED_PARAMETER (RegistryPath); for (k = 0; k < IRP_MJ_MAXIMUM_FUNCTION; k++) { DriverObject->MajorFunction[k] = KbFilter_DispatchPassThrough; } DriverObject->MajorFunction [IRP_MJ_CREATE] = DriverObject->MajorFunction [IRP_MJ_CLOSE] = KbFilter_CreateClose; DriverObject->MajorFunction [IRP_MJ_PNP] = KbFilter_PnP; DriverObject->MajorFunction [IRP_MJ_POWER] = KbFilter_Power; DriverObject->MajorFunction [IRP_MJ_INTERNAL_DEVICE_CONTROL] = KbFilter_InternIoCtl; //--------------------------------------------------------------------------- DriverObject->MajorFunction [IRP_MJ_DEVICE_CONTROL] = KbFilter_IoCtl; DbgPrint(\"KbFilter_IoCtl\\n\"); //--------------------------------------------------------------------------- DriverObject->DriverUnload = KbFilter_Unload; DriverObject->DriverExtension->AddDevice = KbFilter_AddDevice; return STATUS_SUCCESS; } NTSTATUS KbFilter_AddDevice(IN PDRIVER_OBJECT Driver, IN PDEVICE_OBJECT PDO) { PDEVICE_EXTENSION devExt; IO_ERROR_LOG_PACKET errorLogEntry; PDEVICE_OBJECT device; NTSTATUS status = STATUS_SUCCESS; UNICODE_STRING uszDriverString; UNICODE_STRING uszDeviceString; PAGED_CODE(); RtlInitUnicodeString(&uszDriverString, L\"\\\\Device\\\\Kbfilter\"); status = IoCreateDevice(Driver, sizeof(DEVICE_EXTENSION), &uszDriverString, FILE_DEVICE_KEYBOARD, 0, FALSE, &device ); if (!NT_SUCCESS(status)) { return (status); } RtlZeroMemory(device->DeviceExtension, sizeof(DEVICE_EXTENSION)); devExt = (PDEVICE_EXTENSION) device->DeviceExtension; devExt->TopOfStack = IoAttachDeviceToDeviceStack(device, PDO); ASSERT(devExt->TopOfStack); devExt->Self = device; devExt->PDO = PDO; devExt->DeviceState = PowerDeviceD0; devExt->SurpriseRemoved = FALSE; devExt->Removed = FALSE; devExt->Started = FALSE; device->Flags |= (DO_BUFFERED_IO | DO_POWER_PAGABLE); device->Flags &= ~DO_DEVICE_INITIALIZING; RtlInitUnicodeString(&uszDeviceString, L\"\\\\DosDevices\\\\Kbfilter\"); status = IoCreateSymbolicLink(&uszDeviceString, &uszDriverString); if(status != STATUS_SUCCESS) { IoDeleteDevice(device); return status; } DbgPrint(\"Kbfilter:AddDevice\\n\"); return status; } NTSTATUS KbFilter_Complete(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context) { PKEVENT event; event = (PKEVENT) Context; UNREFERENCED_PARAMETER(DeviceObject); UNREFERENCED_PARAMETER(Irp); KeSetEvent(event, 0, FALSE); return STATUS_MORE_PROCESSING_REQUIRED; } NTSTATUS KbFilter_CreateClose (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PIO_STACK_LOCATION irpStack; NTSTATUS status; PDEVICE_EXTENSION devExt; PAGED_CODE(); irpStack = IoGetCurrentIrpStackLocation(Irp); devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension; status = Irp->IoStatus.Status; switch (irpStack->MajorFunction) { case IRP_MJ_CREATE: if (NULL == devExt->UpperConnectData.ClassService) { status = STATUS_INVALID_DEVICE_STATE; } else if ( 1 == InterlockedIncrement(&devExt->EnableCount)) { } else { } break; case IRP_MJ_CLOSE: if (0 == InterlockedDecrement(&devExt->EnableCount)) { } break; } Irp->IoStatus.Status = status; return KbFilter_DispatchPassThrough(DeviceObject, Irp); } NTSTATUS KbFilter_DispatchPassThrough(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp); IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(((PDEVICE_EXTENSION) DeviceObject->DeviceExtension)->TopOfStack, Irp); } NTSTATUS KbFilter_InternIoCtl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PIO_STACK_LOCATION irpStack; PDEVICE_EXTENSION devExt; PINTERNAL_I8042_HOOK_KEYBOARD hookKeyboard; KEVENT event; PCONNECT_DATA connectData; NTSTATUS status = STATUS_SUCCESS; devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension; Irp->IoStatus.Information = 0; irpStack = IoGetCurrentIrpStackLocation(Irp); switch (irpStack->Parameters.DeviceIoControl.IoControlCode) { // // Connect a keyboard class device driver to the port driver. // case IOCTL_INTERNAL_KEYBOARD_CONNECT: // // Only allow one connection. // if (devExt->UpperConnectData.ClassService != NULL) { status = STATUS_SHARING_VIOLATION; break; } else if (irpStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(CONNECT_DATA)) { // // invalid buffer // status = STATUS_INVALID_PARAMETER; break; } // // Copy the connection parameters to the device extension. // connectData = ((PCONNECT_DATA) (irpStack->Parameters.DeviceIoControl.Type3InputBuffer)); devExt->UpperConnectData = *connectData; // // Hook into the report chain. Everytime a keyboard packet is reported // to the system, KbFilter_ServiceCallback will be called // connectData->ClassDeviceObject = devExt->Self; connectData->ClassService = KbFilter_ServiceCallback; break; // // Disconnect a keyboard class device driver from the port driver. // case IOCTL_INTERNAL_KEYBOARD_DISCONNECT: // // Clear the connection parameters in the device extension. // // devExt->UpperConnectData.ClassDeviceObject = NULL; // devExt->UpperConnectData.ClassService = NULL; status = STATUS_NOT_IMPLEMENTED; break; case IOCTL_INTERNAL_I8042_HOOK_KEYBOARD: DebugPrint((\"hook keyboard received!\\n\")); if (irpStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(INTERNAL_I8042_HOOK_KEYBOARD)) { DebugPrint((\"InternalIoctl error - invalid buffer length\\n\")); status = STATUS_INVALID_PARAMETER; break; } hookKeyboard = (PINTERNAL_I8042_HOOK_KEYBOARD) irpStack->Parameters.DeviceIoControl.Type3InputBuffer; devExt->UpperContext = hookKeyboard->Context; hookKeyboard->Context = (PVOID) DeviceObject; if (hookKeyboard->InitializationRoutine) { devExt->UpperInitializationRoutine = hookKeyboard->InitializationRoutine; } hookKeyboard->InitializationRoutine = (PI8042_KEYBOARD_INITIALIZATION_ROUTINE) KbFilter_InitializationRoutine; if (hookKeyboard->IsrRoutine) { devExt->UpperIsrHook = hookKeyboard->IsrRoutine; } hookKeyboard->IsrRoutine = (PI8042_KEYBOARD_ISR) KbFilter_IsrHook; devExt->IsrWritePort = hookKeyboard->IsrWritePort; devExt->QueueKeyboardPacket = hookKeyboard->QueueKeyboardPacket; devExt->CallContext = hookKeyboard->CallContext; status = STATUS_SUCCESS; break; // // These internal ioctls are not supported by the new PnP model. // #if 0 // obsolete case IOCTL_INTERNAL_KEYBOARD_ENABLE: case IOCTL_INTERNAL_KEYBOARD_DISABLE: status = STATUS_NOT_SUPPORTED; break; #endif // obsolete case IOCTL_KEYBOARD_QUERY_ATTRIBUTES: case IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION: case IOCTL_KEYBOARD_QUERY_INDICATORS: case IOCTL_KEYBOARD_SET_INDICATORS: case IOCTL_KEYBOARD_QUERY_TYPEMATIC: case IOCTL_KEYBOARD_SET_TYPEMATIC: break; } if (!NT_SUCCESS(status)) { Irp->IoStatus.Status = status; IoCompleteRequest(Irp, IO_NO_INCREMENT); return status; } return KbFilter_DispatchPassThrough(DeviceObject, Irp); } NTSTATUS KbFilter_IoCtl (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { NTSTATUS ntStatus = STATUS_UNSUCCESSFUL; PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp); // +--------------------------------------------------------------------------+ // + 我需要修改一些东西以简化我的工作 + // +--------------------------------------------------------------------------+ ULONG ControlCode=irpStack->Parameters.DeviceIoControl.IoControlCode; // ULONG InputLength=irpStack->Parameters.DeviceIoControl.InputBufferLength; // ULONG OutputLength=irpStack->Parameters.DeviceIoControl.OutputBufferLength; DbgPrint(\"我实在受不了了!\"); switch(ControlCode) { case IOCTL_LOCK_SYSTEMKEY: DbgPrint(\"IOCTL_LOCK_SYSTEMKEY\\n\"); ntStatus = STATUS_SUCCESS; break; case IOCTL_UNLOCK_SYSTEMKEY: DbgPrint(\"IOCTL_UNLOCK_SYSTEMKEY\\n\"); ntStatus = STATUS_SUCCESS; break; default: DbgPrint(\"Default\\n\"); break; } Irp->IoStatus.Status = ntStatus; // if(ntStatus == STATUS_SUCCESS) // Irp->IoStatus.Information = irpStack->Parameters.DeviceIoControl.OutputBufferLength; // else Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT); DbgPrint(\"IoCompleteRequest\\n\"); return ntStatus; } NTSTATUS KbFilter_PnP(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp) { PDEVICE_EXTENSION devExt; PIO_STACK_LOCATION irpStack; NTSTATUS status = STATUS_SUCCESS; KIRQL oldIrql; KEVENT event; PAGED_CODE(); devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension; irpStack = IoGetCurrentIrpStackLocation(Irp); switch (irpStack->MinorFunction) { case IRP_MN_START_DEVICE: { IoCopyCurrentIrpStackLocationToNext(Irp); KeInitializeEvent(&event, NotificationEvent, FALSE ); IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE) KbFilter_Complete, &event, TRUE, TRUE, TRUE); // No need for Cancel status = IoCallDriver(devExt->TopOfStack, Irp); if (STATUS_PENDING == status) { KeWaitForSingleObject( &event, Executive, // Waiting for reason of a driver KernelMode, // Waiting in kernel mode FALSE, // No allert NULL); // No timeout } if (NT_SUCCESS(status) && NT_SUCCESS(Irp->IoStatus.Status)) { devExt->Started = TRUE; devExt->Removed = FALSE; devExt->SurpriseRemoved = FALSE; } Irp->IoStatus.Status = status; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT); break; } case IRP_MN_SURPRISE_REMOVAL: devExt->SurpriseRemoved = TRUE; IoSkipCurrentIrpStackLocation(Irp); status = IoCallDriver(devExt->TopOfStack, Irp); break; case IRP_MN_REMOVE_DEVICE: devExt->Removed = TRUE; IoSkipCurrentIrpStackLocation(Irp); IoCallDriver(devExt->TopOfStack, Irp); IoDetachDevice(devExt->TopOfStack); IoDeleteDevice(DeviceObject); status = STATUS_SUCCESS; break; case IRP_MN_QUERY_REMOVE_DEVICE: case IRP_MN_QUERY_STOP_DEVICE: case IRP_MN_CANCEL_REMOVE_DEVICE: case IRP_MN_CANCEL_STOP_DEVICE: case IRP_MN_FILTER_RESOURCE_REQUIREMENTS: case IRP_MN_STOP_DEVICE: case IRP_MN_QUERY_DEVICE_RELATIONS: case IRP_MN_QUERY_INTERFACE: case IRP_MN_QUERY_CAPABILITIES: case IRP_MN_QUERY_DEVICE_TEXT: case IRP_MN_QUERY_RESOURCES: case IRP_MN_QUERY_RESOURCE_REQUIREMENTS: case IRP_MN_READ_CONFIG: case IRP_MN_WRITE_CONFIG: case IRP_MN_EJECT: case IRP_MN_SET_LOCK: case IRP_MN_QUERY_ID: case IRP_MN_QUERY_PNP_DEVICE_STATE: default: IoSkipCurrentIrpStackLocation(Irp); status = IoCallDriver(devExt->TopOfStack, Irp); break; } return status; } NTSTATUS KbFilter_Power(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PIO_STACK_LOCATION irpStack; PDEVICE_EXTENSION devExt; POWER_STATE powerState; POWER_STATE_TYPE powerType; PAGED_CODE(); devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension; irpStack = IoGetCurrentIrpStackLocation(Irp); powerType = irpStack->Parameters.Power.Type; powerState = irpStack->Parameters.Power.State; switch (irpStack->MinorFunction) { case IRP_MN_SET_POWER: if (powerType == DevicePowerState) { devExt->DeviceState = powerState.DeviceState; } case IRP_MN_POWER_SEQUENCE: case IRP_MN_WAIT_WAKE: case IRP_MN_QUERY_POWER: default: break; } PoStartNextPowerIrp(Irp); IoSkipCurrentIrpStackLocation(Irp); return PoCallDriver(devExt->TopOfStack, Irp); } NTSTATUS KbFilter_InitializationRoutine(IN PDEVICE_OBJECT DeviceObject, IN PVOID SynchFuncContext, IN PI8042_SYNCH_READ_PORT ReadPort, IN PI8042_SYNCH_WRITE_PORT WritePort, OUT PBOOLEAN TurnTranslationOn) { PDEVICE_EXTENSION devExt; NTSTATUS status = STATUS_SUCCESS; devExt = DeviceObject->DeviceExtension; if (devExt->UpperInitializationRoutine) { status = (*devExt->UpperInitializationRoutine) ( devExt->UpperContext, SynchFuncContext, ReadPort, WritePort, TurnTranslationOn ); if (!NT_SUCCESS(status)) { return status; } } *TurnTranslationOn = TRUE; return status; } BOOLEAN KbFilter_IsrHook(PDEVICE_OBJECT DeviceObject, PKEYBOARD_INPUT_DATA CurrentInput, POUTPUT_PACKET CurrentOutput, UCHAR StatusByte, PUCHAR DataByte, PBOOLEAN ContinueProcessing, PKEYBOARD_SCAN_STATE ScanState) { PDEVICE_EXTENSION devExt; BOOLEAN retVal = TRUE; devExt = DeviceObject->DeviceExtension; if (devExt->UpperIsrHook) { retVal = (*devExt->UpperIsrHook) ( devExt->UpperContext, CurrentInput, CurrentOutput, StatusByte, DataByte, ContinueProcessing, ScanState ); if (!retVal || !(*ContinueProcessing)) { return retVal; } } *ContinueProcessing = TRUE; return retVal; } VOID KbFilter_ServiceCallback(IN PDEVICE_OBJECT DeviceObject, IN PKEYBOARD_INPUT_DATA InputDataStart, IN PKEYBOARD_INPUT_DATA InputDataEnd, IN OUT PULONG InputDataConsumed) { PDEVICE_EXTENSION devExt; if(InputDataStart->Flags & KEY_BREAK) { char theLetter = ConvertScancodeToLetter(InputDataStart->MakeCode); //Print out the scancode if(theLetter != \' \') DbgPrint(\"Kbfilter:%c was pressed \\n\",theLetter); else DbgPrint(\"Kbfilter:scancode %d\\n\",InputDataStart->MakeCode); } devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension; (*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)( devExt->UpperConnectData.ClassDeviceObject, InputDataStart, InputDataEnd, InputDataConsumed); } VOID KbFilter_Unload(IN PDRIVER_OBJECT Driver) { UNICODE_STRING uszDeviceString; PAGED_CODE(); UNREFERENCED_PARAMETER(Driver); ASSERT(NULL == Driver->DeviceObject); IoDeleteDevice(Driver->DeviceObject); RtlInitUnicodeString(&uszDeviceString, L\"\\\\DosDevices\\\\Kbfilter\"); IoDeleteSymbolicLink(&uszDeviceString); } char ConvertScancodeToLetter(ULONG scancode) { switch(scancode) { case 0x10: return \'Q\'; case 0x11: return \'W\'; case 0x12: return \'E\'; case 0x13: return \'R\'; case 0x14: return \'T\'; case 0x15: return \'Y\'; case 0x16: return \'U\'; case 0x17: return \'I\'; case 0x18: return \'O\'; case 0x19: return \'P\'; case 0x1E: return \'A\'; case 0x1F: return \'S\'; case 0x20: return \'D\'; case 0x21: return \'F\'; case 0x22: return \'G\'; case 0x23: return \'H\'; case 0x24: return \'J\'; case 0x25: return \'K\'; case 0x26: return \'L\'; case 0x2C: return \'Z\'; case 0x2D: return \'X\'; case 0x2E: return \'C\'; case 0x2F: return \'V\'; case 0x30: return \'B\'; case 0x31: return \'N\'; case 0x32: return \'M\'; default: return \' \'; } } [编辑 - 8/2/02 by cat5] |
|
5楼#
发布于:2002-08-02 13:21
应用程序:
BOOL LockSystemkey(void) { DWORD ret; HANDLE hDevice,hEvent; hDevice=CreateFile(DEVICENAME, NULL, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hDevice==INVALID_HANDLE_VALUE) { printf(\"%d\",GetLastError()); return FALSE; } // hDevice=GetDeviceViaInterface(&KbfilterDevice_CLASS_GUID,0); // if(hDevice==NULL) // { // printf(\"the device may not be installed.\"); // return FALSE; // } hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // ULONG buffer; if(!DeviceIoControl(hDevice, IOCTL_LOCK_SYSTEMKEY, (LPVOID) hEvent, 0, NULL, 0, &ret, NULL)) { printf(\"%d\",GetLastError());//为什么啊,老是返回1说功能错误。问题到底出在哪? CloseHandle(hDevice); return FALSE; } CloseHandle(hDevice); return TRUE; } |
|
6楼#
发布于:2002-08-02 13:25
和我以前遇到的问题不一样
我再看看 如果是ddk中的例程的话 应该不会有错,你看看你取得的handle对不对 |
|
|
7楼#
发布于:2002-08-02 13:29
我跟踪了一下,handle是正确的,不同机子上可能不一样,我的是0x00000044
|
|
8楼#
发布于:2002-08-02 13:33
你是怎么安装这个驱动的
说说步骤 我来试试 用softice跟一下 |
|
|
9楼#
发布于:2002-08-02 13:39
if(!DeviceIoControl(hDevice,
IOCTL_LOCK_SYSTEMKEY, (LPVOID) hEvent, 0, NULL, 0, &ret, NULL)) 第三个参数是输入缓冲区的长度,不应该为0,试试 |
|
|
10楼#
发布于:2002-08-02 13:40
sorry
是第四个参数 |
|
|
11楼#
发布于:2002-08-02 13:43
我是用设备管理器安装的。另外我想,我把中间4个参数都设成0应该没问题吧。
|
|
12楼#
发布于:2002-08-02 13:45
我试了缓冲区怎么改都不行,GetLastError()都是返回1
|
|
13楼#
发布于:2002-08-02 14:07
你把你的代码发一份给我吧
zydcat@163.com |
|
|
14楼#
发布于:2002-08-02 14:17
ok,请查收,3X!
|
|
15楼#
发布于:2002-08-02 14:17
我也曾遇到过DeviceIoContorl返回1的错误,我的错误原因是定义IOCTL CODE时的第一个参数不统一,即驱动程序和应用程序中的值不一样。不过看你的问题好象不是这个问题。
要不把代码发过来看看吧! |
|
|
16楼#
发布于:2002-08-02 14:26
好的,您的e-mail:??
|
|
17楼#
发布于:2002-08-02 16:54
lydmusic@163.net
|
|
|
18楼#
发布于:2002-08-02 20:39
呵呵!你的初始化函数里面不应该是IRP_MJ_INTERNAL_DEVICE_CONTROL
|
|
19楼#
发布于:2002-08-03 10:13
DriverObject->MajorFunction [IRP_MJ_DEVICE_CONTROL] = KbFilter_IoCtl;
老鼠,IRP_MJ_DEVICE_CONTROL有呀 |
|
|
上一页
下一页