1 // ==========================================================================
3 // ==========================================================================
5 // This source file is included from Qt v4.8.3, because VS2012 won't link
6 // with the 'qtmain.lib' from the official Qt v4.8.3 (VS2010) release.
8 // ==========================================================================
10 /****************************************************************************
12 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
13 ** Contact: http://www.qt-project.org/
15 ** This file is part of the Windows main function of the Qt Toolkit.
17 ** $QT_BEGIN_LICENSE:BSD$
18 ** You may use this file under the terms of the BSD license as follows:
20 ** "Redistribution and use in source and binary forms, with or without
21 ** modification, are permitted provided that the following conditions are
23 ** * Redistributions of source code must retain the above copyright
24 ** notice, this list of conditions and the following disclaimer.
25 ** * Redistributions in binary form must reproduce the above copyright
26 ** notice, this list of conditions and the following disclaimer in
27 ** the documentation and/or other materials provided with the
29 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
30 ** the names of its contributors may be used to endorse or promote
31 ** products derived from this software without specific prior written
34 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
48 ****************************************************************************/
50 #include "qt_windows.h"
57 This file contains the code in the qtmain library for Windows.
58 qtmain contains the Windows startup code and is required for
59 linking to the Qt DLL.
61 When a Windows application starts, the WinMain function is
62 invoked. WinMain calls qWinMain in the Qt DLL/library, which
68 #if defined(Q_OS_WINCE)
69 extern void __cdecl qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
71 extern void qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
79 #if defined(QT_NEEDS_QMAIN)
80 int qMain(int, char **);
84 extern "C" int __cdecl main(int, char **);
86 extern "C" int main(int, char **);
91 WinMain() - Initializes Windows and calls user's startup function main().
92 NOTE: WinMain() won't be called if the application was linked as a "console"
97 int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR /*wCmdParam*/, int cmdShow)
100 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR /*cmdParamarg*/, int cmdShow)
103 QByteArray cmdParam = QString::fromWCharArray(GetCommandLine()).toLocal8Bit();
105 #if defined(Q_OS_WINCE)
106 wchar_t appName[MAX_PATH];
107 GetModuleFileName(0, appName, MAX_PATH);
108 cmdParam.prepend(QString(QLatin1String("\"%1\" ")).arg(QString::fromWCharArray(appName)).toLocal8Bit());
112 QVector<char *> argv(8);
113 qWinMain(instance, prevInstance, cmdParam.data(), cmdShow, argc, argv);
115 #if defined(Q_OS_WINCE)
116 wchar_t uniqueAppID[MAX_PATH];
117 GetModuleFileName(0, uniqueAppID, MAX_PATH);
118 QString uid = QString::fromWCharArray(uniqueAppID).toLower().replace(QLatin1String("\\"), QLatin1String("_"));
120 // If there exists an other instance of this application
121 // it will be the owner of a mutex with the unique ID.
122 HANDLE mutex = CreateMutex(NULL, TRUE, (LPCWSTR)uid.utf16());
123 if (mutex && ERROR_ALREADY_EXISTS == GetLastError()) {
126 // The app is already running, so we use the unique
127 // ID to create a unique messageNo, which is used
128 // as the registered class name for the windows
129 // created. Set the first instance's window to the
130 // foreground, else just terminate.
131 // Use bitwise 0x01 OR to reactivate window state if
133 UINT msgNo = RegisterWindowMessage((LPCWSTR)uid.utf16());
134 HWND aHwnd = FindWindow((LPCWSTR)QString::number(msgNo).utf16(), 0);
136 SetForegroundWindow((HWND)(((ULONG)aHwnd) | 0x01));
141 int result = main(argc, argv.data());
142 #if defined(Q_OS_WINCE)