⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vhidmini.c

📁 winddk src目录下的WDM源码压缩!
💻 C
📖 第 1 页 / 共 3 页
字号:
        //

        RtlCopyMemory(Irp->UserBuffer, readReport, bytesToCopy);
        
        //
        // Report how many bytes were copied
        //

        Irp->IoStatus.Information = bytesToCopy;
    }

    //
    //Set real return status in Irp
    //

    Irp->IoStatus.Status = ntStatus;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    //
    //Free the DPC structure
    //

    ExFreePool(readTimerStruct);
    
    DebugPrint(("ReadTimerDpcRoutine Exit = 0x%x\n", ntStatus));

}


NTSTATUS 
GetReportDescriptor(
    IN PDEVICE_OBJECT DeviceObject, 
    IN PIRP Irp
    )
/*++

Routine Description:

    Finds the Report descriptor and copies it into the buffer provided by the 
    Irp.

Arguments:

    DeviceObject - pointer to a device object.

    Irp - Pointer to Interrupt Request Packet.

Return Value:

    NT status code.

--*/
{
    NTSTATUS            ntStatus = STATUS_SUCCESS;
    PDEVICE_EXTENSION   deviceInfo;
    PIO_STACK_LOCATION  IrpStack;
    ULONG               bytesToCopy;

    DebugPrint(("HidMiniGetReportDescriptor Entry\n"));

    //
    // Get a pointer to the current location in the Irp
    //

    IrpStack = IoGetCurrentIrpStackLocation(Irp);

    //
    // Get a pointer to the device extension
    //

    deviceInfo = GET_MINIDRIVER_DEVICE_EXTENSION(DeviceObject);

    //
    // Copy device descriptor to HIDCLASS buffer
    //

    DebugPrint(("HIDCLASS Buffer = 0x%x, Buffer length = 0x%x\n", 
                Irp->UserBuffer, 
                IrpStack->Parameters.DeviceIoControl.OutputBufferLength));


    bytesToCopy = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;

    if(!bytesToCopy) {
        return STATUS_BUFFER_TOO_SMALL;
    }

    if (bytesToCopy > 
             deviceInfo->HidDescriptor.DescriptorList[0].wReportLength) {
        bytesToCopy = 
             deviceInfo->HidDescriptor.DescriptorList[0].wReportLength;
    }

    DebugPrint(("Copying 0x%x bytes to HIDCLASS buffer\n", bytesToCopy));

    RtlCopyMemory((PUCHAR) Irp->UserBuffer,
                (PUCHAR) deviceInfo->ReportDescriptor, 
                bytesToCopy
                );

    //
    // Report how many bytes were copied
    //
    Irp->IoStatus.Information = bytesToCopy;

    DebugPrint(("HidMiniGetReportDescriptor Exit = 0x%x\n", ntStatus));

    return ntStatus;
    
}


NTSTATUS 
GetDeviceAttributes(
    IN PDEVICE_OBJECT DeviceObject, 
    IN PIRP Irp)
/*++

Routine Description:

    Fill in the given struct _HID_DEVICE_ATTRIBUTES

Arguments:

    DeviceObject - pointer to a device object.

    Irp - Pointer to Interrupt Request Packet.

Return Value:

    NT status code.

--*/
{
    NTSTATUS                 ntStatus = STATUS_SUCCESS;
    PIO_STACK_LOCATION       irpStack;
    PHID_DEVICE_ATTRIBUTES   deviceAttributes;

    DebugPrint(("HidMiniGetDeviceAttributes Entry\n"));

    //
    // Get a pointer to the current location in the Irp
    //

    irpStack = IoGetCurrentIrpStackLocation(Irp);

    //
    // Get a pointer to the device extension
    //

    deviceAttributes = (PHID_DEVICE_ATTRIBUTES) Irp->UserBuffer;

    if(sizeof (HID_DEVICE_ATTRIBUTES) >
        irpStack->Parameters.DeviceIoControl.OutputBufferLength) {
        return STATUS_BUFFER_TOO_SMALL;
    }

    //
    // Report how many bytes were copied
    //
    Irp->IoStatus.Information = sizeof (HID_DEVICE_ATTRIBUTES);

    deviceAttributes->Size = sizeof (HID_DEVICE_ATTRIBUTES);
    deviceAttributes->VendorID = HIDMINI_VID;
    deviceAttributes->ProductID = HIDMINI_PID;
    deviceAttributes->VersionNumber = HIDMINI_VERSION;

    DebugPrint(("HidMiniGetAttributes Exit = 0x%x\n", ntStatus));

    return ntStatus;
}


NTSTATUS
CheckRegistryForDescriptor(
        IN PDEVICE_OBJECT  DeviceObject
        )
/*++

Routine Description:

    Read "ReadFromRegistry" key value from device parameters in the registry. 

Arguments:

    DeviceObject - pointer to a device object.

Return Value:

    NT status code.

--*/

{

    NTSTATUS           ntStatus = STATUS_SUCCESS;
    PDEVICE_OBJECT     pdo;
    HANDLE             handle;
    ULONG              length = 0, resultLength = 0;
    PUCHAR             ReadFromRegistryKeyValue = NULL;
    UNICODE_STRING     ReadFromRegistryKeyValueName;
    ULONG              ReadFromRegistry;

    //
    // Get physical device object
    //

    pdo = GET_PHYSICAL_DEVICE_OBJECT(DeviceObject);

    //
    // Get a handle to the Device Parameters registry key
    //

    ntStatus = IoOpenDeviceRegistryKey(
                         pdo,   
                         PLUGPLAY_REGKEY_DEVICE,   
                         KEY_QUERY_VALUE,    
                         &handle          
                         );
    
    if(!NT_SUCCESS(ntStatus)){   
        
       DebugPrint(("IoOpenDeviceRegistryKey failed status:0x%x\n", ntStatus));
       return ntStatus;

    }

    //
    //Query the "ReadFromRegistry" key value in the parameters key
    //

    RtlInitUnicodeString(&ReadFromRegistryKeyValueName,
                         L"ReadFromRegistry");
    
    ntStatus= ZwQueryValueKey(
                handle,     
                &ReadFromRegistryKeyValueName, 
                KeyValuePartialInformation , 
                (PVOID)ReadFromRegistryKeyValue, 
                length,   
                &resultLength    
                );

    if(STATUS_BUFFER_TOO_SMALL == ntStatus)
    {
        //
        // Query the registry again using resultlength as buffer length
        //
        
        length = resultLength;            
        ReadFromRegistryKeyValue = ExAllocatePoolWithTag(
                                                    PagedPool,
                                                    length,
                                                    VHID_POOL_TAG
                                                    );

        if(!ReadFromRegistryKeyValue){
            
            DebugPrint(("Allocating to keyValueInformation failed\n"));
            ntStatus = STATUS_UNSUCCESSFUL;
            goto query_end;
            
        }
      
        ntStatus= ZwQueryValueKey(
                handle,    
                &ReadFromRegistryKeyValueName ,
                KeyValuePartialInformation , 
                (PVOID)ReadFromRegistryKeyValue, 
                length,  
                &resultLength   
                );

        if(!NT_SUCCESS(ntStatus)){
            
           DebugPrint(("ZwQueryValueKey for ReadFromRegistry"
                        "failed status:0x%x\n", ntStatus));
           
           goto query_end;
           
        }
        
    }
    else {
        
        if(!NT_SUCCESS(ntStatus)){
            
           DebugPrint(("ZwQueryValueKey for ReadFromRegistry failed status:0x%x\n", ntStatus));
           goto query_end;
           
        }
        
    }

    //
    // if ReadFromRegsitry is set to 0 then we do not need to read 
    // the descriptor from regsistry. Just return failure.
    //
    
    ReadFromRegistry = ((PKEY_VALUE_PARTIAL_INFORMATION) 
                       ReadFromRegistryKeyValue)->Data[0] ;

    if(!ReadFromRegistry) {
        
        ntStatus = STATUS_UNSUCCESSFUL;
        goto query_end;
        
    }

query_end:

    ZwClose(handle);
    
    if(ReadFromRegistryKeyValue)
        ExFreePool(ReadFromRegistryKeyValue);

    return ntStatus;

}


NTSTATUS
ReadDescriptorFromRegistry(
        IN PDEVICE_OBJECT  DeviceObject
        )
/*++

Routine Description:

    Read HID report descriptor from registry
    
Arguments:

    DeviceObject - pointer to a device object.

Return Value:

    NT status code.

--*/
{
    NTSTATUS             ntStatus = STATUS_SUCCESS;
    PDEVICE_OBJECT       pdo;
    PDEVICE_EXTENSION    deviceInfo;
    HANDLE               handle;
    ULONG                length = 0;
    ULONG                resultLength = 0;
    ULONG                RegistryReportDescriptorLength = 0;
    PUCHAR               RegistryReportDescriptor= NULL;
    PUCHAR               keyValueInformation = NULL;
    UNICODE_STRING       valueName;

    //
    // Get physical device object
    //

    pdo = GET_PHYSICAL_DEVICE_OBJECT(DeviceObject);

    //
    // Get the hid mini driver device extension
    //

    deviceInfo = GET_MINIDRIVER_DEVICE_EXTENSION(DeviceObject);

    //
    // Get a handle to the Device registry key
    //

    ntStatus = IoOpenDeviceRegistryKey(pdo,   
                                       PLUGPLAY_REGKEY_DEVICE,   
                                       KEY_QUERY_VALUE,    
                                       &handle          
                                       );

    if(!NT_SUCCESS(ntStatus)){   
        
       DebugPrint(("IoOpenDeviceRegistryKey failed status:0x%x\n", ntStatus));
       return ntStatus;

    }
    
    //
    //Query the Descriptor value in the parameters key
    //
    RtlInitUnicodeString(&valueName, L"MyReportDescriptor");
    
    ntStatus= ZwQueryValueKey(
                handle,     
                &valueName,
                KeyValuePartialInformation, 
                (PVOID)keyValueInformation, 
                length,   
                &resultLength    
                );

    if(STATUS_BUFFER_TOO_SMALL == ntStatus){
        length = resultLength;            
        keyValueInformation = ExAllocatePoolWithTag(
                                    PagedPool,
                                    length,
                                    VHID_POOL_TAG
                                    );

        if(!keyValueInformation){
            
            DebugPrint(("Allocating to keyValueInformation failed\n"));
            ntStatus = STATUS_UNSUCCESSFUL;
            goto query_end;
            
        }
        ntStatus= ZwQueryValueKey(
                handle,     
                &valueName , 
                KeyValuePartialInformation , 
                (PVOID)keyValueInformation, 
                length,   
                &resultLength    
                );

        if(!NT_SUCCESS(ntStatus)){
            
            DebugPrint(("ZwQueryValueKey failed status:0x%x\n", ntStatus));                    
            goto query_end;
            
        }
        //
        // Allocate memory for Report Descriptor   
        //
        RegistryReportDescriptorLength = 
                  ((PKEY_VALUE_PARTIAL_INFORMATION)
                  keyValueInformation)->DataLength;

        RegistryReportDescriptor = 
                  ExAllocatePoolWithTag(NonPagedPool, 
                                        RegistryReportDescriptorLength,
                                        VHID_POOL_TAG
                                        );
                            
        if(!RegistryReportDescriptor){
            
            DebugPrint(("Memory allocation for Report descriptor failed\n"));            
            ntStatus = STATUS_UNSUCCESSFUL;
            goto query_end;

        }

        //
        // Copy report descriptor from registry
        //
        RtlCopyMemory(RegistryReportDescriptor, 
                      ((PKEY_VALUE_PARTIAL_INFORMATION)
                      keyValueInformation)->Data, 
                      RegistryReportDescriptorLength
                      );

        DebugPrint(("No. of report descriptor bytes copied: %d\n", 
                    RegistryReportDescriptorLength
                    ));

        //
        // Store the registry report descriptor in the device extension
        //
        deviceInfo->ReadReportDescFromRegistry = TRUE;
        deviceInfo->ReportDescriptor = RegistryReportDescriptor;
        deviceInfo->HidDescriptor.DescriptorList[0].wReportLength = 
                                   (USHORT)RegistryReportDescriptorLength;

    }   
    else {
        if(!NT_SUCCESS(ntStatus)) {            
            DebugPrint(("ZwQueryValueKey failed status:0x%x\n", ntStatus));                
        }
    }            

    query_end:

    ZwClose(handle);
   
    if(keyValueInformation)
        ExFreePool(keyValueInformation);
    
    return ntStatus;

}


#if DBG
PCHAR
PnPMinorFunctionString (
    UCHAR MinorFunction
    )
{
    switch (MinorFunction)
    {
    case IRP_MN_START_DEVICE:
        return "IRP_MN_START_DEVICE";
    case IRP_MN_QUERY_REMOVE_DEVICE:
        return "IRP_MN_QUERY_REMOVE_DEVICE";
    case IRP_MN_REMOVE_DEVICE:
        return "IRP_MN_REMOVE_DEVICE";
    case IRP_MN_CANCEL_REMOVE_DEVICE:
        return "IRP_MN_CANCEL_REMOVE_DEVICE";
    case IRP_MN_STOP_DEVICE:
        return "IRP_MN_STOP_DEVICE";
    case IRP_MN_QUERY_STOP_DEVICE:
        return "IRP_MN_QUERY_STOP_DEVICE";
    case IRP_MN_CANCEL_STOP_DEVICE:
        return "IRP_MN_CANCEL_STOP_DEVICE";
    case IRP_MN_QUERY_DEVICE_RELATIONS:
        return "IRP_MN_QUERY_DEVICE_RELATIONS";
    case IRP_MN_QUERY_INTERFACE:
        return "IRP_MN_QUERY_INTERFACE";
    case IRP_MN_QUERY_CAPABILITIES:
        return "IRP_MN_QUERY_CAPABILITIES";
    case IRP_MN_QUERY_RESOURCES:
        return "IRP_MN_QUERY_RESOURCES";
    case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
        return "IRP_MN_QUERY_RESOURCE_REQUIREMENTS";
    case IRP_MN_QUERY_DEVICE_TEXT:
        return "IRP_MN_QUERY_DEVICE_TEXT";
    case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
        return "IRP_MN_FILTER_RESOURCE_REQUIREMENTS";
    case IRP_MN_READ_CONFIG:
        return "IRP_MN_READ_CONFIG";
    case IRP_MN_WRITE_CONFIG:
        return "IRP_MN_WRITE_CONFIG";
    case IRP_MN_EJECT:
        return "IRP_MN_EJECT";
    case IRP_MN_SET_LOCK:
        return "IRP_MN_SET_LOCK";
    case IRP_MN_QUERY_ID:
        return "IRP_MN_QUERY_ID";
    case IRP_MN_QUERY_PNP_DEVICE_STATE:
        return "IRP_MN_QUERY_PNP_DEVICE_STATE";
    case IRP_MN_QUERY_BUS_INFORMATION:
        return "IRP_MN_QUERY_BUS_INFORMATION";
    case IRP_MN_DEVICE_USAGE_NOTIFICATION:
        return "IRP_MN_DEVICE_USAGE_NOTIFICATION";
    case IRP_MN_SURPRISE_REMOVAL:
        return "IRP_MN_SURPRISE_REMOVAL";
    //case IRP_MN_QUERY_LEGACY_BUS_INFORMATION: // Not defined in WDM.H
        //return "IRP_MN_QUERY_LEGACY_BUS_INFORMATION";
    default:
        return "unknown_pnp_irp";
    }
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -