所以只好和winapi交互利用。
要注意的是QSettings得到的鍵值是斜線,可是winapi是反斜線
const QStringList WinDongle::getComPort()
{
QStringList comports;
QString keyPath = "HARDWARE\\DEVICEMAP\\SERIALCOMM";
HKEY comsKey;
LPCWSTR winKeyPath = (LPCWSTR)keyPath.constData();
if(RegOpenKey(HKEY_LOCAL_MACHINE, winKeyPath, &comsKey) != ERROR_SUCCESS)
{
error = UNKNOW;
RegCloseKey(comsKey);
return comports;
}
QSettings settings(QString("HKEY_LOCAL_MACHINE\\")+keyPath, QSettings::NativeFormat);
QStringList keys = settings.allKeys();
foreach( const QString & key, keys )
{
QString newKey(key);
newKey.replace(QString("/"), QString("\\"));
LPCWSTR winKey = (LPCWSTR)newKey.constData();
char *szData= new char[101];
DWORD dwType, dwLen=100;
if (RegQueryValueEx(comsKey, winKey,
NULL, &dwType, (unsigned char *)szData, &dwLen) == ERROR_SUCCESS)
{
comports.append( QString::fromUtf16((ushort*)szData) );
}
delete[] szData;
}
RegCloseKey(comsKey);
return comports;
}
參考資料:在 Win32 下 Serial Port 的通訊
沒有留言:
張貼留言