private static Boolean GetConnectedStatus(IntPtr hdi, SetupAPI.SP_DEVINFO_DATA devInfoData) { Int32 DN_DEVICE_DISCONNECTED = 0x2000000; UInt64 ulPropertyType = new UInt64(); Int32 dwSize = 4; byte[] devst = new byte[4]; SetupAPI.DEVPROPKEY key = new SetupAPI.DEVPROPKEY(); key.fmtid = Guid.Parse("{4340a6c5-93fa-4706-972c-7b648008a5a7}"); key.pid = 2; while (!SetupAPI.SetupDiGetDevicePropertyW(hdi, ref devInfoData, ref key, ref ulPropertyType, devst, dwSize, ref dwSize, 0)) { int err = Marshal.GetLastWin32Error(); if (err == Kernel32.ERROR_INSUFFICIENT_BUFFER) { devst = new byte[dwSize]; } else if (err == Kernel32.ERROR_INVALID_DATA) { break; } else { throw new Exception(err.ToString()); } } int status = BitConverter.ToInt32(devst); return(!Convert.ToBoolean(status & DN_DEVICE_DISCONNECTED)); }
public static void GetDeviceNames(Guid guid) { IntPtr hdi = SetupAPI.SetupDiGetClassDevsW(ref guid, null, IntPtr.Zero, SetupAPI.DIGCF_PRESENT); SetupAPI.SP_DEVINFO_DATA devInfoData = new SetupAPI.SP_DEVINFO_DATA(); devInfoData.cbSize = Marshal.SizeOf(devInfoData); int i = 0; while (SetupAPI.SetupDiEnumDeviceInfo(hdi, i, ref devInfoData)) { i++; String hwid = GetDeviceInfo(hdi, devInfoData, SetupAPI.SPDRP_HARDWAREID); if (hwid.StartsWith("BTHLE\\Dev_")) { String friendlyName = GetDeviceInfo(hdi, devInfoData, SetupAPI.SPDRP_FRIENDLYNAME); String deviceDesc = GetDeviceInfo(hdi, devInfoData, SetupAPI.SPDRP_DEVICEDESC); String enumeratorName = GetDeviceInfo(hdi, devInfoData, SetupAPI.SPDRP_ENUMERATOR_NAME); String status = GetConnectedStatus(hdi, devInfoData) ? "connected" : "paired"; Console.WriteLine("hwid: {0}\nfriendlyName: {1}\ndeviceDesc: {2}\nenumeratorName: {3}\nstatus: {4}\n\n", hwid, friendlyName, deviceDesc, enumeratorName, status); } } }
public static void GetDevInfo(Guid guid) { IntPtr hdi = SetupAPI.SetupDiGetClassDevsW(ref guid, null, IntPtr.Zero, SetupAPI.DIGCF_PRESENT | SetupAPI.DIGCF_DEVINTERFACE); Console.WriteLine(hdi); SetupAPI.SP_DEVINFO_DATA dd = new SetupAPI.SP_DEVINFO_DATA(); dd.cbSize = Marshal.SizeOf(dd); SetupAPI.SP_DEVICE_INTERFACE_DATA did = new SetupAPI.SP_DEVICE_INTERFACE_DATA(); did.cbSize = Marshal.SizeOf(did); int i = 0; while (SetupAPI.SetupDiEnumDeviceInterfaces(hdi, IntPtr.Zero, ref guid, i, ref did)) { i += 1; SetupAPI.SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SetupAPI.SP_DEVICE_INTERFACE_DETAIL_DATA(); didd.cbSize = IntPtr.Size == 8 ? 8 : 4 + Marshal.SystemDefaultCharSize; UInt32 nRequiredSize = 0; SetupAPI.SetupDiGetDeviceInterfaceDetailW(hdi, ref did, ref didd, 256, ref nRequiredSize, ref dd); Console.WriteLine(didd.DevicePath); Communication(didd.DevicePath); } Console.WriteLine("\n\n"); }
private static String GetDeviceInfo(IntPtr hdi, SetupAPI.SP_DEVINFO_DATA devInfoData, Int32 propertyName) { StringBuilder buffer = new StringBuilder(); Int32 bufferSize = 0; while (!SetupAPI.SetupDiGetDeviceRegistryPropertyW(hdi, ref devInfoData, propertyName, IntPtr.Zero, buffer, bufferSize, ref bufferSize)) { int err = Marshal.GetLastWin32Error(); if (err == Kernel32.ERROR_INSUFFICIENT_BUFFER) { buffer = new StringBuilder(bufferSize); } else if (err == Kernel32.ERROR_INVALID_DATA) { break; } else { throw new Exception(err.ToString()); } } return(buffer.ToString()); }