このプロジェクトを使用して、MTP 経由でデータを取得しています。
https://github.com/notpod/wpd-lib
私の問題: デバイスの「フレンドリ名」は常に空です。Windows では「この 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
は常に空です。
私がこれまでに試したこと:
この投稿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)"
.