Developer technologies | Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Basically I'm working on a small project which involve serial message transfer. I tried GetPortNames() method in C#. It sends Standard Serial Over Bluetooth links instead of Com Ports or USB Ports what to do?
You can get anything with SetupDi APIs and use the right GUID to filter them
For example with GUID_DEVINTERFACE_COMPORT :
//IntPtr hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_USB_HUB, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
IntPtr hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_COMPORT, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDeviceInfo != IntPtr.Zero)
{
var DeviceInfoData = new SP_DEVINFO_DATA() { cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)) };
int nNumberOfDevicesPresent = 0;
while (SetupDiEnumDeviceInfo(hDeviceInfo, nNumberOfDevicesPresent, ref DeviceInfoData))
{
StringBuilder sbDeviceName = new StringBuilder(260);
bool bRet = SetupDiGetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_FRIENDLYNAME, 0, sbDeviceName, (uint)sbDeviceName.Capacity, IntPtr.Zero);
if (bRet)
{
Console.WriteLine("Device Name : {0}", sbDeviceName.ToString());
}
else
{
bRet = SetupDiGetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_DEVICEDESC, 0, sbDeviceName, (uint)sbDeviceName.Capacity, IntPtr.Zero);
if (bRet)
{
Console.WriteLine("Device Description : {0}", sbDeviceName.ToString());
}
}
nNumberOfDevicesPresent++;
}
}
With :
public static Guid GUID_DEVINTERFACE_COMPORT = new Guid("{86E0D1E0-8089-11D0-9CE4-08003E301F73}");
public static Guid GUID_DEVINTERFACE_USB_HUB = new Guid("{f18a0e88-c30c-11d0-8815-00a0c906bed8}");
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hWndParent, int Flags);
public const int DIGCF_PRESENT = 0x00000002;
public const int DIGCF_DEVICEINTERFACE = 0x00000010;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
#if WIN64
public const int PACK_SIZE = 8;
#else
public const int PACK_SIZE = 1;
#endif
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = PACK_SIZE)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public IntPtr Reserved;
}
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property,
uint PropertyRegDataType, StringBuilder PropertyBuffer, uint PropertyBufferSize, IntPtr RequiredSize);
public const int SPDRP_DEVICEDESC = (0x00000000); // DeviceDesc (R/W)
public const int SPDRP_FRIENDLYNAME = (0x0000000C); // FriendlyName (R/W)
public const int SPDRP_LOCATION_INFORMATION = (0x0000000D); // LocationInformation (R/W)