none
Driver's symbolic link disappearing RRS feed

  • Question

  • In my UMDF windows driver I create a symbolic link of my device in \DosDevices\Global\ directory in order to track device instances. So if a device gets connected to the host a symbolic link \DosDevices\Global\MyDevice0 gets created. If another device gets connected a symbolic link \DosDevices\Global\MyDevice1 gets created. I achieve this by following code:

    int symbolicLinkIndex = 0;
    for (symbolicLinkIndex = 0; symbolicLinkIndex < 127; symbolicLinkIndex++) // 127 max usb devices
    {
        swprintf(m_SymbolicLinkName,
                 L"\\DosDevices\\Global\\MyDevice%d",
                 symbolicLinkIndex);
    
        hr = m_FxDevice->CreateSymbolicLinkA(m_SymbolicLinkName);
        if (FAILED(hr))
        {
            // Symbolic link already exists try next index
            if (HRESULT_CODE(hr) == ERROR_ALREADY_EXISTS || HRESULT_CODE(hr) == ERROR_FILE_EXISTS)
                continue;
            else
            {
                TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "%!FUNC! Failed to create symbolic link: %lu", hr);
                goto Exit;
            }
        }
        else
        {
            TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "%!FUNC! Created symbolic link: %S", m_SymbolicLinkName);
            break;
        }
    }

    Method documentation in MSDN: IWDFDevice::CreateSymbolicLink

    This works if I quickly connect two devices behind each other. However it seems if I wait for about 10 seconds and connect a third device it gets the symbolic link \DosDevices\Global\MyDevice0 which shall not happen, because the first device already got index 0 and is still connected.

    So why does it seem that the symbolic link gets deleted after some while? Also I am wondering if my approach is a good one to count device instances?

    Wednesday, October 12, 2016 10:49 AM

All replies

  • If you use device interfaces you won't have this problem and there are built in mechanisms for enumeration. Legacy style symbolic links should only be used where you have legacy code that can't be updated to understand device interfaces.

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Doron Holan [MSFT] Wednesday, October 12, 2016 4:04 PM
    • Unmarked as answer by Armin Bego Thursday, October 13, 2016 4:14 PM
    Wednesday, October 12, 2016 4:04 PM
  • Can you show me an example how I can achieve what I'd like to by using device interfaces? When I call IWDFDevice::CreateDeviceInterface with reference string "MyDevice0" it does not return me an "interface already exists" error when I call it a second time with the exact same reference string. I would like to be able to decide per device which instance it is. Whether "MyDevice0", "MyDevice1", ...
    Wednesday, October 12, 2016 7:34 PM
  • Why? The point of device interfaces is that you do not need to know which instance a device is nor track previous instances

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Wednesday, October 12, 2016 8:29 PM
  • Because of my other question: Here
    Wednesday, October 12, 2016 8:35 PM
  • I would think that the names should be static, regardless of order of insertion or enumeration. IF you are able to implement what you want, card reader #2's name will change to #1 if card reader #1 is not present. That would be extremely confusing to the user as the physical device remains the same. Furthermore, if #1 and #2 are enumerated in a different order on reboot, the names will also change, again confusing the user.

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, October 13, 2016 4:47 PM
  • As Doron has been pointing out a device interface is the correct way to go.  You haven't said how your device is connected to the system, but for a number of buses I have used the location property of the device to create a unique reference string for the device interface.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Thursday, October 13, 2016 5:02 PM
  • As Doron has been pointing out a device interface is the correct way to go.  You haven't said how your device is connected to the system, but for a number of buses I have used the location property of the device to create a unique reference string for the device interface.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com


    I need an enumarated number for my device instance to be able to append it to the "friendly name" string. Thank you for your input so far!
    Thursday, October 13, 2016 10:16 PM
  • I would think that the names should be static, regardless of order of insertion or enumeration. IF you are able to implement what you want, card reader #2's name will change to #1 if card reader #1 is not present. That would be extremely confusing to the user as the physical device remains the same. Furthermore, if #1 and #2 are enumerated in a different order on reboot, the names will also change, again confusing the user.

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.


    This is exactly the behaviour of the default Micorosoft card reader driver (Microsoft Usbccid Smartcard Reader (WUDF)). I have been trying to apply this beaviour to my driver as well.
    Thursday, October 13, 2016 10:16 PM