zoukankan      html  css  js  c++  java
  • 获取usb设备父系或子代

    /// <summary>
            /// 获取设备父系
            /// </summary>
            /// <param name="driver"></param>
            /// <returns></returns>
            private static bool TryGetDriverIdParent(string driver, out string resultDeviceID)
            {
                resultDeviceID = "";
                try
                {
                    int CM_LOCATE_DEVNODE_NORMAL = 0x00000000;
                    int CR_SUCCESS = 0x00000000;
                    UInt32 parentInst;
                    int curInst = 0;
                    int pLen = 0;
                    int apiResult = Win32.CM_Locate_DevNodeA(ref curInst, driver, CM_LOCATE_DEVNODE_NORMAL);
                    if (apiResult != CR_SUCCESS)
                    {
                        return false;
                    }
                    apiResult = Win32.CM_Get_Parent(out parentInst, (UInt32)curInst, CM_LOCATE_DEVNODE_NORMAL);
                    if (apiResult != CR_SUCCESS)
                    {
                        return false;
                    }
                    apiResult = Win32.CM_Get_Device_ID_Size(out pLen, parentInst, CM_LOCATE_DEVNODE_NORMAL);
                    if (apiResult != CR_SUCCESS)
                    {
                        return false;
                    }
                    char[] ptrInstanceBuf = new char[33];
                    //获取设备id字符串地址
                    apiResult = Win32.CM_Get_Device_ID(parentInst, ptrInstanceBuf, pLen, 0);
                    if (apiResult != CR_SUCCESS)
                    {
                        return false;
                    }
                    resultDeviceID = new string(ptrInstanceBuf);
                    return true;
                }
                catch (Exception ecException)
                {
                    LogHelper.WriteErrorLog("获取父代" + ecException.Message, ecException);
                    return false;
                }
            }
    
     [DllImport("setupapi.dll", SetLastError = true)]
            public static extern int CM_Locate_DevNodeA(ref int pdnDevInst, string pDeviceID, int ulFlags);
            [DllImport("setupapi.dll")]
            public static extern int CM_Get_Parent(out UInt32 pdnDevInst, UInt32 dnDevInst, int ulFlags);
            [DllImport("setupapi.dll", SetLastError = true)]
            public static extern int CM_Get_Device_ID_Size(out int pulLen, UInt32 dnDevInst, int flags = 0);
            [DllImport("setupapi.dll", CharSet = CharSet.Unicode)]
            public static extern int CM_Get_Device_ID(UInt32 dnDevInst, char[] buffer, int bufferLen, int flags);
  • 相关阅读:
    几款国产的安卓系统
    微软所有正版软件下载网站ITELLYOU
    微软所有正版软件下载网站ITELLYOU
    键盘大小写状态显示(Softswitch) v2.3汉化绿色版
    牛顿定律的一些思考
    查看接地有没有ok
    Adobe Audition CC 2018软件安装
    声音的掩蔽效应的一些思考
    声学测量的基本量
    The Mean of the Sample Mean|Standard Deviation of the Sample Mean|SE
  • 原文地址:https://www.cnblogs.com/daxiongblog/p/6134386.html
Copyright © 2011-2022 走看看