Reputation: 7909
I am using this project to fetch data via MTP:
https://github.com/notpod/wpd-lib
My problem: the "friendly name" of the device is always empty. Windows shows a friendly name under "This PC", so it should be feasible.
This is how the mentioned github project tries to retrieve the friendly name (from the file WindowsPortableDevice.cs):
var WPD_DEVICE_FRIENDLY_NAME = new PortableDeviceApiLib._tagpropertykey();
WPD_DEVICE_FRIENDLY_NAME.fmtid = new Guid(0x26D4979A, 0xE643, 0x4626, 0x9E, 0x2B, 0x73, 0x6D, 0xC0, 0xC9, 0x2F, 0xDC);
WPD_DEVICE_FRIENDLY_NAME.pid = 12;
string friendlyName;
propertyValues.GetStringValue(ref DevicePropertyKeys.WPD_DEVICE_FRIENDLY_NAME, out friendlyName);
As stated before, the result of friendlyName
is always empty.
What I have tried so far:
In this post I found this other possible solution, which uses the PortableDeviceManagerClass
instead of the PortableDeviceClass
:
string RetrieveFriendlyName(
PortableDeviceApiLib.PortableDeviceManagerClass PortableDeviceManager,
string PnPDeviceID)
{
uint cFriendlyName = 0;
ushort[] usFriendlyName;
string strFriendlyName = String.Empty;
// First, pass NULL as the LPWSTR return string parameter to get the total number
// of characters to allocate for the string value.
PortableDeviceManager.GetDeviceFriendlyName(PnPDeviceID, null, ref cFriendlyName);
// Second allocate the number of characters needed and retrieve the string value.
usFriendlyName = new ushort[cFriendlyName];
if (usFriendlyName.Length > 0)
{
PortableDeviceManager.GetDeviceFriendlyName(PnPDeviceID, usFriendlyName, ref cFriendlyName);
// We need to convert the array of ushorts to a string, one
// character at a time.
foreach (ushort letter in usFriendlyName)
if (letter != 0)
strFriendlyName += (char)letter;
// Return the friendly name
return strFriendlyName;
}
else
return null;
}
The problem here is that I seem to have a different signature of GetDeviceFriendlyName
(different Interop.PortableDeviceApiLib.dll
?). This is mine:
void GetDeviceFriendlyName(string pszPnPDeviceID, ref ushort pDeviceFriendlyName, ref uint pcchDeviceFriendlyName);
It does not accept null
or ushort[]
.
I tested the following, just to see how it behaved:
var pDeviceFriendlyName = default(ushort);
var pcchDeviceFriendlyName = default(uint);
GetDeviceFriendlyName(pszPnPDeviceID, ref pDeviceFriendlyName, ref pcchDeviceFriendlyName);
...but it threw an exception: "The data is invalid. (Exception from HRESULT: 0x8007000D)"
.
Upvotes: 0
Views: 1013
Reputation: 7909
Apparently Windows does not use "friendly name" to show the device on "This PC", but "device model" instead:
WPD_DEVICE_MODEL.fmtid = new Guid(0x26D4979A, 0xE643, 0x4626, 0x9E, 0x2B, 0x73, 0x6D, 0xC0, 0xC9, 0x2F, 0xDC);
WPD_DEVICE_MODEL.pid = 8;
Upvotes: 1