Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I hope someone can help.

I been struggling for days on this function, and it is quite hard to get an answer from the internet.

I am trying to get the location information of the child usb device, using CM_Get_DevNode_Property.

It works perfectly ok on Windows 7 x64, but fails on Windows 7 x86, with return code : CR_INVALID_FLAG

I searched and searched on the internet, but cannot find a good answer.

I do hope someone can.

Below is the code function, i used to get the location information. Thank you very much in advance.

Have a good day.

C#
/// <summary>
/// Enumerates the node.
/// </summary>
internal void EnumerateNode()
{
    uint inst;
    int result = NativeMethods.CM_Get_Child(out inst, this.DevicePtr, 0);

    while (inst != 0)
    {
        int nBytes = 4096;
        IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(nBytes);
        try
        {
            NativeMethods.CM_Get_Device_ID(inst, ptrInstanceBuf, nBytes, 0);
            string instanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

            IntPtr locationBuffer = Marshal.AllocHGlobal(nBytes);
            try
            {
                ulong size = (ulong)nBytes;
                NativeMethods.DevPropKey location = new NativeMethods.DevPropKey();
                location.guid = new Guid(0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0);
                location.id = NativeMethods.Device_LocationInfoId;
                ulong propType = 0;
                string locationInfo = string.Empty;
                if (NativeMethods.CM_Get_DevNode_Property(inst, location, out propType, locationBuffer, ref size, 0) == 0)
                {
                    locationInfo = Marshal.PtrToStringUni(locationBuffer);
                }

                UsbNode node = new UsbNode(instanceID, locationInfo, inst);
                this.UsbNodes.Add(node);
                node.EnumerateNode();

                NativeMethods.CM_Get_Sibling(out inst, inst, 0);
            }
            finally
            {
                Marshal.FreeHGlobal(locationBuffer);
            }
        }
        finally
        {
            Marshal.FreeHGlobal(ptrInstanceBuf);
        }
    }
}
Posted

1 solution

Maybe you can try what Microsoft recommends.

Remarks



"This function is reserved for system use. Do not use this function in your class installers, co-installers, or device installation applications. Use SetupDiGetDeviceProperty instead."

Perhaps you have already tried this path.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900