Qt 4.8
qtmain_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
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 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include "qt_windows.h"
42 #include "qbytearray.h"
43 #include "qstring.h"
44 #include "qvector.h"
45 
46 /*
47  This file contains the code in the qtmain library for Windows.
48  qtmain contains the Windows startup code and is required for
49  linking to the Qt DLL.
50 
51  When a Windows application starts, the WinMain function is
52  invoked. WinMain calls qWinMain in the Qt DLL/library, which
53  initializes Qt.
54 */
55 
57 
58 #if defined(Q_OS_WINCE)
59 extern void __cdecl qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
60 #else
61 extern void qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
62 #endif
63 
65 
67 
68 
69 #if defined(QT_NEEDS_QMAIN)
70 int qMain(int, char **);
71 #define main qMain
72 #else
73 #ifdef Q_OS_WINCE
74 extern "C" int __cdecl main(int, char **);
75 #else
76 extern "C" int main(int, char **);
77 #endif
78 #endif
79 
80 /*
81  WinMain() - Initializes Windows and calls user's startup function main().
82  NOTE: WinMain() won't be called if the application was linked as a "console"
83  application.
84 */
85 
86 #ifdef Q_OS_WINCE
87 int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR /*wCmdParam*/, int cmdShow)
88 #else
89 extern "C"
90 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR /*cmdParamarg*/, int cmdShow)
91 #endif
92 {
93  QByteArray cmdParam = QString::fromWCharArray(GetCommandLine()).toLocal8Bit();
94 
95 #if defined(Q_OS_WINCE)
96  wchar_t appName[MAX_PATH];
97  GetModuleFileName(0, appName, MAX_PATH);
98  cmdParam.prepend(QString(QLatin1String("\"%1\" ")).arg(QString::fromWCharArray(appName)).toLocal8Bit());
99 #endif
100 
101  int argc = 0;
102  QVector<char *> argv(8);
103  qWinMain(instance, prevInstance, cmdParam.data(), cmdShow, argc, argv);
104 
105 #if defined(Q_OS_WINCE)
106  wchar_t uniqueAppID[MAX_PATH];
107  GetModuleFileName(0, uniqueAppID, MAX_PATH);
108  QString uid = QString::fromWCharArray(uniqueAppID).toLower().replace(QLatin1String("\\"), QLatin1String("_"));
109 
110  // If there exists an other instance of this application
111  // it will be the owner of a mutex with the unique ID.
112  HANDLE mutex = CreateMutex(NULL, TRUE, (LPCWSTR)uid.utf16());
113  if (mutex && ERROR_ALREADY_EXISTS == GetLastError()) {
114  CloseHandle(mutex);
115 
116  // The app is already running, so we use the unique
117  // ID to create a unique messageNo, which is used
118  // as the registered class name for the windows
119  // created. Set the first instance's window to the
120  // foreground, else just terminate.
121  // Use bitwise 0x01 OR to reactivate window state if
122  // it was hidden
123  UINT msgNo = RegisterWindowMessage((LPCWSTR)uid.utf16());
124  HWND aHwnd = FindWindow((LPCWSTR)QString::number(msgNo).utf16(), 0);
125  if (aHwnd)
126  SetForegroundWindow((HWND)(((ULONG)aHwnd) | 0x01));
127  return 0;
128  }
129 #endif // Q_OS_WINCE
130 
131  int result = main(argc, argv.data());
132 #if defined(Q_OS_WINCE)
133  CloseHandle(mutex);
134 #endif
135  return result;
136 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void __cdecl qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector< char *> &)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QByteArray & prepend(char c)
Prepends the character ch to this byte array.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int __cdecl main(int, char **)
Definition: qaxmain.cpp:46
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR, int cmdShow)
Definition: qtmain_win.cpp:87
void * HANDLE
Definition: qnamespace.h:1671
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
#define TRUE
Synonym for true.
Definition: qglobal.h:1018
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
static QString appName
T * data()
Returns a pointer to the data stored in the vector.
Definition: qvector.h:152
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290
#define APIENTRY
Definition: glfunctions.h:57