msi: Abstract MSI_RecordSetStream.
[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 DWORD gUIFilter = 0;
45 LPVOID gUIContext = NULL;
46 WCHAR gszLogFile[MAX_PATH];
47 WCHAR msi_path[MAX_PATH];
48 ITypeLib *msi_typelib = NULL;
49 HINSTANCE msi_hInstance;
50
51 /*
52  * Dll lifetime tracking declaration
53  */
54 static void LockModule(void)
55 {
56     InterlockedIncrement(&dll_count);
57 }
58
59 static void UnlockModule(void)
60 {
61     InterlockedDecrement(&dll_count);
62 }
63
64 /******************************************************************
65  *      DllMain
66  */
67 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
68 {
69     switch (fdwReason)
70     {
71     case DLL_PROCESS_ATTACH:
72         msi_hInstance = hinstDLL;
73         DisableThreadLibraryCalls(hinstDLL);
74         msi_dialog_register_class();
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 static HRESULT create_msiserver( IUnknown *pOuter, LPVOID *ppObj )
119 {
120     FIXME("\n");
121     return E_FAIL;
122 }
123
124 typedef struct tagIClassFactoryImpl {
125     const IClassFactoryVtbl *lpVtbl;
126     HRESULT (*create_object)( IUnknown*, LPVOID* );
127 } IClassFactoryImpl;
128
129 static HRESULT WINAPI MsiCF_QueryInterface(LPCLASSFACTORY iface,
130                 REFIID riid,LPVOID *ppobj)
131 {
132     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
133
134     TRACE("%p %s %p\n",This,debugstr_guid(riid),ppobj);
135
136     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
137         IsEqualCLSID( riid, &IID_IClassFactory ) )
138     {
139         IClassFactory_AddRef( iface );
140         *ppobj = iface;
141         return S_OK;
142     }
143     return E_NOINTERFACE;
144 }
145
146 static ULONG WINAPI MsiCF_AddRef(LPCLASSFACTORY iface)
147 {
148     LockModule();
149     return 2;
150 }
151
152 static ULONG WINAPI MsiCF_Release(LPCLASSFACTORY iface)
153 {
154     UnlockModule();
155     return 1;
156 }
157
158 static HRESULT WINAPI MsiCF_CreateInstance(LPCLASSFACTORY iface,
159     LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj)
160 {
161     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
162     IUnknown *unk = NULL;
163     HRESULT r;
164
165     TRACE("%p %p %s %p\n", This, pOuter, debugstr_guid(riid), ppobj);
166
167     r = This->create_object( pOuter, (LPVOID*) &unk );
168     if (SUCCEEDED(r))
169     {
170         r = IUnknown_QueryInterface( unk, riid, ppobj );
171         IUnknown_Release( unk );
172     }
173     return r;
174 }
175
176 static HRESULT WINAPI MsiCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
177 {
178     TRACE("%p %d\n", iface, dolock);
179
180     if (dolock)
181         LockModule();
182     else
183         UnlockModule();
184
185     return S_OK;
186 }
187
188 static const IClassFactoryVtbl MsiCF_Vtbl =
189 {
190     MsiCF_QueryInterface,
191     MsiCF_AddRef,
192     MsiCF_Release,
193     MsiCF_CreateInstance,
194     MsiCF_LockServer
195 };
196
197 static IClassFactoryImpl MsiServer_CF = { &MsiCF_Vtbl, create_msiserver };
198
199 /******************************************************************
200  * DllGetClassObject          [MSI.@]
201  */
202 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
203 {
204     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
205
206     if ( IsEqualCLSID (rclsid, &CLSID_IMsiServerX2) )
207     {
208         *ppv = (LPVOID) &MsiServer_CF;
209         return S_OK;
210     }
211
212     if( IsEqualCLSID (rclsid, &CLSID_IMsiServerMessage) ||
213         IsEqualCLSID (rclsid, &CLSID_IMsiServer) ||
214         IsEqualCLSID (rclsid, &CLSID_IMsiServerX1) ||
215         IsEqualCLSID (rclsid, &CLSID_IMsiServerX3) )
216     {
217         FIXME("create %s object\n", debugstr_guid( rclsid ));
218     }
219
220     return CLASS_E_CLASSNOTAVAILABLE;
221 }
222
223 /******************************************************************
224  * DllGetVersion              [MSI.@]
225  */
226 HRESULT WINAPI DllGetVersion(DLLVERSIONINFO *pdvi)
227 {
228     TRACE("%p\n",pdvi);
229
230     if (pdvi->cbSize < sizeof(DLLVERSIONINFO))
231         return E_INVALIDARG;
232
233     pdvi->dwMajorVersion = MSI_MAJORVERSION;
234     pdvi->dwMinorVersion = MSI_MINORVERSION;
235     pdvi->dwBuildNumber = MSI_BUILDNUMBER;
236     pdvi->dwPlatformID = DLLVER_PLATFORM_WINDOWS;
237
238     return S_OK;
239 }
240
241 /******************************************************************
242  * DllCanUnloadNow            [MSI.@]
243  */
244 HRESULT WINAPI DllCanUnloadNow(void)
245 {
246     return dll_count == 0 ? S_OK : S_FALSE;
247 }