public static string GetPnPDeviceId(NetworkInterface ni) { Guid classGuid = GUID_DEVINTERFACE_NET; IntPtr hwndParent = IntPtr.Zero; Int32 flags = Win32DeviceMgmt.DIGCF_DEVICEINTERFACE | Win32DeviceMgmt.DIGCF_PRESENT; IntPtr pDevInfoSet = IntPtr.Zero; IntPtr pNewDevInfoSet = IntPtr.Zero; try { pNewDevInfoSet = Win32DeviceMgmt.SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, hwndParent, flags); //, pDevInfoSet, strMachineName, IntPtr.Zero); if (pNewDevInfoSet == IntPtr.Zero) { Logger.Log("Failed to get device information list"); return(""); } Int32 iRet; Int32 iMemberIndex = 0; do { Win32DeviceMgmt.SP_DEVINFO_DATA devInfoData = new Win32DeviceMgmt.SP_DEVINFO_DATA(); devInfoData.ClassGuid = Guid.Empty; devInfoData.DevInst = 0; devInfoData.Reserved = UIntPtr.Zero; devInfoData.cbSize = Marshal.SizeOf(devInfoData); iRet = Win32DeviceMgmt.SetupDiEnumDeviceInfo(pNewDevInfoSet, iMemberIndex, ref devInfoData); if (iRet == 0) { Int32 iLastError = Win32DeviceMgmt.GetLastError(); if (iLastError == Win32DeviceMgmt.ERROR_NO_MORE_FILES) { //Console.WriteLine("No more devices in list"); break; } else { iMemberIndex++; continue; } } string desc = GetDevicePropertyString(pNewDevInfoSet, devInfoData, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_DEVICEDESC); if (ni.Description.Equals(desc)) { return(GetDeviceInstanceId(pNewDevInfoSet, devInfoData)); } string friendly = GetDevicePropertyString(pNewDevInfoSet, devInfoData, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_FRIENDLYNAME); if (ni.Description.Equals(friendly)) { return(GetDeviceInstanceId(pNewDevInfoSet, devInfoData)); } iMemberIndex++; } while (true); return(""); } finally { Win32DeviceMgmt.SetupDiDestroyDeviceInfoList(pNewDevInfoSet); } }
static String GetDeviceInstanceId(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData) { StringBuilder strId = new StringBuilder(0); Int32 iRequiredSize = 0; Int32 iSize = 0; Int32 iRet = Win32DeviceMgmt.SetupDiGetDeviceInstanceId(DeviceInfoSet, ref DeviceInfoData, strId, iSize, ref iRequiredSize); strId = new StringBuilder(iRequiredSize); iSize = iRequiredSize; iRet = Win32DeviceMgmt.SetupDiGetDeviceInstanceId(DeviceInfoSet, ref DeviceInfoData, strId, iSize, ref iRequiredSize); if (iRet == 1) { return(strId.ToString()); } return(String.Empty); }
static byte[] GetDeviceProperty(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property) { StringBuilder strId = new StringBuilder(0); byte[] ptrBuf = null; UInt32 RegType; UInt32 iRequiredSize = 0; UInt32 iSize = 0; bool iRet = Win32DeviceMgmt.SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, (uint)property, out RegType, ptrBuf, iSize, out iRequiredSize); ptrBuf = new byte[iRequiredSize]; iSize = iRequiredSize; iRet = Win32DeviceMgmt.SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, (uint)property, out RegType, ptrBuf, iSize, out iRequiredSize); if (iRet) { return(ptrBuf); } return(new byte[0]); }
static Guid GetDevicePropertyGuid(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property) { byte[] ptrBuf = GetDeviceProperty(DeviceInfoSet, DeviceInfoData, property); return(new Guid(ptrBuf)); }
static String GetDevicePropertyString(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property) { byte[] ptrBuf = GetDeviceProperty(DeviceInfoSet, DeviceInfoData, property); return(ptrBuf.ToStrAuto()); }