mstask: Call interface methods properly.
[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     IActiveScriptParse32 *pActiveScriptParse32 = NULL;
85     IActiveScriptParse64 *pActiveScriptParse64 = NULL;
86     MsiActiveScriptSite *pActiveScriptSite = NULL;
87     IDispatch *pDispatch = NULL;
88     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
89     DISPID dispid;
90     CLSID clsid;
91     VARIANT var;
92     DWORD ret = ERROR_INSTALL_FAILURE;
93
94     CoInitialize(NULL);
95
96     /* Create MsiActiveScriptSite object */
97     hr = create_ActiveScriptSite(NULL, (void **)&pActiveScriptSite);
98     if (hr != S_OK) goto done;
99
100     /* Create an installer object */
101     hr = create_msiserver(NULL, (LPVOID *)&pActiveScriptSite->pInstaller);
102     if (hr != S_OK) goto done;
103
104     /* Create a session object */
105     hr = create_session(hPackage, pActiveScriptSite->pInstaller, &pActiveScriptSite->pSession);
106     if (hr != S_OK) goto done;
107
108     /* Create the scripting engine */
109     if ((type & 7) == msidbCustomActionTypeJScript)
110         hr = CLSIDFromProgID(szJScript, &clsid);
111     else if ((type & 7) == msidbCustomActionTypeVBScript)
112         hr = CLSIDFromProgID(szVBScript, &clsid);
113     else {
114         ERR("Unknown script type %d\n", type);
115         goto done;
116     }
117     if (FAILED(hr)) {
118         ERR("Could not find CLSID for Windows Script\n");
119         goto done;
120     }
121     hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IActiveScript, (void **)&pActiveScript);
122     if (FAILED(hr)) {
123         ERR("Could not instantiate class for Windows Script\n");
124         goto done;
125     }
126
127     if (type & msidbCustomActionType64BitScript)
128     {
129         hr = IActiveScript_QueryInterface(pActiveScript, &IID_IActiveScriptParse64, (void **)&pActiveScriptParse64);
130         if (FAILED(hr)) goto done;
131
132         hr = IActiveScript_SetScriptSite(pActiveScript, (IActiveScriptSite *)pActiveScriptSite);
133         if (FAILED(hr)) goto done;
134
135         hr = IActiveScriptParse64_InitNew(pActiveScriptParse64);
136         if (FAILED(hr)) goto done;
137
138         hr = IActiveScript_AddNamedItem(pActiveScript, szSession, SCRIPTITEM_GLOBALMEMBERS);
139         if (FAILED(hr)) goto done;
140
141         hr = IActiveScriptParse64_ParseScriptText(pActiveScriptParse64, script, NULL, NULL, NULL, 0, 0, 0L, NULL, NULL);
142         if (FAILED(hr)) goto done;
143     }
144     else
145     {
146         hr = IActiveScript_QueryInterface(pActiveScript, &IID_IActiveScriptParse32, (void **)&pActiveScriptParse32);
147         if (FAILED(hr)) goto done;
148
149         hr = IActiveScript_SetScriptSite(pActiveScript, (IActiveScriptSite *)pActiveScriptSite);
150         if (FAILED(hr)) goto done;
151
152         hr = IActiveScriptParse32_InitNew(pActiveScriptParse32);
153         if (FAILED(hr)) goto done;
154
155         hr = IActiveScript_AddNamedItem(pActiveScript, szSession, SCRIPTITEM_GLOBALMEMBERS);
156         if (FAILED(hr)) goto done;
157
158         hr = IActiveScriptParse32_ParseScriptText(pActiveScriptParse32, script, NULL, NULL, NULL, 0, 0, 0L, NULL, NULL);
159         if (FAILED(hr)) goto done;
160     }
161
162     hr = IActiveScript_SetScriptState(pActiveScript, SCRIPTSTATE_CONNECTED);
163     if (FAILED(hr)) goto done;
164
165     /* Call a function if necessary through the IDispatch interface */
166     if (function != NULL && strlenW(function) > 0) {
167         TRACE("Calling function %s\n", debugstr_w(function));
168
169         hr = IActiveScript_GetScriptDispatch(pActiveScript, NULL, &pDispatch);
170         if (FAILED(hr)) goto done;
171
172         hr = IDispatch_GetIDsOfNames(pDispatch, &IID_NULL, (WCHAR **)&function, 1,LOCALE_USER_DEFAULT, &dispid);
173         if (FAILED(hr)) goto done;
174
175         hr = IDispatch_Invoke(pDispatch, dispid, &IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &var, NULL, NULL);
176         if (FAILED(hr)) goto done;
177
178         /* Check return value, if it's not IDOK we failed */
179         hr = VariantChangeType(&var, &var, 0, VT_I4);
180         if (FAILED(hr)) goto done;
181
182         if (V_I4(&var) == IDOK)
183             ret = ERROR_SUCCESS;
184         else ret = ERROR_INSTALL_FAILURE;
185
186         VariantClear(&var);
187     } else {
188         /* If no function to be called, MSI behavior is to succeed */
189         ret = ERROR_SUCCESS;
190     }
191
192 done:
193
194     if (pDispatch) IDispatch_Release(pDispatch);
195     if (pActiveScript) IActiveScript_Release(pActiveScript);
196     if (pActiveScriptParse32) IActiveScriptParse32_Release(pActiveScriptParse32);
197     if (pActiveScriptParse64) IActiveScriptParse64_Release(pActiveScriptParse64);
198     if (pActiveScriptSite)
199     {
200         if (pActiveScriptSite->pSession) IDispatch_Release(pActiveScriptSite->pSession);
201         if (pActiveScriptSite->pInstaller) IDispatch_Release(pActiveScriptSite->pInstaller);
202         IActiveScriptSite_Release((IActiveScriptSite *)pActiveScriptSite);
203     }
204     CoUninitialize();    /* must call even if CoInitialize failed */
205     return ret;
206 }
207
208 /*
209  * MsiActiveScriptSite
210  */
211
212 /*** IUnknown methods ***/
213 static HRESULT WINAPI MsiActiveScriptSite_QueryInterface(IActiveScriptSite* iface, REFIID riid, void** ppvObject)
214 {
215     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
216
217     TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
218
219     if (IsEqualGUID(riid, &IID_IUnknown) ||
220         IsEqualGUID(riid, &IID_IActiveScriptSite))
221     {
222         IActiveScriptSite_AddRef(iface);
223         *ppvObject = This;
224         return S_OK;
225     }
226
227     TRACE("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
228
229     return E_NOINTERFACE;
230 }
231
232 static ULONG WINAPI MsiActiveScriptSite_AddRef(IActiveScriptSite* iface)
233 {
234     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
235
236     TRACE("(%p/%p)\n", iface, This);
237
238     return InterlockedIncrement(&This->ref);
239 }
240
241 static ULONG WINAPI MsiActiveScriptSite_Release(IActiveScriptSite* iface)
242 {
243     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
244     ULONG ref = InterlockedDecrement(&This->ref);
245
246     TRACE("(%p/%p)\n", iface, This);
247
248     if (!ref)
249         msi_free(This);
250
251     return ref;
252 }
253
254 /*** IActiveScriptSite methods **/
255 static HRESULT WINAPI MsiActiveScriptSite_GetLCID(IActiveScriptSite* iface, LCID* plcid)
256 {
257     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
258     TRACE("(%p/%p)->(%p)\n", This, iface, plcid);
259     return E_NOTIMPL;  /* Script will use system-defined locale */
260 }
261
262 static HRESULT WINAPI MsiActiveScriptSite_GetItemInfo(IActiveScriptSite* iface, LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppiunkItem, ITypeInfo** ppti)
263 {
264     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
265     TRACE("(%p/%p)->(%p,%d,%p,%p)\n", This, iface, pstrName, dwReturnMask, ppiunkItem, ppti);
266
267     /* Determine the kind of pointer that is requested, and make sure placeholder is valid */
268     if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
269         if (!ppti) return E_INVALIDARG;
270         *ppti = NULL;
271     }
272     if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
273         if (!ppiunkItem) return E_INVALIDARG;
274         *ppiunkItem = NULL;
275     }
276
277     /* Are we looking for the session object? */
278     if (!strcmpW(szSession, pstrName)) {
279         if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
280             return load_type_info(This->pSession, ppti, &DIID_Session, 0);
281         else if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
282             IDispatch_QueryInterface(This->pSession, &IID_IUnknown, (void **)ppiunkItem);
283             return S_OK;
284         }
285     }
286
287     return TYPE_E_ELEMENTNOTFOUND;
288 }
289
290 static HRESULT WINAPI MsiActiveScriptSite_GetDocVersionString(IActiveScriptSite* iface, BSTR* pbstrVersion)
291 {
292     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
293     TRACE("(%p/%p)->(%p)\n", This, iface, pbstrVersion);
294     return E_NOTIMPL;
295 }
296
297 static HRESULT WINAPI MsiActiveScriptSite_OnScriptTerminate(IActiveScriptSite* iface, const VARIANT* pvarResult, const EXCEPINFO* pexcepinfo)
298 {
299     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
300     TRACE("(%p/%p)->(%p,%p)\n", This, iface, pvarResult, pexcepinfo);
301     return S_OK;
302 }
303
304 static HRESULT WINAPI MsiActiveScriptSite_OnStateChange(IActiveScriptSite* iface, SCRIPTSTATE ssScriptState)
305 {
306     switch (ssScriptState) {
307         case SCRIPTSTATE_UNINITIALIZED:
308               TRACE("State: Uninitialized.\n");
309               break;
310
311         case SCRIPTSTATE_INITIALIZED:
312               TRACE("State: Initialized.\n");
313               break;
314
315         case SCRIPTSTATE_STARTED:
316               TRACE("State: Started.\n");
317               break;
318
319         case SCRIPTSTATE_CONNECTED:
320               TRACE("State: Connected.\n");
321               break;
322
323         case SCRIPTSTATE_DISCONNECTED:
324               TRACE("State: Disconnected.\n");
325               break;
326
327         case SCRIPTSTATE_CLOSED:
328               TRACE("State: Closed.\n");
329               break;
330
331         default:
332               ERR("Unknown State: %d\n", ssScriptState);
333               break;
334     }
335
336     return S_OK;
337 }
338
339 static HRESULT WINAPI MsiActiveScriptSite_OnScriptError(IActiveScriptSite* iface, IActiveScriptError* pscripterror)
340 {
341     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
342     EXCEPINFO exception;
343     HRESULT hr;
344
345     TRACE("(%p/%p)->(%p)\n", This, iface, pscripterror);
346
347     memset(&exception, 0, sizeof(EXCEPINFO));
348     hr = IActiveScriptError_GetExceptionInfo(pscripterror, &exception);
349     if (SUCCEEDED(hr))
350     {
351         ERR("script error: %s\n", debugstr_w(exception.bstrDescription));
352         SysFreeString(exception.bstrSource);
353         SysFreeString(exception.bstrDescription);
354         SysFreeString(exception.bstrHelpFile);
355     }
356
357     return S_OK;
358 }
359
360 static HRESULT WINAPI MsiActiveScriptSite_OnEnterScript(IActiveScriptSite* iface)
361 {
362     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
363     TRACE("(%p/%p)\n", This, iface);
364     return S_OK;
365 }
366
367 static HRESULT WINAPI MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite* iface)
368 {
369     MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
370     TRACE("(%p/%p)\n", This, iface);
371     return S_OK;
372 }
373
374 static const struct IActiveScriptSiteVtbl ASS_Vtbl =
375 {
376     MsiActiveScriptSite_QueryInterface,
377     MsiActiveScriptSite_AddRef,
378     MsiActiveScriptSite_Release,
379     MsiActiveScriptSite_GetLCID,
380     MsiActiveScriptSite_GetItemInfo,
381     MsiActiveScriptSite_GetDocVersionString,
382     MsiActiveScriptSite_OnScriptTerminate,
383     MsiActiveScriptSite_OnStateChange,
384     MsiActiveScriptSite_OnScriptError,
385     MsiActiveScriptSite_OnEnterScript,
386     MsiActiveScriptSite_OnLeaveScript
387 };