获取计算机所有串口的VC代码
多数涉及串口操作的软件,在让用户选择串口时,只会机械的给出从COM1到COM16的组合框。但实际上可能有大于COM16的串口号,于是想编程来获取实际的串口号再列出来。看起来很难,其实关于串口的信息保存了注册表中,代码如下:
void CPageSetCom::ShowComm()
{
long lReg;
HKEY hKey;
DWORD MaxValueLength;
DWORD dwValueNumber;
lReg=RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
if(lReg!=ERROR_SUCCESS)
{
AfxMessageBox(L"Open Registry Error!\n");
return;
}
lReg=RegQueryInfoKeyA(hKey,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&dwValueNumber, //返回和hKey关联的值
&MaxValueLength,
NULL,
NULL,
NULL);
if(lReg!=ERROR_SUCCESS) //没有成功
{
AfxMessageBox(L"Getting Info Error!\n");
return;
}
LPSTR pValueName,pCOMNumber;
DWORD cchValueName,dwValueSize=6;
for(DWORD i=0;i < dwValueNumber;i++)
{
cchValueName=MaxValueLength+1;
dwValueSize=6;
pValueName=(LPSTR)VirtualAlloc(NULL,cchValueName,MEM_COMMIT,PAGE_READWRITE);
lReg=RegEnumValueA(hKey,
i,
pValueName,
&cchValueName,
NULL,
NULL,
NULL,
NULL);
if((lReg!=ERROR_SUCCESS)&&(lReg!=ERROR_NO_MORE_ITEMS))
{
AfxMessageBox(L"Enum Registry Error or No More Items!\n");
continue;
}
pCOMNumber=(LPSTR)VirtualAlloc(NULL,6,MEM_COMMIT,PAGE_READWRITE);
lReg=RegQueryValueExA(hKey,
pValueName,
NULL,
NULL,
(LPBYTE)pCOMNumber,
&dwValueSize);
if(lReg!=ERROR_SUCCESS)
{
AfxMessageBox(L"Can not get the name of the port");
continue;
}
CString strCommList;
//AfxMessageBox(pCOMNumber);
CharToUnicode(pCOMNumber,&strCommList);
//m_ctlPort.AddString(strCommList);
BOOL m_bInsert=0;
if(((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->GetCount()==0)
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->AddString(strCommList);
else
{
CString strTemp=strCommList;
strCommList.TrimLeft(L"COM");
for(int icurrent=0;icurrent<((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->GetCount();icurrent++)
{
CString strCurrent;
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->GetLBText(icurrent,strCurrent);
strCurrent.TrimLeft(L"COM");
if(_ttol(strCurrent)>_ttol(strCommList))
{
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->InsertString(icurrent,strTemp);
m_bInsert = 1;
break;
}
}
if(!m_bInsert)
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->InsertString(icurrent,strTemp);
}
VirtualFree(pValueName,0,MEM_RELEASE);
VirtualFree(pCOMNumber,0,MEM_RELEASE);
}
}
int CPageSetCom::CharToUnicode(char *pchIn, CString *pstrOut)
{
int nLen;
WCHAR *ptch;
if(pchIn == NULL)
{
return 0;
}
nLen = MultiByteToWideChar(CP_ACP, 0, pchIn, -1, NULL, 0);
ptch = new WCHAR[nLen];
MultiByteToWideChar(CP_ACP, 0, pchIn, -1, ptch, nLen);
pstrOut->Format(_T("%s"), ptch);
delete [] ptch;
return nLen;
}
void CPageSetCom::ShowComm()
{
long lReg;
HKEY hKey;
DWORD MaxValueLength;
DWORD dwValueNumber;
lReg=RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
if(lReg!=ERROR_SUCCESS)
{
AfxMessageBox(L"Open Registry Error!\n");
return;
}
lReg=RegQueryInfoKeyA(hKey,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&dwValueNumber, //返回和hKey关联的值
&MaxValueLength,
NULL,
NULL,
NULL);
if(lReg!=ERROR_SUCCESS) //没有成功
{
AfxMessageBox(L"Getting Info Error!\n");
return;
}
LPSTR pValueName,pCOMNumber;
DWORD cchValueName,dwValueSize=6;
for(DWORD i=0;i < dwValueNumber;i++)
{
cchValueName=MaxValueLength+1;
dwValueSize=6;
pValueName=(LPSTR)VirtualAlloc(NULL,cchValueName,MEM_COMMIT,PAGE_READWRITE);
lReg=RegEnumValueA(hKey,
i,
pValueName,
&cchValueName,
NULL,
NULL,
NULL,
NULL);
if((lReg!=ERROR_SUCCESS)&&(lReg!=ERROR_NO_MORE_ITEMS))
{
AfxMessageBox(L"Enum Registry Error or No More Items!\n");
continue;
}
pCOMNumber=(LPSTR)VirtualAlloc(NULL,6,MEM_COMMIT,PAGE_READWRITE);
lReg=RegQueryValueExA(hKey,
pValueName,
NULL,
NULL,
(LPBYTE)pCOMNumber,
&dwValueSize);
if(lReg!=ERROR_SUCCESS)
{
AfxMessageBox(L"Can not get the name of the port");
continue;
}
CString strCommList;
//AfxMessageBox(pCOMNumber);
CharToUnicode(pCOMNumber,&strCommList);
//m_ctlPort.AddString(strCommList);
BOOL m_bInsert=0;
if(((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->GetCount()==0)
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->AddString(strCommList);
else
{
CString strTemp=strCommList;
strCommList.TrimLeft(L"COM");
for(int icurrent=0;icurrent<((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->GetCount();icurrent++)
{
CString strCurrent;
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->GetLBText(icurrent,strCurrent);
strCurrent.TrimLeft(L"COM");
if(_ttol(strCurrent)>_ttol(strCommList))
{
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->InsertString(icurrent,strTemp);
m_bInsert = 1;
break;
}
}
if(!m_bInsert)
((CComboBox*)GetDlgItem(IDC_CMBREADERCOM))->InsertString(icurrent,strTemp);
}
VirtualFree(pValueName,0,MEM_RELEASE);
VirtualFree(pCOMNumber,0,MEM_RELEASE);
}
}
int CPageSetCom::CharToUnicode(char *pchIn, CString *pstrOut)
{
int nLen;
WCHAR *ptch;
if(pchIn == NULL)
{
return 0;
}
nLen = MultiByteToWideChar(CP_ACP, 0, pchIn, -1, NULL, 0);
ptch = new WCHAR[nLen];
MultiByteToWideChar(CP_ACP, 0, pchIn, -1, ptch, nLen);
pstrOut->Format(_T("%s"), ptch);
delete [] ptch;
return nLen;
}