9474b2a9597383890565fe9aea0e12bf5c113867
[reactos.git] / reactos / dll / win32 / msi / msi_main.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2006 Mike McCormack for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "shlwapi.h"
30 #include "oleauto.h"
31 #include "msipriv.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36
37 static LONG dll_count;
38
39 /* the UI level */
40 INSTALLUILEVEL gUILevel = INSTALLUILEVEL_BASIC;
41 HWND gUIhwnd = 0;
42 INSTALLUI_HANDLERA gUIHandlerA = NULL;
43 INSTALLUI_HANDLERW gUIHandlerW = NULL;
44 DWORD gUIFilter = 0;
45 LPVOID gUIContext = NULL;
46 WCHAR gszLogFile[MAX_PATH];
47 HINSTANCE msi_hInstance;
48
49 static WCHAR msi_path[MAX_PATH];
50 static ITypeLib *msi_typelib;
51
52 /*
53 * Dll lifetime tracking declaration
54 */
55 static void LockModule(void)
56 {
57 InterlockedIncrement(&dll_count);
58 }
59
60 static void UnlockModule(void)
61 {
62 InterlockedDecrement(&dll_count);
63 }
64
65 /******************************************************************
66 * DllMain
67 */
68 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
69 {
70 switch (fdwReason)
71 {
72 case DLL_PROCESS_ATTACH:
73 msi_hInstance = hinstDLL;
74 DisableThreadLibraryCalls(hinstDLL);
75 break;
76 case DLL_PROCESS_DETACH:
77 if (msi_typelib) ITypeLib_Release( msi_typelib );
78 msi_dialog_unregister_class();
79 msi_free_handle_table();
80 break;
81 }
82 return TRUE;
83 }
84
85 static CRITICAL_SECTION MSI_typelib_cs;
86 static CRITICAL_SECTION_DEBUG MSI_typelib_cs_debug =
87 {
88 0, 0, &MSI_typelib_cs,
89 { &MSI_typelib_cs_debug.ProcessLocksList,
90 &MSI_typelib_cs_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": MSI_typelib_cs") }
92 };
93 static CRITICAL_SECTION MSI_typelib_cs = { &MSI_typelib_cs_debug, -1, 0, 0, 0, 0 };
94
95 ITypeLib *get_msi_typelib( LPWSTR *path )
96 {
97 EnterCriticalSection( &MSI_typelib_cs );
98
99 if (!msi_typelib)
100 {
101 TRACE("loading typelib\n");
102
103 if (GetModuleFileNameW( msi_hInstance, msi_path, MAX_PATH ))
104 LoadTypeLib( msi_path, &msi_typelib );
105 }
106
107 LeaveCriticalSection( &MSI_typelib_cs );
108
109 if (path)
110 *path = msi_path;
111
112 if (msi_typelib)
113 ITypeLib_AddRef( msi_typelib );
114
115 return msi_typelib;
116 }
117
118 typedef struct tagIClassFactoryImpl {
119 const IClassFactoryVtbl *lpVtbl;
120 HRESULT (*create_object)( IUnknown*, LPVOID* );
121 } IClassFactoryImpl;
122
123 static HRESULT WINAPI MsiCF_QueryInterface(LPCLASSFACTORY iface,
124 REFIID riid,LPVOID *ppobj)
125 {
126 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
127
128 TRACE("%p %s %p\n",This,debugstr_guid(riid),ppobj);
129
130 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
131 IsEqualCLSID( riid, &IID_IClassFactory ) )
132 {
133 IClassFactory_AddRef( iface );
134 *ppobj = iface;
135 return S_OK;
136 }
137 return E_NOINTERFACE;
138 }
139
140 static ULONG WINAPI MsiCF_AddRef(LPCLASSFACTORY iface)
141 {
142 LockModule();
143 return 2;
144 }
145
146 static ULONG WINAPI MsiCF_Release(LPCLASSFACTORY iface)
147 {
148 UnlockModule();
149 return 1;
150 }
151
152 static HRESULT WINAPI MsiCF_CreateInstance(LPCLASSFACTORY iface,
153 LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj)
154 {
155 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
156 IUnknown *unk = NULL;
157 HRESULT r;
158
159 TRACE("%p %p %s %p\n", This, pOuter, debugstr_guid(riid), ppobj);
160
161 r = This->create_object( pOuter, (LPVOID*) &unk );
162 if (SUCCEEDED(r))
163 {
164 r = IUnknown_QueryInterface( unk, riid, ppobj );
165 IUnknown_Release( unk );
166 }
167 return r;
168 }
169
170 static HRESULT WINAPI MsiCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
171 {
172 TRACE("%p %d\n", iface, dolock);
173
174 if (dolock)
175 LockModule();
176 else
177 UnlockModule();
178
179 return S_OK;
180 }
181
182 static const IClassFactoryVtbl MsiCF_Vtbl =
183 {
184 MsiCF_QueryInterface,
185 MsiCF_AddRef,
186 MsiCF_Release,
187 MsiCF_CreateInstance,
188 MsiCF_LockServer
189 };
190
191 static IClassFactoryImpl MsiServer_CF = { &MsiCF_Vtbl, create_msiserver };
192 static IClassFactoryImpl WineMsiCustomRemote_CF = { &MsiCF_Vtbl, create_msi_custom_remote };
193 static IClassFactoryImpl WineMsiRemotePackage_CF = { &MsiCF_Vtbl, create_msi_remote_package };
194
195 /******************************************************************
196 * DllGetClassObject [MSI.@]
197 */
198 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
199 {
200 TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
201
202 if ( IsEqualCLSID (rclsid, &CLSID_IMsiServerX2) )
203 {
204 *ppv = &MsiServer_CF;
205 return S_OK;
206 }
207
208 if ( IsEqualCLSID (rclsid, &CLSID_IWineMsiRemoteCustomAction) )
209 {
210 *ppv = &WineMsiCustomRemote_CF;
211 return S_OK;
212 }
213
214 if ( IsEqualCLSID (rclsid, &CLSID_IWineMsiRemotePackage) )
215 {
216 *ppv = &WineMsiRemotePackage_CF;
217 return S_OK;
218 }
219
220 if( IsEqualCLSID (rclsid, &CLSID_IMsiServerMessage) ||
221 IsEqualCLSID (rclsid, &CLSID_IMsiServer) ||
222 IsEqualCLSID (rclsid, &CLSID_IMsiServerX1) ||
223 IsEqualCLSID (rclsid, &CLSID_IMsiServerX3) )
224 {
225 FIXME("create %s object\n", debugstr_guid( rclsid ));
226 }
227
228 return CLASS_E_CLASSNOTAVAILABLE;
229 }
230
231 /******************************************************************
232 * DllGetVersion [MSI.@]
233 */
234 HRESULT WINAPI DllGetVersion(DLLVERSIONINFO *pdvi)
235 {
236 TRACE("%p\n",pdvi);
237
238 if (pdvi->cbSize < sizeof(DLLVERSIONINFO))
239 return E_INVALIDARG;
240
241 pdvi->dwMajorVersion = MSI_MAJORVERSION;
242 pdvi->dwMinorVersion = MSI_MINORVERSION;
243 pdvi->dwBuildNumber = MSI_BUILDNUMBER;
244 pdvi->dwPlatformID = DLLVER_PLATFORM_WINDOWS;
245
246 return S_OK;
247 }
248
249 /******************************************************************
250 * DllCanUnloadNow [MSI.@]
251 */
252 HRESULT WINAPI DllCanUnloadNow(void)
253 {
254 return dll_count == 0 ? S_OK : S_FALSE;
255 }