Windows驱动
+ -

CreateFile传递参数给驱动

2021-07-01 145 0

BULKUSB的IRP_MN_CREATE的回调函数如下:

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

Routine Description:

    Dispatch routine for create.

Arguments:

    DeviceObject - pointer to device object
    Irp - I/O request packet.

Return Value:

    NT status value

--*/
{
    ULONG                       i;
    NTSTATUS                    ntStatus;
    PFILE_OBJECT                fileObject;
    PDEVICE_EXTENSION           deviceExtension;
    PIO_STACK_LOCATION          irpStack;
    PBULKUSB_PIPE_CONTEXT       pipeContext;
    PUSBD_INTERFACE_INFORMATION interface;

    PAGED_CODE();

    BulkUsb_DbgPrint(3, ("BulkUsb_DispatchCreate - begins\n"));

    //
    // initialize variables
    //
    irpStack = IoGetCurrentIrpStackLocation(Irp);
    fileObject = irpStack->FileObject;
    deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;

    if(deviceExtension->DeviceState != Working) {

        ntStatus = STATUS_INVALID_DEVICE_STATE;
        goto BulkUsb_DispatchCreate_Exit;
    }

    if(deviceExtension->UsbInterface) {

        interface = deviceExtension->UsbInterface;
    }
    else {

        BulkUsb_DbgPrint(1, ("UsbInterface not found\n"));

        ntStatus = STATUS_INVALID_DEVICE_STATE;
        goto BulkUsb_DispatchCreate_Exit;
    }

    //
    // FsContext is Null for the device
    //
    if(fileObject) {

        fileObject->FsContext = NULL; 
    }
    else {

        ntStatus = STATUS_INVALID_PARAMETER;
        goto BulkUsb_DispatchCreate_Exit;
    }

    if(0 == fileObject->FileName.Length) {

        //
        // opening a device as opposed to pipe.
        //
        ntStatus = STATUS_SUCCESS;

        InterlockedIncrement(&deviceExtension->OpenHandleCount);

        //
        // the device is idle if it has no open handles or pending PnP Irps
        // since we just received an open handle request, cancel idle req.
        //
        if(deviceExtension->SSEnable) {

            CancelSelectSuspend(deviceExtension);
        }

        goto BulkUsb_DispatchCreate_Exit;
    }

    pipeContext = BulkUsb_PipeWithName(DeviceObject, &fileObject->FileName);

    if(pipeContext == NULL) {

        ntStatus = STATUS_INVALID_PARAMETER;
        goto BulkUsb_DispatchCreate_Exit;
    }

    ntStatus = STATUS_INVALID_PARAMETER;

    for(i=0; i<interface->NumberOfPipes; i++) {

        if(pipeContext == &deviceExtension->PipeContext[i]) {

            //
            // found a match
            //
            BulkUsb_DbgPrint(3, ("open pipe %d\n", i));

            fileObject->FsContext = &interface->Pipes[i];

            ASSERT(fileObject->FsContext);

            pipeContext->PipeOpen = TRUE;

            ntStatus = STATUS_SUCCESS;

            //
            // increment OpenHandleCounts
            //
            InterlockedIncrement(&deviceExtension->OpenHandleCount);

            //
            // the device is idle if it has no open handles or pending PnP Irps
            // since we just received an open handle request, cancel idle req.
            //
            if(deviceExtension->SSEnable) {

                CancelSelectSuspend(deviceExtension);
            }
        }
    }

BulkUsb_DispatchCreate_Exit:

    Irp->IoStatus.Status = ntStatus;
    Irp->IoStatus.Information = 0;

    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    BulkUsb_DbgPrint(3, ("BulkUsb_DispatchCreate - ends\n"));

    return ntStatus;
}

其中,有句话是获取设备的管道名

  pipeContext = BulkUsb_PipeWithName(DeviceObject, &fileObject->FileName);

个人百思不得其解,打开设备时,不是用的是设备名吗,怎么这个文件名是怎么来的呢?我们平时打开设备时这个地主都是空~

后来通过应用层的代码才发现,是在设备名加\\组成设备名\\文件名这样的格式,这样就自动会动将\\及后的变成文件名了。

这其实就是变成了通过传递不同的参数到CreateFile,可以实现不同的功能了~

LE
open_file( char *filename)
/*++
Routine Description:

    Called by main() to open an instance of our device after obtaining its name

Arguments:

    None

Return Value:

    Device handle on success else NULL

--*/
{

        int success = 1;
        HANDLE h;

        if ( !GetUsbDeviceFileName(
                (LPGUID) &GUID_CLASS_I82930_BULK,
                completeDeviceName) )
        {
                NOISY(("Failed to GetUsbDeviceFileName\n", GetLastError()));
                return  INVALID_HANDLE_VALUE;
        }

    strcat (completeDeviceName,
                        "\\"
                        );                      

    strcat (completeDeviceName,
                        filename
                        );                                      

        printf("completeDeviceName = (%s)\n", completeDeviceName);

        h = CreateFile(completeDeviceName,
                GENERIC_WRITE | GENERIC_READ,
                FILE_SHARE_WRITE | FILE_SHARE_READ,
                NULL,
                OPEN_EXISTING,
                0,
                NULL);

        if (h == INVALID_HANDLE_VALUE) {
                NOISY(("Failed to open (%s) = %d", completeDeviceName, GetLastError()));
                success = 0;
        } else {
                        NOISY(("Opened successfully.\n"));
    }           

        return h;
}

代码,我这里没有试,想应该没有问题吧~

0 篇笔记 写笔记

CreateFile()中的OPEN_ALWAYS和CREATE_ALWAYS之间的差异
任何人都可以解释Windows API的 CreateFile()功能的创建配置OPEN_ALWAYS和CREATE_ALWAYS之间的区别?对我来说,似乎他们只是简单地“创建文件,如果它不存在”。如果文件已经存在,CREATE_ALWAYS也会截断内容。另一方面,OPEN_ALWAYS不会破坏已......
CreateFile打开驱动设备权限不足
记得以前写了一个文件过滤驱动,使用的是minifilter的框架。在创建设备时,很常规的使用IoCreateDevice来创建一个命名的设备对象,然后对这个设备名使用IoCreateSymbolicLink创建其应用层的链接符号名,这样我们在应用层就可以通过CreateFile函数就可以打开这个设备......
Windows内核打开和关闭文件
文件的打开下面的函数用于打开一个文件: NTSTATUS ZwCreateFile( OUT PHANDLE FileHandle, IN ACCESS_MASK DesiredAccess, ......
CreateFile传递参数给驱动
BULKUSB的IRP_MN_CREATE的回调函数如下:NTSTATUSBulkUsb_DispatchCreate( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp )/*++Routine Descr......
文件数据读取内存GetBufferFromFile
使用std::string存储从文件中读取的二进制数。BOOL GetBufferFromFile(TCHAR* pFileName, std::string& strFileStream){ HANDLE hFile = INVALID_HANDLE_VALUE; B......
IRP_MJ_CREATE标识CreateFile句柄FsContext
驱动程序可以通过CreateFile打开设备,不过有一种情况我们需要对不同的CreateFile句柄在驱动中标识不同的CONTEXT.这个CONTEXT可以存储在rpSp->FileObject->FsContext中。即每个CreateFile实例句柄都在驱动中有惟一对应的MY_CON......
作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!