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