系统中有多少串口可以使用?串口必须在系统中注册,所以打开注册表找到相关
的键值就可以。下面是一个打开注册表并读取数值的函数。
// 返回值为串口数量
//返回strComArray为串口名
int CComDemoDlg::GetComName(CString * strComArray)
{
int i=0;
if(!(GetVersion() & 0x80000000))
{// Windows NT code
HKEY hHardwareKey;//, hControlKey;
long lRc;
DWORD dwDataType,dwLength, dwIndex, dwValueName;
char cValueName[50];
lRc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"HARDWARE//DEVICEMAP//SERIALCOMM",
0,
KEY_READ,
&hHardwareKey);
if(lRc != ERROR_SUCCESS)
return -1;//打开注册表失败!
//
char cCheckName[10]={0};
//
for(dwIndex = 0; ; dwIndex++)
{
dwLength = 10;
dwValueName = sizeof(cValueName);
lRc = RegEnumValue(
hHardwareKey, // handle to key to
query
dwIndex, // index of
value to query
cValueName, // address of
buffer for value string
&dwValueName, // address for size of
value buffer
NULL, // reserved
&dwDataType, // address of buffer
for type code
(unsigned char *)cCheckName, //
address of buffer for value data
&dwLength
// address for size of data buffer
);
//
if(lRc != ERROR_SUCCESS)
{
RegCloseKey(hHardwareKey);
return i;
}
//
strComArray[i]=cCheckName;
i++;
}
//
RegCloseKey(hHardwareKey);
}
return i;
}