Jon Trauntvein Jon Trauntvein - 1 year ago 154
C++ Question

Why does GetDefaultCommConfig fail on windows 10

I use the following code to verify that a serial port name is valid on the computer:

typedef std::pair<StrAsc const, bool> port_pair_type;
typedef std::list<port_pair_type> port_pairs_type;
port_pairs_type pairs;
StrBin config_buffer;
port_pair_type pair(pairs.front());
// we need to get the default configuration for the port. This may
// require some fudging on the buffer size. That is why two calls
// are being made.
uint4 config_size = config_buffer.length();
StrUni temp(pair.first);
COMMCONFIG *config(reinterpret_cast<COMMCONFIG *>(config_buffer.getContents_writable()));
config->dwSize = sizeof(COMMCONFIG);
rcd = GetDefaultCommConfigW(
temp.c_str(), config, &config_size);
if(!rcd && config_buffer.length() < config_size)
config_buffer.fill(0, config_size);
config = reinterpret_cast<COMMCONFIG *>(config_buffer.getContents_writable());
config->dwSize = sizeof(COMMCONFIG);
rcd = GetDefaultCommConfigW(
reinterpret_cast<COMMCONFIG *>(config_buffer.getContents_writable()),

// if the call succeeded, we can go ahead and look at the
// configuration structure.
COMMCONFIG const *config = reinterpret_cast<COMMCONFIG const *>(
if(config->dwProviderSubType == PST_RS232)
OsException error("GetDefaultCommConfig Failed");
trace("\"%s\"", error.what());

On windows 10, when trying to confirm a serial port that uses usbser.sys, the call to GetDefaultCommConfig() is failing and the error code returned by GetLastError() is 87 (invalid parameter). As I am aware, the usbser.sys driver has been rewritten on windows 10 and I suspect that this is a problem with that driver. Does anyone else have an idea of what might be going wrong?

Answer Source

This had been a bug in usbser.sys and was fixed with the Windows 10 Update KB3124262 from 27.01.2016.

The Microsoft employee explained:

The COM port name in the HKLM\HARDWARE\DEVICEMAP\SERIALCOMM registry is not NULL terminated.

Related discussion on MSDN

Because of Windows 10's update policies this issue should not appear in the future anymore.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download