| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the Windows main function of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:BSD$ | 
|---|
| 9 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 10 | ** | 
|---|
| 11 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 12 | ** modification, are permitted provided that the following conditions are | 
|---|
| 13 | ** met: | 
|---|
| 14 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 15 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 16 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 17 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 18 | **     the documentation and/or other materials provided with the | 
|---|
| 19 | **     distribution. | 
|---|
| 20 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 21 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 22 | **     products derived from this software without specific prior written | 
|---|
| 23 | **     permission. | 
|---|
| 24 | ** | 
|---|
| 25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 36 | ** $QT_END_LICENSE$ | 
|---|
| 37 | ** | 
|---|
| 38 | ****************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | #include "qt_windows.h" | 
|---|
| 41 | #include "qbytearray.h" | 
|---|
| 42 | #include "qstring.h" | 
|---|
| 43 | #include "qvector.h" | 
|---|
| 44 |  | 
|---|
| 45 | /* | 
|---|
| 46 | This file contains the code in the qtmain library for Windows. | 
|---|
| 47 | qtmain contains the Windows startup code and is required for | 
|---|
| 48 | linking to the Qt DLL. | 
|---|
| 49 |  | 
|---|
| 50 | When a Windows application starts, the WinMain function is | 
|---|
| 51 | invoked. WinMain calls qWinMain in the Qt DLL/library, which | 
|---|
| 52 | initializes Qt. | 
|---|
| 53 | */ | 
|---|
| 54 |  | 
|---|
| 55 | QT_BEGIN_NAMESPACE | 
|---|
| 56 |  | 
|---|
| 57 | #if defined(Q_OS_WINCE) | 
|---|
| 58 | extern void __cdecl qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &); | 
|---|
| 59 | #else | 
|---|
| 60 | extern void qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &); | 
|---|
| 61 | #endif | 
|---|
| 62 |  | 
|---|
| 63 | QT_END_NAMESPACE | 
|---|
| 64 |  | 
|---|
| 65 | QT_USE_NAMESPACE | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | #if defined(QT_NEEDS_QMAIN) | 
|---|
| 69 | int qMain(int, char **); | 
|---|
| 70 | #define main qMain | 
|---|
| 71 | #else | 
|---|
| 72 | #ifdef Q_OS_WINCE | 
|---|
| 73 | extern "C" int __cdecl main(int, char **); | 
|---|
| 74 | #else | 
|---|
| 75 | extern "C" int main(int, char **); | 
|---|
| 76 | #endif | 
|---|
| 77 | #endif | 
|---|
| 78 |  | 
|---|
| 79 | /* | 
|---|
| 80 | WinMain() - Initializes Windows and calls user's startup function main(). | 
|---|
| 81 | NOTE: WinMain() won't be called if the application was linked as a "console" | 
|---|
| 82 | application. | 
|---|
| 83 | */ | 
|---|
| 84 |  | 
|---|
| 85 | #ifdef Q_OS_WINCE | 
|---|
| 86 | int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR /*wCmdParam*/, int cmdShow) | 
|---|
| 87 | #else | 
|---|
| 88 | extern "C" | 
|---|
| 89 | int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR /*cmdParamarg*/, int cmdShow) | 
|---|
| 90 | #endif | 
|---|
| 91 | { | 
|---|
| 92 | QByteArray cmdParam; | 
|---|
| 93 | QT_WA({ | 
|---|
| 94 | LPTSTR cmdline = GetCommandLineW(); | 
|---|
| 95 | cmdParam = QString::fromUtf16((const unsigned short *)cmdline).toLocal8Bit(); | 
|---|
| 96 | }, { | 
|---|
| 97 | cmdParam = GetCommandLineA(); | 
|---|
| 98 | }); | 
|---|
| 99 |  | 
|---|
| 100 | #if defined(Q_OS_WINCE) | 
|---|
| 101 | TCHAR appName[256]; | 
|---|
| 102 | GetModuleFileName(0, appName, 255); | 
|---|
| 103 | cmdParam = QString(QLatin1String("\"%1\" ")).arg(QString::fromUtf16((const unsigned short *)appName)).toLocal8Bit() + cmdParam; | 
|---|
| 104 | #endif | 
|---|
| 105 |  | 
|---|
| 106 | int argc = 0; | 
|---|
| 107 | QVector<char *> argv(8); | 
|---|
| 108 | qWinMain(instance, prevInstance, cmdParam.data(), cmdShow, argc, argv); | 
|---|
| 109 |  | 
|---|
| 110 | #if defined(Q_OS_WINCE) | 
|---|
| 111 | TCHAR uniqueAppID[256]; | 
|---|
| 112 | GetModuleFileName(0, uniqueAppID, 255); | 
|---|
| 113 | QString uid = QString::fromUtf16((const unsigned short *)uniqueAppID).toLower().replace(QString(QLatin1String("\\")), QString(QLatin1String("_"))); | 
|---|
| 114 |  | 
|---|
| 115 | // If there exists an other instance of this application | 
|---|
| 116 | // it will be the owner of a mutex with the unique ID. | 
|---|
| 117 | HANDLE mutex = CreateMutex(NULL, TRUE, (LPCWSTR)uid.utf16()); | 
|---|
| 118 | if (mutex && ERROR_ALREADY_EXISTS == GetLastError()) { | 
|---|
| 119 | CloseHandle(mutex); | 
|---|
| 120 |  | 
|---|
| 121 | // The app is already running, so we use the unique | 
|---|
| 122 | // ID to create a unique messageNo, which is used | 
|---|
| 123 | // as the registered class name for the windows | 
|---|
| 124 | // created. Set the first instance's window to the | 
|---|
| 125 | // foreground, else just terminate. | 
|---|
| 126 | // Use bitwise 0x01 OR to reactivate window state if | 
|---|
| 127 | // it was hidden | 
|---|
| 128 | UINT msgNo = RegisterWindowMessage((LPCWSTR)uid.utf16()); | 
|---|
| 129 | HWND aHwnd = FindWindow((LPCWSTR)QString::number(msgNo).utf16(), 0); | 
|---|
| 130 | if (aHwnd) | 
|---|
| 131 | SetForegroundWindow((HWND)(((ULONG)aHwnd) | 0x01)); | 
|---|
| 132 | return 0; | 
|---|
| 133 | } | 
|---|
| 134 | #endif // Q_OS_WINCE | 
|---|
| 135 |  | 
|---|
| 136 | int result = main(argc, argv.data()); | 
|---|
| 137 | #if defined(Q_OS_WINCE) | 
|---|
| 138 | CloseHandle(mutex); | 
|---|
| 139 | #endif | 
|---|
| 140 | return result; | 
|---|
| 141 | } | 
|---|