这是我再网上找的一段获取串口号的代码,执行后在Combox下拉列表中总有Com3,但实际并没有这个口。高手麻烦指点下int CKZB7Dlg::Getcom()
{
int reg; // CStringArray strCommArr;
HKEY hKey;
int rtn;
//m_cmbComm.ResetContent();
rtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm",
NULL, KEY_READ, &hKey); // 打开串口注册表
if( rtn == ERROR_SUCCESS)
{
int i=0;
char portName[256],commName[256];
DWORD dwLong,dwSize;
while(1)
{
dwSize = sizeof(portName);
dwLong = dwSize;
rtn = RegEnumValue( hKey, i, portName, &dwLong,
NULL, NULL, (PUCHAR)commName, &dwSize );
if( rtn == ERROR_NO_MORE_ITEMS ) // 枚举串口
{
break;
}
// strCommArr.Add(commName);
m_memoCOM.AddString(commName);
i++;
}
reg=i;
}
RegCloseKey(hKey);
return reg;
}
{
int reg; // CStringArray strCommArr;
HKEY hKey;
int rtn;
//m_cmbComm.ResetContent();
rtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm",
NULL, KEY_READ, &hKey); // 打开串口注册表
if( rtn == ERROR_SUCCESS)
{
int i=0;
char portName[256],commName[256];
DWORD dwLong,dwSize;
while(1)
{
dwSize = sizeof(portName);
dwLong = dwSize;
rtn = RegEnumValue( hKey, i, portName, &dwLong,
NULL, NULL, (PUCHAR)commName, &dwSize );
if( rtn == ERROR_NO_MORE_ITEMS ) // 枚举串口
{
break;
}
// strCommArr.Add(commName);
m_memoCOM.AddString(commName);
i++;
}
reg=i;
}
RegCloseKey(hKey);
return reg;
}
// 枚举所有的串口,以及名称
//***********************
int
EnumAllComm( TCHAR *buf )
{
HKEY hkey;
int result;
int i = 0; *buf = 0; result = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T( "Hardware\\DeviceMap\\SerialComm" ),
NULL,
KEY_READ,
&hkey ); if( ERROR_SUCCESS == result ) // 打开串口注册表
{
TCHAR portName[ 0x100 ], commName[ 0x100 ];
DWORD dwLong, dwSize; do
{
dwSize = sizeof( portName ) / sizeof( TCHAR );
dwLong = dwSize;
result = RegEnumValue( hkey, i, portName, &dwLong, NULL, NULL, ( LPBYTE )commName, &dwSize );
if( ERROR_NO_MORE_ITEMS == result )
{
// 枚举串口
break; // commName就是串口名字
}
_tcscpy( buf, commName );
buf += ( _tcslen( buf ) + 1 );
i++;
} while ( 1 ); RegCloseKey( hkey );
} *buf = 0; return i;
}
// 使用
m_comselect.ResetContent(); comcount = EnumAllComm( comname );
p = comname;
while( *p )
{
m_comselect.AddString( p );
p += ( _tcslen( p ) + 1 );
}
,就好了,谢谢楼上的
ZeroMemory(commName, sizeof(commName));