OSDN Git Service

Updated VS2012 project file.
[lamexp/LameXP.git] / src / WinMain.cpp
1 // ==========================================================================
2 // qtmain_win.cpp
3 // ==========================================================================
4 //
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.
7 //
8 // ==========================================================================
9
10 /****************************************************************************
11 **
12 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
13 ** Contact: http://www.qt-project.org/
14 **
15 ** This file is part of the Windows main function of the Qt Toolkit.
16 **
17 ** $QT_BEGIN_LICENSE:BSD$
18 ** You may use this file under the terms of the BSD license as follows:
19 **
20 ** "Redistribution and use in source and binary forms, with or without
21 ** modification, are permitted provided that the following conditions are
22 ** met:
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
28 **     distribution.
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
32 **     permission.
33 **
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."
45 **
46 ** $QT_END_LICENSE$
47 **
48 ****************************************************************************/
49
50 #include "qt_windows.h"
51
52 #include <QByteArray>
53 #include <QString>
54 #include <QVector>
55
56 /*
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.
60
61   When a Windows application starts, the WinMain function is
62   invoked. WinMain calls qWinMain in the Qt DLL/library, which
63   initializes Qt.
64 */
65
66 QT_BEGIN_NAMESPACE
67
68 #if defined(Q_OS_WINCE)
69 extern void __cdecl qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
70 #else
71 extern void qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
72 #endif
73
74 QT_END_NAMESPACE
75
76 QT_USE_NAMESPACE
77
78
79 #if defined(QT_NEEDS_QMAIN)
80 int qMain(int, char **);
81 #define main qMain
82 #else
83 #ifdef Q_OS_WINCE
84 extern "C" int __cdecl main(int, char **);
85 #else
86 extern "C" int main(int, char **);
87 #endif
88 #endif
89
90 /*
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"
93   application.
94 */
95
96 #ifdef Q_OS_WINCE
97 int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR /*wCmdParam*/, int cmdShow)
98 #else
99 extern "C"
100 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR /*cmdParamarg*/, int cmdShow)
101 #endif
102 {
103     QByteArray cmdParam = QString::fromWCharArray(GetCommandLine()).toLocal8Bit();
104
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());
109 #endif
110
111     int argc = 0;
112     QVector<char *> argv(8);
113     qWinMain(instance, prevInstance, cmdParam.data(), cmdShow, argc, argv);
114
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("_"));
119
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()) {
124         CloseHandle(mutex);
125
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
132         // it was hidden
133         UINT msgNo = RegisterWindowMessage((LPCWSTR)uid.utf16());
134         HWND aHwnd = FindWindow((LPCWSTR)QString::number(msgNo).utf16(), 0);
135         if (aHwnd)
136             SetForegroundWindow((HWND)(((ULONG)aHwnd) | 0x01));
137         return 0;
138     }
139 #endif // Q_OS_WINCE
140
141     int result = main(argc, argv.data());
142 #if defined(Q_OS_WINCE)
143     CloseHandle(mutex);
144 #endif
145     return result;
146 }