locked
Get the driver handle by using CreateFile() API RRS feed

  • Question

  • I have driver created for WFP callout. I want to send some data from this driver to user mode application.

    How can I do this?

    I came across API DeviceIoControl() which calls the driver's callback function. But for this I need the device handle for the driver which can be created using CreateFile(). But I don't know what value should I give for file name in CreateFile() function??

    I start the driver with the command "net start WFPSamplerCallouts". So I thought of giving same name in CreatFile() which is giving error as "The system cannot find the file specified.".

    So what name should I use to get the driver handle?

    Sunday, March 24, 2013 10:41 AM

All replies

  • Did you create a symbolic link?

       NTSTATUS       status = STATUS_SUCCESS; 
       UNICODE_STRING dosUnicodeString;  
       UNICODE_STRING ntUnicodeString;
    
       RtlZeroMemory(&dosUnicodeString,
                     sizeof(UNICODE_STRING));
    
       RtlZeroMemory(&ntUnicodeString,
                     sizeof(UNICODE_STRING));
    
       pGlobalDeviceObject = 0;
    
       RtlInitUnicodeString(&ntUnicodeString,
                            L"\\Device\WFPSamplerCallouts");
    
       RtlInitUnicodeString(&dosUnicodeString,
                            L"\\DosDevice\\WFPSamplerCallouts");
    
       status = IoCreateDevice(pDriverObject,
                               0,
                               &ntUnicodeString,
                               FILE_DEVICE_NETWORK,
                               0,
                               FALSE,
                               &pGlobalDeviceObject);
       DRIVER_BAIL_ON_FAILURE(status);
    
       DRIVER_BAIL_ON_NULL_POINTER(pGlobalDeviceObject);
    
       pGlobalDeviceObject->Flags |= DO_BUFFERED_IO;
    
       // Initialize the driver object with this driver's entry points.
       pDriverObject->MajorFunction[IRP_MJ_CREATE]         = DriverDispatch;
       pDriverObject->MajorFunction[IRP_MJ_CLOSE]          = DriverDispatch;
       pDriverObject->MajorFunction[IRP_MJ_CLEANUP]        = DriverDispatch;
       pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverDispatch;
       pDriverObject->DriverUnload                         = DriverUnload;
    
       // Create symbolic link between the Dos Device name and Nt
       // Device name for the test protocol driver.
       status = IoCreateSymbolicLink(&dosUnicodeString,
                                     &ntUnicodeString);
       DRIVER_BAIL_ON_FAILURE(status);
    


    You then use the device name:

       WCHAR    pDeviceName[40];  
       wcscpy_s(pDeviceName,
                40,
                L"\\\\.\\");  
    
       wcscat_s(pDeviceName,
                40,
                pDevice);  
      
       *pDriver = CreateFileW(pDeviceName,   
                              GENERIC_READ | GENERIC_WRITE,  
                              FILE_SHARE_READ | 
                              FILE_SHARE_WRITE,  
                              NULL, /// lpSecurityAttirbutes
                              OPEN_EXISTING,  
                              FILE_ATTRIBUTE_NORMAL | 
                              FILE_FLAG_OVERLAPPED,
                              NULL); /// lpTemplateFile 

    Hope this helps,


    Dusty Harper [MSFT]
    Microsoft Corporation
    ------------------------------------------------------------
    This posting is provided "AS IS", with NO warranties and confers NO rights
    ------------------------------------------------------------

    Monday, April 8, 2013 6:44 PM
    Moderator