source: trunk/src/activeqt/control/qaxservermain.cpp@ 81

Last change on this file since 81 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the ActiveQt framework 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 <qstringlist.h>
41#include <qvector.h>
42
43#include "qaxfactory.h"
44
45#ifndef QT_NO_WIN_ACTIVEQT
46
47#include <qt_windows.h>
48
49QT_BEGIN_NAMESPACE
50
51static DWORD *classRegistration = 0;
52static DWORD dwThreadID;
53static bool qAxActivity = false;
54static HANDLE hEventShutdown;
55
56#ifdef QT_DEBUG
57QT_STATIC_CONST DWORD dwTimeOut = 1000;
58QT_STATIC_CONST DWORD dwPause = 500;
59#else
60QT_STATIC_CONST DWORD dwTimeOut = 5000; // time for EXE to be idle before shutting down
61QT_STATIC_CONST DWORD dwPause = 1000; // time to wait for threads to finish up
62#endif
63
64extern HANDLE hEventShutdown;
65extern bool qAxActivity;
66extern HANDLE qAxInstance;
67extern bool qAxIsServer;
68extern bool qAxOutProcServer;
69extern char qAxModuleFilename[MAX_PATH];
70extern QString qAxInit();
71extern void qAxCleanup();
72extern HRESULT UpdateRegistry(BOOL bRegister);
73extern HRESULT GetClassObject(const GUID &clsid, const GUID &iid, void **ppUnk);
74extern ulong qAxLockCount();
75extern bool qax_winEventFilter(void *message);
76
77#if defined(Q_CC_BOR)
78extern "C" __stdcall HRESULT DumpIDL(const QString &outfile, const QString &ver);
79#else
80STDAPI DumpIDL(const QString &outfile, const QString &ver);
81#endif
82
83// Monitors the shutdown event
84static DWORD WINAPI MonitorProc(void* pv)
85{
86 while (1) {
87 WaitForSingleObject(hEventShutdown, INFINITE);
88 DWORD dwWait=0;
89 do {
90 qAxActivity = false;
91 dwWait = WaitForSingleObject(hEventShutdown, dwTimeOut);
92 } while (dwWait == WAIT_OBJECT_0);
93 // timed out
94 if (!qAxActivity && !qAxLockCount()) // if no activity let's really bail
95 break;
96 }
97 CloseHandle(hEventShutdown);
98 PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
99 PostQuitMessage(0);
100
101 return 0;
102}
103
104// Starts the monitoring thread
105static bool StartMonitor()
106{
107 dwThreadID = GetCurrentThreadId();
108 hEventShutdown = CreateEventA(0, false, false, 0);
109 if (hEventShutdown == 0)
110 return false;
111 DWORD dwThreadID;
112 HANDLE h = CreateThread(0, 0, MonitorProc, 0, 0, &dwThreadID);
113 return (h != NULL);
114}
115
116void qax_shutDown()
117{
118 qAxActivity = true;
119 if (hEventShutdown)
120 SetEvent(hEventShutdown); // tell monitor that we transitioned to zero
121}
122
123/*
124 Start the COM server (if necessary).
125*/
126bool qax_startServer(QAxFactory::ServerType type)
127{
128 if (qAxIsServer)
129 return true;
130
131 const QStringList keys = qAxFactory()->featureList();
132 if (!keys.count())
133 return false;
134
135 if (!qAxFactory()->isService())
136 StartMonitor();
137
138 classRegistration = new DWORD[keys.count()];
139 int object = 0;
140 for (QStringList::ConstIterator key = keys.begin(); key != keys.end(); ++key, ++object) {
141 IUnknown* p = 0;
142 CLSID clsid = qAxFactory()->classID(*key);
143
144 // Create a QClassFactory (implemented in qaxserverbase.cpp)
145 HRESULT hRes = GetClassObject(clsid, IID_IClassFactory, (void**)&p);
146 if (SUCCEEDED(hRes))
147 hRes = CoRegisterClassObject(clsid, p, CLSCTX_LOCAL_SERVER,
148 type == QAxFactory::MultipleInstances ? REGCLS_MULTIPLEUSE : REGCLS_SINGLEUSE,
149 classRegistration+object);
150 if (p)
151 p->Release();
152 }
153
154 qAxIsServer = true;
155 return true;
156}
157
158/*
159 Stop the COM server (if necessary).
160*/
161bool qax_stopServer()
162{
163 if (!qAxIsServer || !classRegistration)
164 return true;
165
166 qAxIsServer = false;
167
168 const QStringList keys = qAxFactory()->featureList();
169 int object = 0;
170 for (QStringList::ConstIterator key = keys.begin(); key != keys.end(); ++key, ++object)
171 CoRevokeClassObject(classRegistration[object]);
172
173 delete []classRegistration;
174 classRegistration = 0;
175
176 Sleep(dwPause); //wait for any threads to finish
177
178 return true;
179}
180
181#if defined(Q_OS_WINCE)
182extern void __cdecl qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
183#else
184extern void qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
185#endif
186
187QT_END_NAMESPACE
188
189#if defined(QT_NEEDS_QMAIN)
190int qMain(int, char **);
191#define main qMain
192#else
193#if defined(Q_OS_WINCE)
194extern "C" int __cdecl main(int, char **);
195#else
196extern "C" int main(int, char **);
197#endif
198#endif
199
200
201EXTERN_C int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR, int nShowCmd)
202{
203 QT_USE_NAMESPACE
204
205 qAxOutProcServer = true;
206 GetModuleFileNameA(0, qAxModuleFilename, MAX_PATH-1);
207 qAxInstance = hInstance;
208
209 QByteArray cmdParam;
210 QT_WA({
211 LPTSTR cmdline = GetCommandLineW();
212 cmdParam = QString::fromUtf16((const ushort *)cmdline).toLocal8Bit();
213 }, {
214 cmdParam = GetCommandLineA();
215 });
216
217 QList<QByteArray> cmds = cmdParam.split(' ');
218 QByteArray unprocessed;
219
220 int nRet = 0;
221 bool run = true;
222 bool runServer = false;
223 for (int i = 0; i < cmds.count(); ++i) {
224 QByteArray cmd = cmds.at(i).toLower();
225 if (cmd == "-activex" || cmd == "/activex" || cmd == "-embedding" || cmd == "/embedding") {
226 runServer = true;
227 } else if (cmd == "-unregserver" || cmd == "/unregserver") {
228 nRet = UpdateRegistry(false);
229 run = false;
230 break;
231 } else if (cmd == "-regserver" || cmd == "/regserver") {
232 nRet = UpdateRegistry(true);
233 run = false;
234 break;
235 } else if (cmd == "-dumpidl" || cmd == "/dumpidl") {
236 ++i;
237 if (i < cmds.count()) {
238 QByteArray outfile = cmds.at(i);
239 ++i;
240 QByteArray version;
241 if (i < cmds.count() && (cmds.at(i) == "-version" || cmds.at(i) == "/version")) {
242 ++i;
243 if (i < cmds.count())
244 version = cmds.at(i);
245 else
246 version = "1.0";
247 }
248
249 nRet = DumpIDL(QString::fromLatin1(outfile.constData()), QString::fromLatin1(version.constData()));
250 } else {
251 qWarning("Wrong commandline syntax: <app> -dumpidl <idl file> [-version <x.y.z>]");
252 }
253 run = false;
254 break;
255 } else {
256 unprocessed += cmds.at(i) + " ";
257 }
258 }
259
260 if (run) {
261 HRESULT hRes = CoInitialize(0);
262
263 int argc;
264 QVector<char*> argv(8);
265 qWinMain(hInstance, hPrevInstance, unprocessed.data(), nShowCmd, argc, argv);
266 qAxInit();
267 if (runServer)
268 QAxFactory::startServer();
269 nRet = ::main(argc, argv.data());
270 QAxFactory::stopServer();
271 qAxCleanup();
272 CoUninitialize();
273
274 }
275
276 return nRet;
277}
278
279#endif // QT_NO_WIN_ACTIVEQT
Note: See TracBrowser for help on using the repository browser.