从MTP设备获取信息:“友好名称”始终为空

时间:2016-10-09 14:40:27

标签: c# mtp

我正在使用此项目通过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的结果始终为空。

到目前为止我尝试了什么:

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);

它不接受nullushort[]

我测试了以下内容,只是为了看它的表现如何:

var pDeviceFriendlyName = default(ushort);
var pcchDeviceFriendlyName = default(uint);
GetDeviceFriendlyName(pszPnPDeviceID, ref pDeviceFriendlyName, ref pcchDeviceFriendlyName);

...但它引发了一个例外:"The data is invalid. (Exception from HRESULT: 0x8007000D)"

1 个答案:

答案 0 :(得分:1)

显然Windows不使用"友好名称"将设备显示在"此PC",但"设备型号"代替:

chart  = new QChart;
chartView = new QChartView(chart);
gridLayout->addWidget(chartView,0,0);