我使用這個項目通過MTP來獲取數據:充分利用MTP設備信息:「友好名稱」始終是空
https://github.com/notpod/wpd-lib
我的問題:「友好名稱」的設備始終是空的。 Windows在「This PC」下顯示一個友好名稱,所以它應該是可行的。
這是提到GitHub的項目是如何嘗試檢索友好名稱(從文件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);
如前所述的friendlyName
結果總是空的。
我迄今爲止嘗試:
在this post我發現這個其他可能的解決方案,它使用PortableDeviceManagerClass
代替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;
}
這裏的問題是,我似乎有一個不同的簽名GetDeviceFriendlyName
(不同的Interop.PortableDeviceApiLib.dll
?)。這是我的:
void GetDeviceFriendlyName(string pszPnPDeviceID, ref ushort pDeviceFriendlyName, ref uint pcchDeviceFriendlyName);
它不接受null
或ushort[]
。
我測試了以下,只是爲了看看它是如何表現:
var pDeviceFriendlyName = default(ushort);
var pcchDeviceFriendlyName = default(uint);
GetDeviceFriendlyName(pszPnPDeviceID, ref pDeviceFriendlyName, ref pcchDeviceFriendlyName);
...但它引發了異常:"The data is invalid. (Exception from HRESULT: 0x8007000D)"
。