Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“friendly name” from the device is always empty #1

Open
xavierpena opened this issue Oct 9, 2016 · 2 comments
Open

“friendly name” from the device is always empty #1

xavierpena opened this issue Oct 9, 2016 · 2 comments

Comments

@xavierpena
Copy link

Hi,

I've just posted the same question in StackOverflow, but since it concerns your project I wanted to know if you have any solution for this.

My problem: the "friendly name" of the device is always empty. Windows shows a friendly name under "This PC", so it should be feasible.

I have checked similar solutions like this one here based on the same source, but they have the same issue.

This is how your 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)".

@xavierpena
Copy link
Author

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;

@gabrielepmattia
Copy link

gabrielepmattia commented Oct 23, 2016

Yes, I confirm what @xavierpena said. We can change friendly name to model (gabrielepmattia@f0c1b00) or we can add a new field called model and give there the device model.

EDIT: Definitely the second option is better, even for maintaining compatibility gabrielepmattia@ab2bdeb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants