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: 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?