GetDefaultCommConfig (kernel32)
Last changed: -204.238.102.222

.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern bool GetDefaultCommConfig(string lpszName, [In, Out] ref COMMCONFIG lpCC,
   ref uint lpdwSize);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

        [DllImport("kernel32.dll")]
        static extern bool GetDefaultCommConfig(string lpszName, [In, Out] ref COMMCONFIG lpCC, ref uint lpdwSize);

        [StructLayout(LayoutKind.Sequential)]
            internal struct DCB
        {
            public Int32 DCBLength;
            public Int32 BaudRate;
            public Int32 fBitField;
            public Int16 wReserved;
            public Int16 XonLim;
            public Int16 XoffLim;
            public Byte ByteSize;
            public Byte Parity;
            public Byte StopBits;
            public Char XonChar;
            public Char XoffChar;
            public Char ErrorChar;
            public Char EofChar;
            public Char EvtChar;
            public Int32 wReserved1;
        }

        [StructLayout(LayoutKind.Sequential)]
            internal struct COMMCONFIG
        {
            public Int32 dwSize;
            public Int16 wVersion;
            public Int16 wReserved;
            public DCB dcb;
            public Int32 dwProviderSubType;
            public Int32 dwProviderOffset;
            public Int32 dwProviderSize;
            public string wcProviderData;
        }

        private void ScanCommPorts(int maxComPortNumber)
        {
            COMMCONFIG cc = new COMMCONFIG();
            uint sz = (uint)Marshal.SizeOf(cc);

            for(int i=1; i<=maxComPortNumber; i++)
            {
                string comPortName = string.Format("COM{0}", i);
                bool bSuccess = GetDefaultCommConfig(comPortName, ref cc, ref sz);
                Console.WriteLine(string.Format("{0}: {1}", comPortName, bSuccess ? "Valid" : "Invalid"));
            }
        }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation