通过注册表枚举串口

CStringArray m_Serial_Port;

BOOL Enum_Serial_Port() //枚举串口
{
	m_Serial_Port.RemoveAll();

	HKEY hKey;
	LPCTSTR data_Set = _T("HARDWARE\\DEVICEMAP\\SERIALCOMM\\");
	LONG ret0 = RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, &hKey);
	if(ret0 != ERROR_SUCCESS)
	{
		AfxMessageBox("错误:无法打开有关的hKEY");
		return FALSE;
	}

	DWORD dwIndex = 0;
	while(1)
	{
		LONG Status;
		CHAR Name[256]={0};
		UCHAR szPortName[80]={0};
		DWORD dwName;
		DWORD dwSizeofPortName;
		DWORD Type;
		dwName = sizeof(Name);
		dwSizeofPortName = sizeof(szPortName);
		Status = RegEnumValue(hKey, dwIndex++, Name, &dwName, NULL, &Type,
			szPortName, &dwSizeofPortName);

		if((Status == ERROR_SUCCESS)||(Status == ERROR_MORE_DATA))
		{
			CString str = szPortName;
			m_Serial_Port.Add(str);		
		}
		else
		{
			break;
		}
	}
	RegCloseKey(hKey);

	return(m_Serial_Port.GetSize() > 0 ? TRUE:FALSE);
}


 

你可能感兴趣的:(null)