msi/tests: Fix the scope of todo_wine in the tests for MsiApplyMultiplePatches.
[wine] / dlls / 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 INSTALLUI_HANDLER_RECORD gUIHandlerRecord = NULL;
45 DWORD                    gUIFilter        = 0;
46 LPVOID                   gUIContext       = NULL;
47 WCHAR                   *gszLogFile       = NULL;
48 HINSTANCE msi_hInstance;
49
50 static WCHAR msi_path[MAX_PATH];
51 static ITypeLib *msi_typelib;
52
53 /*
54  * Dll lifetime tracking declaration
55  */
56 static void LockModule(void)
57 {
58     InterlockedIncrement(&dll_count);
59 }
60
61 static void UnlockModule(void)
62 {
63     InterlockedDecrement(&dll_count);
64 }
65
66 /******************************************************************
67  *      DllMain
68  */
69 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
70 {
71     switch (fdwReason)
72     {
73     case DLL_PROCESS_ATTACH:
74         msi_hInstance = hinstDLL;
75         DisableThreadLibraryCalls(hinstDLL);
76         break;
77     case DLL_PROCESS_DETACH:
78         if (msi_typelib) ITypeLib_Release( msi_typelib );
79         msi_dialog_unregister_class();
80         msi_free_handle_table();
81         msi_free( gszLogFile );
82         break;
83     }
84     return TRUE;
85 }
86
87 static CRITICAL_SECTION MSI_typelib_cs;
88 static CRITICAL_SECTION_DEBUG MSI_typelib_cs_debug =
89 {
90     0, 0, &MSI_typelib_cs,
91     { &MSI_typelib_cs_debug.ProcessLocksList,
92       &MSI_typelib_cs_debug.ProcessLocksList },
93       0, 0, { (DWORD_PTR)(__FILE__ ": MSI_typelib_cs") }
94 };
95 static CRITICAL_SECTION MSI_typelib_cs = { &MSI_typelib_cs_debug, -1, 0, 0, 0, 0 };
96
97 ITypeLib *get_msi_typelib( LPWSTR *path )
98 {
99     EnterCriticalSection( &MSI_typelib_cs );
100
101     if (!msi_typelib)
102     {
103         TRACE("loading typelib\n");
104
105         if (GetModuleFileNameW( msi_hInstance, msi_path, MAX_PATH ))
106             LoadTypeLib( msi_path, &msi_typelib );
107     }
108
109     LeaveCriticalSection( &MSI_typelib_cs );
110
111     if (path)
112         *path = msi_path;
113
114     if (msi_typelib)
115         ITypeLib_AddRef( msi_typelib );
116
117     return msi_typelib;
118 }
119
120 typedef struct tagIClassFactoryImpl {
121     const IClassFactoryVtbl *lpVtbl;
122     HRESULT (*create_object)( IUnknown*, LPVOID* );
123 } IClassFactoryImpl;
124
125 static HRESULT WINAPI MsiCF_QueryInterface(LPCLASSFACTORY iface,
126                 REFIID riid,LPVOID *ppobj)
127 {
128     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
129
130     TRACE("%p %s %p\n",This,debugstr_guid(riid),ppobj);
131
132     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
133         IsEqualCLSID( riid, &IID_IClassFactory ) )
134     {
135         IClassFactory_AddRef( iface );
136         *ppobj = iface;
137         return S_OK;
138     }
139     return E_NOINTERFACE;
140 }
141
142 static ULONG WINAPI MsiCF_AddRef(LPCLASSFACTORY iface)
143 {
144     LockModule();
145     return 2;
146 }
147
148 static ULONG WINAPI MsiCF_Release(LPCLASSFACTORY iface)
149 {
150     UnlockModule();
151     return 1;
152 }
153
154 static HRESULT WINAPI MsiCF_CreateInstance(LPCLASSFACTORY iface,
155     LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj)
156 {
157     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
158     IUnknown *unk = NULL;
159     HRESULT r;
160
161     TRACE("%p %p %s %p\n", This, pOuter, debugstr_guid(riid), ppobj);
162
163     r = This->create_object( pOuter, (LPVOID*) &unk );
164     if (SUCCEEDED(r))
165     {
166         r = IUnknown_QueryInterface( unk, riid, ppobj );
167         IUnknown_Release( unk );
168     }
169     return r;
170 }
171
172 static HRESULT WINAPI MsiCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
173 {
174     TRACE("%p %d\n", iface, dolock);
175
176     if (dolock)
177         LockModule();
178     else
179         UnlockModule();
180
181     return S_OK;
182 }
183
184 static const IClassFactoryVtbl MsiCF_Vtbl =
185 {
186     MsiCF_QueryInterface,
187     MsiCF_AddRef,
188     MsiCF_Release,
189     MsiCF_CreateInstance,
190     MsiCF_LockServer
191 };
192
193 static IClassFactoryImpl MsiServer_CF = { &MsiCF_Vtbl, create_msiserver };
194 static IClassFactoryImpl WineMsiCustomRemote_CF = { &MsiCF_Vtbl, create_msi_custom_remote };
195 static IClassFactoryImpl WineMsiRemotePackage_CF = { &MsiCF_Vtbl, create_msi_remote_package };
196
197 /******************************************************************
198  * DllGetClassObject          [MSI.@]
199  */
200 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
201 {
202     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
203
204     if ( IsEqualCLSID (rclsid, &CLSID_IMsiServerX2) )
205     {
206         *ppv = &MsiServer_CF;
207         return S_OK;
208     }
209
210     if ( IsEqualCLSID (rclsid, &CLSID_IWineMsiRemoteCustomAction) )
211     {
212         *ppv = &WineMsiCustomRemote_CF;
213         return S_OK;
214     }
215
216     if ( IsEqualCLSID (rclsid, &CLSID_IWineMsiRemotePackage) )
217     {
218         *ppv = &WineMsiRemotePackage_CF;
219         return S_OK;
220     }
221
222     if( IsEqualCLSID (rclsid, &CLSID_IMsiServerMessage) ||
223         IsEqualCLSID (rclsid, &CLSID_IMsiServer) ||
224         IsEqualCLSID (rclsid, &CLSID_IMsiServerX1) ||
225         IsEqualCLSID (rclsid, &CLSID_IMsiServerX3) )
226     {
227         FIXME("create %s object\n", debugstr_guid( rclsid ));
228     }
229
230     return CLASS_E_CLASSNOTAVAILABLE;
231 }
232
233 /******************************************************************
234  * DllGetVersion              [MSI.@]
235  */
236 HRESULT WINAPI DllGetVersion(DLLVERSIONINFO *pdvi)
237 {
238     TRACE("%p\n",pdvi);
239
240     if (pdvi->cbSize < sizeof(DLLVERSIONINFO))
241         return E_INVALIDARG;
242
243     pdvi->dwMajorVersion = MSI_MAJORVERSION;
244     pdvi->dwMinorVersion = MSI_MINORVERSION;
245     pdvi->dwBuildNumber = MSI_BUILDNUMBER;
246     pdvi->dwPlatformID = DLLVER_PLATFORM_WINDOWS;
247
248     return S_OK;
249 }
250
251 /******************************************************************
252  * DllCanUnloadNow            [MSI.@]
253  */
254 HRESULT WINAPI DllCanUnloadNow(void)
255 {
256     return dll_count == 0 ? S_OK : S_FALSE;
257 }