void ClassName::getSystemComPortList(CStringArray &comPortList)
{
comPortList.RemoveAll();
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
0,
KEY_READ,
&hKey) == ERROR_SUCCESS
)
{
TCHAR achKey[MAX_PATH]; // buffer for subkey name
DWORD cbName; // size of name
string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey
size
DWORD cchMaxClass; // longest class
string
DWORD cValues; // number of values
for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of
security descriptor
FILETIME
ftLastWriteTime; // last
write time
DWORD i, retCode;
TCHAR achValue[MAX_PATH];
DWORD cchValue = MAX_PATH;
TCHAR achValue2[MAX_PATH];
DWORD cchValue2 =
MAX_PATH;
// Get the class name
and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class
name
&cchClassName, // size of class
string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey
size
&cchMaxClass, // longest class
string
&cValues, // number of values
for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
// Enumerate the subkeys, until RegEnumKeyEx
fails.
if (cSubKeys)
{
TRACE(_T("Number
of subkeys: %d\n"), cSubKeys);
for (i = 0;
i
{
cbName = MAX_PATH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode ==
ERROR_SUCCESS)
{
_tprintf(TEXT("(%d)
%s\n"), i + 1, achKey);
}
}
}
// Enumerate the key
values.
if (cValues)
{
//TRACE(_T("Number
of values: %d\n"), cValues);
for (i = 0,
retCode = ERROR_SUCCESS; i
{
cchValue = MAX_PATH;
achValue[0] = '\0';
cchValue2 = MAX_PATH;
achValue2[0] = '\0';
retCode = RegEnumValue(hKey, i,
achValue,
&cchValue,
NULL,
NULL,
(UCHAR*)achValue2,
&cchValue2);
if (retCode ==
ERROR_SUCCESS)
{
//TRACE(_T("i:%d,
achValue:%s, achValue2:%s\n"), i, achValue, achValue2);
comPortList.Add(achValue2);
}
}
}
}
RegCloseKey(hKey);
}
沒有留言:
張貼留言