| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the tools applications of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "serenum.h" | 
|---|
| 43 | #include <QByteArray> | 
|---|
| 44 | #include <QString> | 
|---|
| 45 | #include <QDebug> | 
|---|
| 46 | #include <windows.h> | 
|---|
| 47 | #include <windef.h> | 
|---|
| 48 | #include <setupapi.h> | 
|---|
| 49 | #include <devguid.h> | 
|---|
| 50 | #include <winreg.h> | 
|---|
| 51 | #include <shlwapi.h> | 
|---|
| 52 |  | 
|---|
| 53 | //{4d36e978-e325-11ce-bfc1-08002be10318} | 
|---|
| 54 | //DEFINE_GUID(GUID_DEVCLASS_PORTS, 0x4D36E978, 0xE325, 0x11CE, 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 ); | 
|---|
| 55 |  | 
|---|
| 56 | QList<SerialPortId> enumerateSerialPorts() | 
|---|
| 57 | { | 
|---|
| 58 | DWORD index=0; | 
|---|
| 59 | SP_DEVINFO_DATA info; | 
|---|
| 60 | GUID guid = GUID_DEVCLASS_PORTS; | 
|---|
| 61 | HDEVINFO infoset = SetupDiGetClassDevs(&guid, 0, 0, DIGCF_PRESENT); | 
|---|
| 62 | QString valueName(16384, 0); | 
|---|
| 63 | QList<SerialPortId> list; | 
|---|
| 64 |  | 
|---|
| 65 | for (index=0;;index++) { | 
|---|
| 66 | ZeroMemory(&info, sizeof(SP_DEVINFO_DATA)); | 
|---|
| 67 | info.cbSize = sizeof(SP_DEVINFO_DATA); | 
|---|
| 68 | if (!SetupDiEnumDeviceInfo(infoset, index, &info)) | 
|---|
| 69 | break; | 
|---|
| 70 | QString friendlyName; | 
|---|
| 71 | QString portName; | 
|---|
| 72 | DWORD size=0; | 
|---|
| 73 | SetupDiGetDeviceRegistryProperty(infoset, &info, SPDRP_FRIENDLYNAME, 0, 0, 0, &size); | 
|---|
| 74 | QByteArray ba(size, 0); | 
|---|
| 75 | if(SetupDiGetDeviceRegistryProperty(infoset, &info, SPDRP_FRIENDLYNAME, 0, (BYTE*)(ba.data()), size, 0)) { | 
|---|
| 76 | friendlyName = QString((const QChar*)(ba.constData()), ba.size() / 2 - 1); | 
|---|
| 77 | } | 
|---|
| 78 | HKEY key = SetupDiOpenDevRegKey(infoset, &info, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); | 
|---|
| 79 | if(key != INVALID_HANDLE_VALUE) { | 
|---|
| 80 | //RegGetValue not supported on XP, SHRegGetValue not supported by mingw, so use the old method of enumerating all the values | 
|---|
| 81 | for (DWORD dwi=0;;dwi++) { | 
|---|
| 82 | DWORD vsize = valueName.size(); | 
|---|
| 83 | if (ERROR_SUCCESS == RegEnumValue(key, dwi, (WCHAR*)(valueName.data()), &vsize, 0, 0, 0, &size)) { | 
|---|
| 84 | if (valueName.startsWith("PortName")) { | 
|---|
| 85 | QByteArray ba(size, 0); | 
|---|
| 86 | vsize = valueName.size(); | 
|---|
| 87 | if(ERROR_SUCCESS == RegEnumValue(key, dwi, (WCHAR*)(valueName.data()), &vsize, 0, 0, (BYTE*)(ba.data()), &size)) { | 
|---|
| 88 | portName = QString((const QChar*)(ba.constData()), ba.size() / 2 - 1); | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 | } else { | 
|---|
| 92 | break; | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 | RegCloseKey(key); | 
|---|
| 96 | } | 
|---|
| 97 | SerialPortId id; | 
|---|
| 98 | id.portName = portName; | 
|---|
| 99 | id.friendlyName = friendlyName; | 
|---|
| 100 | list.append(id); | 
|---|
| 101 | } | 
|---|
| 102 | SetupDiDestroyDeviceInfoList(infoset); | 
|---|
| 103 | return list; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|