How to open control object from user app using symbolic link?

Discussion in 'Windows Vista Drivers' started by dmitrym_10, Aug 21, 2007.

  1. dmitrym_10

    dmitrym_10 Guest

    Hi There,

    I am having problem opening control device object that I create in driver
    from the user application.
    I create a control object and make a symbolic link to it. When I try to open
    it using CreateFile() it returns invalid handle and GetlastError() returns
    "file not found"

    Here is how I create control object:

    #define K_MOUSE_FILTER_DEVICE_NAME L"\\Device\\MyCompanyMouseSniffer"
    #define K_MOUSE_CONTROL_DEVICE_NAME L"\\Device\\MyCompanyMouseSnifferIoCtl"

    NTSTATUS
    MouseFilter_CreateControlObject(IN WDFDRIVER Driver)
    {
    PWDFDEVICE_INIT DeviceInit;
    UNICODE_STRING DeviceName;
    WDF_OBJECT_ATTRIBUTES DeviceAttributes;
    WDF_IO_QUEUE_CONFIG DeviceQueueConfig;
    WDFQUEUE DeviceQueue;
    UNICODE_STRING SymbolicLinkName;

    NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;

    if (InstanceNo != 0)
    return STATUS_SUCCESS;

    /* Allocate INIT structure. */
    DeviceInit = WdfControlDeviceInitAllocate(Driver,
    &SDDL_DEVOBJ_SYS_ALL_ADM_RWX_WORLD_RW_RES_R);
    if (NULL == DeviceInit)
    {
    DebugPrint(("WdfControlDeviceInitAllocate failed\n"));
    return status;
    }

    /* Assign name to the control device. */
    RtlInitUnicodeString(&DeviceName, K_MOUSE_FILTER_DEVICE_NAME);
    status = WdfDeviceInitAssignName(DeviceInit, &DeviceName);
    if (!NT_SUCCESS(status))
    {
    WdfDeviceInitFree(DeviceInit);
    DebugPrint(("WdfDeviceInitAssignName failed with status code
    0x%x\n", status));
    return status;
    }

    /* Shutdown notification must be set before WdfDeviceCreate() call. */
    WdfControlDeviceInitSetShutdownNotification(DeviceInit,
    ControlDevice_EvtShutdown, WdfDeviceShutdown);

    /* Initialize attributes struct. */
    WDF_OBJECT_ATTRIBUTES_INIT(&DeviceAttributes);

    /* Create device. */
    status = WdfDeviceCreate(&DeviceInit, &DeviceAttributes, &ControlObject);
    if (!NT_SUCCESS(status))
    {
    WdfDeviceInitFree(DeviceInit);
    DebugPrint(("WdfDeviceCreate failed with status code 0x%x\n",
    status));
    return status;
    }

    /* Create device queue. */
    WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&DeviceQueueConfig,
    WdfIoQueueDispatchSequential);
    DeviceQueueConfig.EvtIoRead = ControlObject_EvtIoRead; // READ ONLY
    status = WdfIoQueueCreate(ControlObject, &DeviceQueueConfig,
    WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE);
    if (!NT_SUCCESS(status))
    {
    WdfDeviceInitFree(DeviceInit);
    DebugPrint(("WdfIoQueueCreate failed with status code 0x%x\n",
    status));
    return status;
    }

    /* Create symbolic link so it could be open by CreateFile() from user
    space. */
    RtlInitUnicodeString(&SymbolicLinkName, K_MOUSE_CONTROL_DEVICE_NAME);
    status = WdfDeviceCreateSymbolicLink(ControlObject, &SymbolicLinkName);
    if (!NT_SUCCESS(status))
    {
    WdfDeviceInitFree(DeviceInit);
    DebugPrint(("WdfDeviceCreateSymbolicLink failed with status code
    0x%x\n", status));
    return status;
    }

    /* Done. */
    WdfControlFinishInitializing(ControlObject);

    InstanceNo ++;

    return status;
    }

    It doesn't fail and function return status 0.

    Here is how I open it from user application:
    PWCHAR path = L"\\\\.\\MyCompanyMouseSnifferIoCtl";
    HANDLE device = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, OPEN_EXISTING, 0, NULL );

    I also tried L"\\Device\\MyCompanyMouseSnifferIoCtl";

    I can't figure out what I am doing wrong. I appreciate your help.
    Thank you,
    Dmitry.
     
    dmitrym_10, Aug 21, 2007
    #1
    1. Advertisements

  2. dmitrym_10

    dmitrym_10 Guest

    I have figured it out.

    Device must me named with NT device name
    #define K_MOUSE_FILTER_DEVICE_NAME L"\\Device\\MyCompanyMouseSniffer"

    And if symbolic link uses Dos device name
    #define K_MOUSE_FILTER_DEVICE_NAME L"\\Device\\MyCompanyMouseSniffer"

    It all works and I can open it from user application with CreateFile().
     
    dmitrym_10, Aug 21, 2007
    #2
    1. Advertisements

  3. change
    to
    #define K_MOUSE_CONTROL_DEVICE_NAME L"\\??\\MyCompanyMouseSnifferIoCtl"

    also, look at the KMDF kbdfiltr example which enumerates a raw PDO. it is
    much easier to manage then a control object if there is more then one mouse

    d
     
    Doron Holan [MSFT], Aug 21, 2007
    #3
  4. When we looked at raw PDOs (versus control devices), my colleague
    basically reported that the OS behavior was different in each of 2K,
    XP, and Vista. In some cases you got the hardware wizard (or at least
    the balloon tip), in some cases you needed a skeleton INF to install
    the PDO, and so on. Is that still the case or did we do something
    wrong?
     
    chris.aseltine, Aug 21, 2007
    #4
  5. Yes you have to provide a NULL INF on Win2K to get the raw PDO instantiated.
    I don't remember whether the balloon tip comes up on XP and above. My
    feeling is that it does.
     
    Eliyas Yakub [MSFT], Aug 21, 2007
    #5
  6. you need the INF for win2k, i think we accidentally left that iNF out of the
    WDK and it will be in the next wdk. yes, there are differences but i don't
    really see why that is a showstopper if you have the appropriate INF. IMHO,
    the 1:1 relationship for raw PDo to mouse is much better then the 1:N
    relationship you have with a control device.

    d
     
    Doron Holan [MSFT], Aug 22, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.