kernel32: Moved EnumPageFiles[AW] implementation to kernel32.
[wine] / dlls / msi / script.c
1 /*
2  * Implementation of scripting for Microsoft Installer (msi.dll)
3  *
4  * Copyright 2007 Misha Koshelev
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 #define COBJMACROS
22
23 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winuser.h"
28 #include "msidefs.h"
29 #include "msipriv.h"
30 #include "activscp.h"
31 #include "oleauto.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "msiserver.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38
39 static const WCHAR szJScript[] = { 'J','S','c','r','i','p','t',0};
40 static const WCHAR szVBScript[] = { 'V','B','S','c','r','i','p','t',0};
41 static const WCHAR szSession[] = {'S','e','s','s','i','o','n',0};
42
43 /*
44  * MsiActiveScriptSite - Our IActiveScriptSite implementation.
45  */
46
47 typedef struct {
48     IActiveScriptSite lpVtbl;
49     IDispatch *pInstaller;
50     IDispatch *pSession;
51     LONG ref;
52 } MsiActiveScriptSite;
53
54 static const struct IActiveScriptSiteVtbl ASS_Vtbl;
55
56 static HRESULT create_ActiveScriptSite(IUnknown *pUnkOuter, LPVOID *ppObj)
57 {
58     MsiActiveScriptSite* object;
59
60     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
61
62     if( pUnkOuter )
63         return CLASS_E_NOAGGREGATION;
64
65     object = msi_alloc_zero( sizeof(MsiActiveScriptSite) );
66
67     object->lpVtbl.lpVtbl = &ASS_Vtbl;
68     object->ref = 1;
69     object->pInstaller = NULL;
70     object->pSession = NULL;
71
72     *ppObj = object;
73
74     return S_OK;
75 }
76
77 /*
78  * Call a script.
79  */
80 DWORD call_script(MSIHANDLE hPackage, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action)
81 {
82     HRESULT hr;
83     IActiveScript *pActiveScript = NULL;
84     IActiveScriptParse *pActiveScriptParse = NULL;
85     MsiActiveScriptSite *pActiveScriptSite = NULL;
86     IDispatch *pDispatch = NULL;
87     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
88     DISPID dispid;
89     CLSID clsid;
90     VARIANT var;
91
92     /* Return success by default (if Windows Script not installed) - not native behavior. This
93      * should be here until we implement wine scripting. */
94     DWORD ret = ERROR_SUCCESS;
95
96     CoInitialize(NULL);
97
98     /* Create MsiActiveScriptSite object */
99     hr = create_ActiveScriptSite(NULL, (void **)&pActiveScriptSite);
100     if (hr != S_OK) goto done;
101
102     /* Create an installer object */
103     hr = create_msiserver(NULL, (LPVOID *)&pActiveScriptSite->pInstaller);
104     if (hr != S_OK) goto done;
105
106     /* Create a session object */
107     hr = create_session(hPackage, pActiveScriptSite->pInstaller, &pActiveScriptSite->pSession);
108     if (hr != S_OK) goto done;
109
110     /* Create the scripting engine */
111     if ((type & 7) == msidbCustomActionTypeJScript)
112         hr = CLSIDFromProgID(szJScript, &clsid);
113     else if ((type & 7) == msidbCustomActionTypeVBScript)
114         hr = CLSIDFromProgID(szVBScript, &clsid);
115     else {
116         ERR("Unknown script type %d\n", type);
117         goto done;
118     }
119     if (FAILED(hr)) {
120         ERR("Could not find CLSID for Windows Script\n");
121         goto done;
122     }
123     hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IActiveScript, (void **)&pActiveScript);
124     if (FAILED(hr)) {
125         ERR("Could not instantiate class for Windows Script\n");
126         goto done;
127     }
128
129     /* If we got this far, Windows Script is installed, so don't return success by default anymore */
130     ret = ERROR_INSTALL_FAILURE;
131
132     /* Get the IActiveScriptParse engine interface */
133     hr = IActiveScript_QueryInterface(pActiveScript, &IID_IActiveScriptParse, (void **)&pActiveScriptParse);
134     if (FAILED(hr)) goto done;
135
136     /* Give our host to the engine */
137     hr = IActiveScript_SetScriptSite(pActiveScript, (IActiveScriptSite *)pActiveScriptSite);
138     if (FAILED(hr)) goto done;
139
140     /* Initialize the script engine */
141     hr = IActiveScriptParse64_InitNew(pActiveScriptParse);
142     if (FAILED(hr)) goto done;
143
144     /* Add the session object */
145     hr = IActiveScript_AddNamedItem(pActiveScript, szSession, SCRIPTITEM_ISVISIBLE);
146     if (FAILED(hr)) goto done;
147
148     /* Pass the script to the engine */
149     hr = IActiveScriptParse64_ParseScriptText(pActiveScriptParse, script, NULL, NULL, NULL, 0, 0, 0L, NULL, NULL);
150     if (FAILED(hr)) goto done;
151
152     /* Start processing the script */
153     hr = IActiveScript_SetScriptState(pActiveScript, SCRIPTSTATE_CONNECTED);
154     if (FAILED(hr)) goto done;
155
156     /* Call a function if necessary through the IDispatch interface */
157     if (function != NULL && strlenW(function) > 0) {
158         TRACE("Calling function %s\n", debugstr_w(function));
159
160         hr = IActiveScript_GetScriptDispatch(pActiveScript, NULL, &pDispatch);
161         if (FAILED(hr)) goto done;
162
163         hr = IDispatch_GetIDsOfNames(pDispatch, &IID_NULL, (WCHAR **)&function, 1,LOCALE_USER_DEFAULT, &dispid);
164         if (FAILED(hr)) goto done;
165
166         hr = IDispatch_Invoke(pDispatch, dispid, &IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &var, NULL, NULL);
167         if (FAILED(hr)) goto done;
168
169         /* Check return value, if it's not IDOK we failed */
170         hr = VariantChangeType(&var, &var, 0, VT_I4);
171         if (FAILED(hr)) goto done;
172
173         if (V_I4(&var) == IDOK)
174             ret = ERROR_SUCCESS;
175         else ret = ERROR_INSTALL_FAILURE;
176
177         VariantClear(&var);
178     } else {
179         /* If no function to be called, MSI behavior is to succeed */
180         ret = ERROR_SUCCESS;
181     }
182
183 done:
184
185     /* Free everything that needs to be freed */
186     if (pDispatch) IDispatch_Release(pDispatch);
187     if (pActiveScript) IActiveScriptSite_Release(pActiveScript);
188     if (pActiveScriptSite &&
189         pActiveScriptSite->pSession) IUnknown_Release((IUnknown *)pActiveScriptSite->pSession);
190     if (pActiveScriptSite &&
191         pActiveScriptSite->pInstaller) IUnknown_Release((IUnknown *)pActiveScriptSite->pInstaller);
192     if (pActiveScriptSite) IUnknown_Release((IUnknown *)pActiveScriptSite);
193
194     CoUninitialize();    /* must call even if CoInitialize failed */
195
196     return ret;
197 }
198
199 /*
200  * MsiActiveScriptSite
201  */
202
203 /*** IUnknown methods ***/
204 static HRESULT WINAPI MsiActiveScriptSite_QueryInterface(IActiveScriptSite* iface, REFIID riid, void** ppvObject)
205 {
206     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
207
208     TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
209
210     if (IsEqualGUID(riid, &IID_IUnknown) ||
211         IsEqualGUID(riid, &IID_IActiveScriptSite))
212     {
213         IClassFactory_AddRef(iface);
214         *ppvObject = This;
215         return S_OK;
216     }
217
218     TRACE("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
219
220     return E_NOINTERFACE;
221 }
222
223 static ULONG WINAPI MsiActiveScriptSite_AddRef(IActiveScriptSite* iface)
224 {
225     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
226
227     TRACE("(%p/%p)\n", iface, This);
228
229     return InterlockedIncrement(&This->ref);
230 }
231
232 static ULONG WINAPI MsiActiveScriptSite_Release(IActiveScriptSite* iface)
233 {
234     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
235     ULONG ref = InterlockedDecrement(&This->ref);
236
237     TRACE("(%p/%p)\n", iface, This);
238
239     if (!ref)
240         msi_free(This);
241
242     return ref;
243 }
244
245 /*** IActiveScriptSite methods **/
246 static HRESULT WINAPI MsiActiveScriptSite_GetLCID(IActiveScriptSite* iface, LCID* plcid)
247 {
248     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
249     TRACE("(%p/%p)->(%p)\n", This, iface, plcid);
250     return E_NOTIMPL;  /* Script will use system-defined locale */
251 }
252
253 static HRESULT WINAPI MsiActiveScriptSite_GetItemInfo(IActiveScriptSite* iface, LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppiunkItem, ITypeInfo** ppti)
254 {
255     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
256     TRACE("(%p/%p)->(%p,%d,%p,%p)\n", This, iface, pstrName, dwReturnMask, ppiunkItem, ppti);
257
258     /* Determine the kind of pointer that is requested, and make sure placeholder is valid */
259     if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
260         if (!ppti) return E_INVALIDARG;
261         *ppti = NULL;
262     }
263     if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
264         if (!ppiunkItem) return E_INVALIDARG;
265         *ppiunkItem = NULL;
266     }
267
268     /* Are we looking for the session object? */
269     if (!strcmpW(szSession, pstrName)) {
270         if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
271             return load_type_info(This->pSession, ppti, &DIID_Session, 0);
272         else if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
273             IDispatch_QueryInterface(This->pSession, &IID_IUnknown, (void **)ppiunkItem);
274             return S_OK;
275         }
276     }
277
278     return TYPE_E_ELEMENTNOTFOUND;
279 }
280
281 static HRESULT WINAPI MsiActiveScriptSite_GetDocVersionString(IActiveScriptSite* iface, BSTR* pbstrVersion)
282 {
283     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
284     TRACE("(%p/%p)->(%p)\n", This, iface, pbstrVersion);
285     return E_NOTIMPL;
286 }
287
288 static HRESULT WINAPI MsiActiveScriptSite_OnScriptTerminate(IActiveScriptSite* iface, const VARIANT* pvarResult, const EXCEPINFO* pexcepinfo)
289 {
290     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
291     TRACE("(%p/%p)->(%p,%p)\n", This, iface, pvarResult, pexcepinfo);
292     return S_OK;
293 }
294
295 static HRESULT WINAPI MsiActiveScriptSite_OnStateChange(IActiveScriptSite* iface, SCRIPTSTATE ssScriptState)
296 {
297     switch (ssScriptState) {
298         case SCRIPTSTATE_UNINITIALIZED:
299               TRACE("State: Uninitialized.\n");
300               break;
301
302         case SCRIPTSTATE_INITIALIZED:
303               TRACE("State: Initialized.\n");
304               break;
305
306         case SCRIPTSTATE_STARTED:
307               TRACE("State: Started.\n");
308               break;
309
310         case SCRIPTSTATE_CONNECTED:
311               TRACE("State: Connected.\n");
312               break;
313
314         case SCRIPTSTATE_DISCONNECTED:
315               TRACE("State: Disconnected.\n");
316               break;
317
318         case SCRIPTSTATE_CLOSED:
319               TRACE("State: Closed.\n");
320               break;
321
322         default:
323               ERR("Unknown State: %d\n", ssScriptState);
324               break;
325     }
326
327     return S_OK;
328 }
329
330 static HRESULT WINAPI MsiActiveScriptSite_OnScriptError(IActiveScriptSite* iface, IActiveScriptError* pscripterror)
331 {
332     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
333     EXCEPINFO exception;
334     HRESULT hr;
335
336     TRACE("(%p/%p)->(%p)\n", This, iface, pscripterror);
337
338     memset(&exception, 0, sizeof(EXCEPINFO));
339     hr = IActiveScriptError_GetExceptionInfo(pscripterror, &exception);
340     if (SUCCEEDED(hr))
341     {
342         ERR("script error: %s\n", debugstr_w(exception.bstrDescription));
343         SysFreeString(exception.bstrSource);
344         SysFreeString(exception.bstrDescription);
345         SysFreeString(exception.bstrHelpFile);
346     }
347
348     return S_OK;
349 }
350
351 static HRESULT WINAPI MsiActiveScriptSite_OnEnterScript(IActiveScriptSite* iface)
352 {
353     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
354     TRACE("(%p/%p)\n", This, iface);
355     return S_OK;
356 }
357
358 static HRESULT WINAPI MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite* iface)
359 {
360     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
361     TRACE("(%p/%p)\n", This, iface);
362     return S_OK;
363 }
364
365 static const struct IActiveScriptSiteVtbl ASS_Vtbl =
366 {
367     MsiActiveScriptSite_QueryInterface,
368     MsiActiveScriptSite_AddRef,
369     MsiActiveScriptSite_Release,
370     MsiActiveScriptSite_GetLCID,
371     MsiActiveScriptSite_GetItemInfo,
372     MsiActiveScriptSite_GetDocVersionString,
373     MsiActiveScriptSite_OnScriptTerminate,
374     MsiActiveScriptSite_OnStateChange,
375     MsiActiveScriptSite_OnScriptError,
376     MsiActiveScriptSite_OnEnterScript,
377     MsiActiveScriptSite_OnLeaveScript
378 };