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