获取计算机串口资源列表解决方案
获取计算机串口资源列表
各位有谁知道获取计算机串口名称和数目,而且用VC的代码实现。谢谢!
------解决方案--------------------
请参考以下代码
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);
}
}
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)
{
}
}while (ret==ERROR_SUCCESS);
}
}
RegCloseKey(hKey);
各位有谁知道获取计算机串口名称和数目,而且用VC的代码实现。谢谢!
------解决方案--------------------
请参考以下代码
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);
}
}
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)
{
}
}while (ret==ERROR_SUCCESS);
}
}
RegCloseKey(hKey);