tools/runonphone/serenum_win.cpp
branchRCL_3
changeset 4 3b1da2848fc7
equal deleted inserted replaced
3:41300fa6a67c 4:3b1da2848fc7
       
     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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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