I need to enumeratre serial port. I've found some CPP code, but, I'm not familiar with C and can't translate it to use with harbour.
I have whole archive file enumser.zip created by:
whih contains all definitions and working .exe file.
I've cutted some code to present the problem:
Code: Select all
#ifndef NO_ENUMSERIAL_USING_REGISTRY
#if defined CENUMERATESERIAL_USE_STL
#if defined _UNICODE
BOOL CEnumerateSerial::UsingRegistry(std::vector<std::wstring>& ports)
#else
BOOL CEnumerateSerial::UsingRegistry(std::vector<std::string>& ports)
#endif
#elif defined _AFX
BOOL CEnumerateSerial::UsingRegistry(CStringArray& ports)
#else
BOOL CEnumerateSerial::UsingRegistry(CSimpleArray<CString>& ports)
#endif
{
//Make sure we clear out any elements which may already be in the array(s)
#if defined CENUMERATESERIAL_USE_STL
ports.clear();
#else
ports.RemoveAll();
#endif
//What will be the return value from this function (assume the worst)
BOOL bSuccess = FALSE;
HKEY hSERIALCOMM;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hSERIALCOMM) == ERROR_SUCCESS)
{
//Get the max value name and max value lengths
DWORD dwMaxValueNameLen;
DWORD dwMaxValueLen;
DWORD dwQueryInfo = RegQueryInfoKey(hSERIALCOMM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwMaxValueNameLen, &dwMaxValueLen, NULL, NULL);
if (dwQueryInfo == ERROR_SUCCESS)
{
DWORD dwMaxValueNameSizeInChars = dwMaxValueNameLen + 1; //Include space for the NULL terminator
DWORD dwMaxValueNameSizeInBytes = dwMaxValueNameSizeInChars * sizeof(TCHAR);
DWORD dwMaxValueDataSizeInChars = dwMaxValueLen/sizeof(TCHAR) + 1; //Include space for the NULL terminator
DWORD dwMaxValueDataSizeInBytes = dwMaxValueDataSizeInChars * sizeof(TCHAR);
//Allocate some space for the value name and value data
ATL::CHeapPtr<TCHAR> szValueName;
ATL::CHeapPtr<BYTE> byValue;
if (szValueName.Allocate(dwMaxValueNameSizeInChars) && byValue.Allocate(dwMaxValueDataSizeInBytes))
{
bSuccess = TRUE;
//Enumerate all the values underneath HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
DWORD dwIndex = 0;
DWORD dwType;
DWORD dwValueNameSize = dwMaxValueNameSizeInChars;
DWORD dwDataSize = dwMaxValueDataSizeInBytes;
memset(szValueName.m_pData, 0, dwMaxValueNameSizeInBytes);
memset(byValue.m_pData, 0, dwMaxValueDataSizeInBytes);
LONG nEnum = RegEnumValue(hSERIALCOMM, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, byValue, &dwDataSize);
while (nEnum == ERROR_SUCCESS)
{
//If the value is of the correct type, then add it to the array
if (dwType == REG_SZ)
{
TCHAR* szPort = reinterpret_cast<TCHAR*>(byValue.m_pData);
#if defined CENUMERATESERIAL_USE_STL
ports.push_back(szPort);
#else
ports.Add(szPort);
#endif
}
//Prepare for the next time around
dwValueNameSize = dwMaxValueNameSizeInChars;
dwDataSize = dwMaxValueDataSizeInBytes;
memset(szValueName.m_pData, 0, dwMaxValueNameSizeInBytes);
memset(byValue.m_pData, 0, dwMaxValueDataSizeInBytes);
++dwIndex;
nEnum = RegEnumValue(hSERIALCOMM, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, byValue, &dwDataSize);
}
}
else
SetLastError(ERROR_OUTOFMEMORY);
}
//Close the registry key now that we are finished with it
RegCloseKey(hSERIALCOMM);
if (dwQueryInfo != ERROR_SUCCESS)
SetLastError(dwQueryInfo);
}
return bSuccess;
}
#endif
Regards, Marek