检索全部COM
HKEY hKey;
LONG ret;
OSVERSIONINFO osvi;
BOOL bOsVersionInfoEx;
char keyinfo[100],comm_name[200],ValueName[200];
int i;
DWORD sType,Reserved,cbData,cbValueName;
hIcon=AfxGetApp()->LoadIcon(IDI_HARDWARE);
SetIcon(hIcon,false);
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
memset(keyinfo,0,100);
strcpy(keyinfo,"HARDWARE//DEVICEMAP//SERIALCOMM");
i=0; sType=REG_SZ;Reserved=0;
bOsVersionInfoEx =GetVersionEx(&osvi);
ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyinfo,0,KEY_ALL_ACCESS,&hKey);
if (ret==ERROR_SUCCESS){
// 10-25
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
for(i=1;i<=128;i++)
{
sprintf(comm_name,"COM%d",i);
if (CommPortIsUsed(comm_name)==1) m_comlist.AddString(comm_name);
}
}
else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
do
{
cbData=200;cbValueName=200;
memset(comm_name,0,200);
memset(ValueName,0,200);
ret=RegEnumValue(hKey,i,ValueName,&cbValueName,NULL,&sType,(LPBYTE)comm_name,&cbData);
if (ret==ERROR_SUCCESS)
{
// comm_name就是COM的信息
}while (ret==ERROR_SUCCESS);
}
}
RegCloseKey(hKey);
获取MODEM信息可以用RAS_PAI。
以下2个函数需要包含
#include <ras.h>
#include <raserror.h>
long CRasClient::GetModemCount()
{
DWORD dwSize = 0;
DWORD dwNumOfDevices = 0;
DWORD dwRV = RasEnumDevices(NULL, &dwSize, &dwNumOfDevices);
RASDEVINFO *lpRdi = new RASDEVINFO[dwNumOfDevices];
lpRdi->dwSize = sizeof(*lpRdi);
dwRV = RasEnumDevices(lpRdi, &dwSize, &dwNumOfDevices);
if(dwRV != 0)
{
delete []lpRdi;
return -1;
}
CString strType;
int nModemCount = 0;
for(int i = 0; i < (int)dwNumOfDevices; i++)
{
strType = lpRdi[i].szDeviceType;
if(strType.CompareNoCase("MODEM") == 0) // = "RASDT_Modem";
nModemCount++;
}
delete []lpRdi;
return nModemCount;
}
BOOL CRasClient::GetModemName(CString* strModemNameArray)
{
DWORD dwSize = 0;
DWORD dwNumOfDevices = 0;
DWORD dwRV = RasEnumDevices(NULL, &dwSize, &dwNumOfDevices);
RASDEVINFO *lpRdi = new RASDEVINFO[dwNumOfDevices];
lpRdi->dwSize = sizeof(*lpRdi);
dwRV = RasEnumDevices(lpRdi, &dwSize, &dwNumOfDevices);
if(dwRV != 0)
{
delete []lpRdi;
return FALSE;
}
CString strType;
int j = 0;
for(int i = 0; i < (int)dwNumOfDevices; i++)
{
strType = lpRdi[i].szDeviceType;
if(strType.CompareNoCase("MODEM") == 0) // = "RASDT_Modem";
{
strModemNameArray[j] = lpRdi[i].szDeviceName;
j++;
}
}
delete []lpRdi;
return TRUE;
}
补充一下,获取COM信息的程序只适用于NT/2000/XP,所以if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
for(i=1;i<=128;i++)
{
sprintf(comm_name,"COM%d",i);
if (CommPortIsUsed(comm_name)==1) m_comlist.AddString(comm_name);
}
}
这段代码不要
taianmonkey() :
检测COM口:
CString strCom;
int nCom = 0;
int count = 0;
HANDLE hCom;
do {
nCom++;
strCom.Format("COM%d", nCom);
hCom = CreateFile(strCom, 0, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(INVALID_HANDLE_VALUE == hCom )
break;
m_cbComList.AddString(strCom);
CloseHandle(hCom);
} while(1);
以上方法很准确!
zhangnanonnet:
see the function:
i think you must create the connection for each modem, like "我的连接" or other,then you use the connection to get the RASENTRY* lpRasEntry.
i think strEntry should be like "我的连接"
DWORD GetEntryProperties(CString strEntry, RASENTRY* lpRasEntry
, LPTSTR lpszPhoneBook)
{
LPTSTR lpszEntry = strEntry.GetBuffer(1);
strEntry.ReleaseBuffer();
if(RasValidateEntryName(lpszPhoneBook, lpszEntry) == ERROR_ALREADY_EXISTS)
{
BYTE bDeviceInfo = NULL;
DWORD dwDeviceInfoSize = sizeof(bDeviceInfo);
DWORD dwEntrySize = 0;
DWORD dwRV;
lpRasEntry->dwSize = sizeof(*lpRasEntry);
dwRV = RasGetEntryProperties(lpszPhoneBook, lpszEntry, lpRasEntry, &dwEntrySize,
&bDeviceInfo, &dwDeviceInfoSize); // GET 603
dwRV = RasGetEntryProperties(lpszPhoneBook, lpszEntry, lpRasEntry, &dwEntrySize,
&bDeviceInfo, &dwDeviceInfoSize); // GET 0
return dwRV;
}
else
return ERROR_ENTRY_NO_FOUND;
}
我回来试了一下,发现你那样会有问题的,因为每次插上一个MODEM编号可能会变化的,我想知道你用MODEM作什么,既然MODEM都是一样的,那么每个MODEM的作用也应该是一样的,这样你直接搜索出有几个COM上有MODEM,然后自己维护一个匹配表,比如:COM1->MODEM1这样的,当选择MODEM1就打开COM1不就可以了