I developed an application that enumerates and merges the content maps of all drives ever connected to a system. Doing the same for devices with the WPD API Interop.PortableDeviceApiLib.dll, there is a marshal limit for managing contemporary devices.
I managed to follow the trick cited in https://blogs.msdn.microsoft.com/dimeby8/2006/12/05/enumerating-wpd-devices-in-c/
to dis-assemble and re-assemble Interop.PortableDeviceApiLib.dll in order to manage more then one device at a time, replacing
instance void GetDevices([in][out] string& marshal( lpwstr) pPnPDeviceIDs
with
instance void GetDevices([in][out] string[] marshal( lpwstr[]) pPnPDeviceIDs
BUT, with two connected devices, the second item of the returned array is ALWAYS null !!!
This is the code snippet:
public string[] Refresh() {
deviceManager.RefreshDeviceList();
string[] nullArray = { null };
uint count = 1;
try { deviceManager.GetDevices(null, ref count); } // <-- I tried also with nullArray instead of null
catch (Exception ex) { Console.WriteLine(ex.Message); count = 0; }
if (count == 0)
return null;
var deviceIds = new string[count];
deviceManager.GetDevices(deviceIds, ref count);
foreach (var deviceId in deviceIds)
{
Add(new PortableDevice(deviceId));
}
return deviceIds;
}
Note that there are two already connected devices (on each I responded to Allow connection: YES), so the var count receives the value 2 and deviceIds[0] is OK, but deviceIds[1] is always null ! (even after swapping the devices on the USB slots).
I use Windows Studio Professional 2017 on Windows 10 The two devices are a Honor9 and a USB disk or an iPad. In the project.csproj file I inserted: ...
<Reference Include="Interop.PortableDeviceApiLib">
<HintPath>.\Interop.PortableDeviceApiLib.dll</HintPath>
</Reference>
<Reference Include="Interop.PortableDeviceTypesLib">
<HintPath>.\Interop.PortableDeviceTypesLib.dll</HintPath>
</Reference>
... instead to reference the COM files:
<COMReference Include="PortableDeviceApiLib">
<Guid>{1F001332-1A57-4934-BE31-AFFC99F4EE0A}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>False</EmbedInteropTypes>
</COMReference>
<COMReference Include="PortableDeviceTypesLib">
<Guid>{2B00BA2F-E750-4BEB-9235-97142EDE1D3E}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>False</EmbedInteropTypes>
</COMReference>
because previously it gave me two problems: 1) every compilation overwrote the new bin/Debug/Interop.PortableDeviceApiLib.dll; 2) It gave me the exception: Impossible to find void PortableDeviceApiLib.IPortableDeviceManager.GetDevices(System.String ByRef, UInt32 ByRef)
Is there anybody to solve the null value always returned for the non-first devices?
try editing the Interop.PortableDeviceApiLib.dll
After you make your edits to your Interop.PortableDeviceApiLib.dll add back the dll and set embedded interop types to false. This should fix the errors you where getting with the GetDevices() function.