Qt 4.8
qaxserverdll.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 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 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 #ifndef QT_NO_WIN_ACTIVEQT
42 
43 #include <qapplication.h>
44 #include <qwidget.h>
45 
46 #include <qt_windows.h>
47 
49 
50 bool qax_ownQApp = false;
51 HHOOK qax_hhook = 0;
52 
53 // in qaxserver.cpp
54 extern wchar_t qAxModuleFilename[MAX_PATH];
55 extern bool qAxIsServer;
56 extern ITypeLib *qAxTypeLibrary;
57 extern unsigned long qAxLockCount();
58 extern QString qAxInit();
59 extern void qAxCleanup();
60 extern HANDLE qAxInstance;
61 static uint qAxThreadId = 0;
62 
63 extern HRESULT UpdateRegistry(int bRegister);
64 extern HRESULT GetClassObject(const GUID &clsid, const GUID &iid, void **ppUnk);
65 
67 {
68  return UpdateRegistry(true);
69 }
70 
72 {
73  return UpdateRegistry(false);
74 }
75 
76 STDAPI DllGetClassObject(const GUID &clsid, const GUID &iid, void** ppv)
77 {
78  if (!qAxThreadId)
79  qAxThreadId = GetCurrentThreadId();
80  else if (GetCurrentThreadId() != qAxThreadId)
81  return E_FAIL;
82 
83  GetClassObject(clsid, iid, ppv);
84  if (!*ppv)
85  return CLASS_E_CLASSNOTAVAILABLE;
86  return S_OK;
87 }
88 
90 {
91  if (GetCurrentThreadId() != qAxThreadId)
92  return S_FALSE;
93  if (qAxLockCount())
94  return S_FALSE;
95  if (!qax_ownQApp)
96  return S_OK;
97 
98  // check if qApp still runs widgets (in other DLLs)
99  QWidgetList widgets = qApp->allWidgets();
100  int count = widgets.count();
101  for (int w = 0; w < widgets.count(); ++w) {
102  // remove all Qt generated widgets
103  QWidget *widget = widgets.at(w);
104  if (widget->windowType() == Qt::Desktop || widget->objectName() == QLatin1String("Qt internal tablet widget"))
105  count--;
106  }
107  if (count)
108  return S_FALSE;
109 
110  // no widgets left - destroy qApp
111  if (qax_hhook)
112  UnhookWindowsHookEx(qax_hhook);
113 
114  delete qApp;
115  qax_ownQApp = false;
116 
117  // never allow unloading - safety net for Internet Explorer
118  return S_FALSE;
119 }
120 
121 
122 EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpvReserved)
123 {
124  GetModuleFileName(hInstance, qAxModuleFilename, MAX_PATH);
125  qAxInstance = hInstance;
126  qAxIsServer = true;
127 
128  if (dwReason == DLL_PROCESS_ATTACH) {
129  DisableThreadLibraryCalls(hInstance);
130  qAxInit();
131  } else if (dwReason == DLL_PROCESS_DETACH) {
132  qAxCleanup();
133  }
134 
135  return true;
136 }
137 
139 #endif // QT_NO_WIN_ACTIVEQT
STDAPI DllCanUnloadNow()
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
Definition: quuid.h:52
wchar_t qAxModuleFilename[MAX_PATH]
Definition: qaxserver.cpp:67
ITypeLib * qAxTypeLibrary
Definition: qaxserver.cpp:66
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
static uint qAxThreadId
HRESULT UpdateRegistry(int bRegister)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
HRESULT GetClassObject(const GUID &clsid, const GUID &iid, void **ppUnk)
#define qApp
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
STDAPI DllUnregisterServer()
unsigned int uint
Definition: qglobal.h:996
void * HANDLE
Definition: qnamespace.h:1671
EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpvReserved)
HHOOK qax_hhook
long HRESULT
QString objectName() const
QString qAxInit()
Definition: qaxserver.cpp:111
unsigned long qAxLockCount()
Definition: qaxserver.cpp:180
STDAPI DllGetClassObject(const GUID &clsid, const GUID &iid, void **ppv)
bool qAxIsServer
Definition: qaxserver.cpp:64
Qt::WindowType windowType() const
Returns the window type of this widget.
Definition: qwidget.h:937
STDAPI DllRegisterServer()
void qAxCleanup()
Definition: qaxserver.cpp:139
bool qax_ownQApp
HANDLE qAxInstance
Definition: qaxserver.cpp:65
The QList class is a template class that provides lists.
Definition: qdatastream.h:62