void COptotuneControlDlg::GetComportList(CStringList* pszlistComportList)
{
HKEY hkRegistryPath = NULL; // A pointer which stored whole path of registry
pszlistComportList->RemoveAll(); // Initial the list = NULL
// Open the folder which recorded comport device table in registry
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), NULL, KEY_READ, &hkRegistryPath) == ERROR_SUCCESS)
{
int nEnumIndex = 0; // Enum device from index 0
DWORD dwDeviceNameLength = 256; // The buffer length to store device name ex: "\Device\VCP0"
LPSTR lpDeviceName = new CHAR[dwDeviceNameLength]; // The char[] buffer to store device name
DWORD dwComportNameLength = sizeof(_T("COM256")); // The buffer length to store device data ex: "COM5"
LPBYTE lpComportName = new BYTE[dwComportNameLength]; // The BYTE[] buffer to store device name
DWORD dwType = REG_SZ; // Regidtry data type
// Enum enabled comport device
long lErrorCode = RegEnumValue(hkRegistryPath, nEnumIndex, lpDeviceName, &dwDeviceNameLength, NULL, &dwType, lpComportName, &dwComportNameLength);
// If it find device, add data to string list
while(lErrorCode != ERROR_NO_MORE_ITEMS)
{
pszlistComportList->AddTail((LPCTSTR)lpComportName); // Add data to string list
nEnumIndex = nEnumIndex + 1; // Increase index
dwDeviceNameLength = 256; // Reset limitation of device name length
dwComportNameLength = sizeof(_T("COM256")); // Reset limitation of device data length
// Enum next device
lErrorCode = RegEnumValue(hkRegistryPath, nEnumIndex, lpDeviceName, &dwDeviceNameLength, NULL, &dwType, lpComportName, &dwComportNameLength);
}
// Release memory and pointer
delete[] lpDeviceName;
lpDeviceName = NULL;
delete[] lpComportName;
lpComportName = NULL;
// Close registry folder
RegCloseKey(hkRegistryPath);
}
}
each for serial port
最新推荐文章于 2021-05-28 03:07:10 发布