[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 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 DWORD ret = ERROR_INSTALL_FAILURE;
92
93 CoInitialize(NULL);
94
95 /* Create MsiActiveScriptSite object */
96 hr = create_ActiveScriptSite(NULL, (void **)&pActiveScriptSite);
97 if (hr != S_OK) goto done;
98
99 /* Create an installer object */
100 hr = create_msiserver(NULL, (LPVOID *)&pActiveScriptSite->pInstaller);
101 if (hr != S_OK) goto done;
102
103 /* Create a session object */
104 hr = create_session(hPackage, pActiveScriptSite->pInstaller, &pActiveScriptSite->pSession);
105 if (hr != S_OK) goto done;
106
107 /* Create the scripting engine */
108 if ((type & 7) == msidbCustomActionTypeJScript)
109 hr = CLSIDFromProgID(szJScript, &clsid);
110 else if ((type & 7) == msidbCustomActionTypeVBScript)
111 hr = CLSIDFromProgID(szVBScript, &clsid);
112 else {
113 ERR("Unknown script type %d\n", type);
114 goto done;
115 }
116 if (FAILED(hr)) {
117 ERR("Could not find CLSID for Windows Script\n");
118 goto done;
119 }
120 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IActiveScript, (void **)&pActiveScript);
121 if (FAILED(hr)) {
122 ERR("Could not instantiate class for Windows Script\n");
123 goto done;
124 }
125
126 /* Get the IActiveScriptParse engine interface */
127 hr = IActiveScript_QueryInterface(pActiveScript, &IID_IActiveScriptParse, (void **)&pActiveScriptParse);
128 if (FAILED(hr)) goto done;
129
130 /* Give our host to the engine */
131 hr = IActiveScript_SetScriptSite(pActiveScript, (IActiveScriptSite *)pActiveScriptSite);
132 if (FAILED(hr)) goto done;
133
134 /* Initialize the script engine */
135 hr = IActiveScriptParse64_InitNew(pActiveScriptParse);
136 if (FAILED(hr)) goto done;
137
138 /* Add the session object */
139 hr = IActiveScript_AddNamedItem(pActiveScript, szSession, SCRIPTITEM_GLOBALMEMBERS);
140 if (FAILED(hr)) goto done;
141
142 /* Pass the script to the engine */
143 hr = IActiveScriptParse64_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 &&
185 pActiveScriptSite->pInstaller) IUnknown_Release((IUnknown *)pActiveScriptSite->pInstaller);
186 if (pActiveScriptSite) IUnknown_Release((IUnknown *)pActiveScriptSite);
187
188 CoUninitialize(); /* must call even if CoInitialize failed */
189
190 return ret;
191 }
192
193 /*
194 * MsiActiveScriptSite
195 */
196
197 /*** IUnknown methods ***/
198 static HRESULT WINAPI MsiActiveScriptSite_QueryInterface(IActiveScriptSite* iface, REFIID riid, void** ppvObject)
199 {
200 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
201
202 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
203
204 if (IsEqualGUID(riid, &IID_IUnknown) ||
205 IsEqualGUID(riid, &IID_IActiveScriptSite))
206 {
207 IClassFactory_AddRef(iface);
208 *ppvObject = This;
209 return S_OK;
210 }
211
212 TRACE("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
213
214 return E_NOINTERFACE;
215 }
216
217 static ULONG WINAPI MsiActiveScriptSite_AddRef(IActiveScriptSite* iface)
218 {
219 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
220
221 TRACE("(%p/%p)\n", iface, This);
222
223 return InterlockedIncrement(&This->ref);
224 }
225
226 static ULONG WINAPI MsiActiveScriptSite_Release(IActiveScriptSite* iface)
227 {
228 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
229 ULONG ref = InterlockedDecrement(&This->ref);
230
231 TRACE("(%p/%p)\n", iface, This);
232
233 if (!ref)
234 msi_free(This);
235
236 return ref;
237 }
238
239 /*** IActiveScriptSite methods **/
240 static HRESULT WINAPI MsiActiveScriptSite_GetLCID(IActiveScriptSite* iface, LCID* plcid)
241 {
242 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
243 TRACE("(%p/%p)->(%p)\n", This, iface, plcid);
244 return E_NOTIMPL; /* Script will use system-defined locale */
245 }
246
247 static HRESULT WINAPI MsiActiveScriptSite_GetItemInfo(IActiveScriptSite* iface, LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppiunkItem, ITypeInfo** ppti)
248 {
249 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
250 TRACE("(%p/%p)->(%p,%d,%p,%p)\n", This, iface, pstrName, dwReturnMask, ppiunkItem, ppti);
251
252 /* Determine the kind of pointer that is requested, and make sure placeholder is valid */
253 if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
254 if (!ppti) return E_INVALIDARG;
255 *ppti = NULL;
256 }
257 if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
258 if (!ppiunkItem) return E_INVALIDARG;
259 *ppiunkItem = NULL;
260 }
261
262 /* Are we looking for the session object? */
263 if (!strcmpW(szSession, pstrName)) {
264 if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
265 return load_type_info(This->pSession, ppti, &DIID_Session, 0);
266 else if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
267 IDispatch_QueryInterface(This->pSession, &IID_IUnknown, (void **)ppiunkItem);
268 return S_OK;
269 }
270 }
271
272 return TYPE_E_ELEMENTNOTFOUND;
273 }
274
275 static HRESULT WINAPI MsiActiveScriptSite_GetDocVersionString(IActiveScriptSite* iface, BSTR* pbstrVersion)
276 {
277 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
278 TRACE("(%p/%p)->(%p)\n", This, iface, pbstrVersion);
279 return E_NOTIMPL;
280 }
281
282 static HRESULT WINAPI MsiActiveScriptSite_OnScriptTerminate(IActiveScriptSite* iface, const VARIANT* pvarResult, const EXCEPINFO* pexcepinfo)
283 {
284 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
285 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pvarResult, pexcepinfo);
286 return S_OK;
287 }
288
289 static HRESULT WINAPI MsiActiveScriptSite_OnStateChange(IActiveScriptSite* iface, SCRIPTSTATE ssScriptState)
290 {
291 switch (ssScriptState) {
292 case SCRIPTSTATE_UNINITIALIZED:
293 TRACE("State: Uninitialized.\n");
294 break;
295
296 case SCRIPTSTATE_INITIALIZED:
297 TRACE("State: Initialized.\n");
298 break;
299
300 case SCRIPTSTATE_STARTED:
301 TRACE("State: Started.\n");
302 break;
303
304 case SCRIPTSTATE_CONNECTED:
305 TRACE("State: Connected.\n");
306 break;
307
308 case SCRIPTSTATE_DISCONNECTED:
309 TRACE("State: Disconnected.\n");
310 break;
311
312 case SCRIPTSTATE_CLOSED:
313 TRACE("State: Closed.\n");
314 break;
315
316 default:
317 ERR("Unknown State: %d\n", ssScriptState);
318 break;
319 }
320
321 return S_OK;
322 }
323
324 static HRESULT WINAPI MsiActiveScriptSite_OnScriptError(IActiveScriptSite* iface, IActiveScriptError* pscripterror)
325 {
326 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
327 EXCEPINFO exception;
328 HRESULT hr;
329
330 TRACE("(%p/%p)->(%p)\n", This, iface, pscripterror);
331
332 memset(&exception, 0, sizeof(EXCEPINFO));
333 hr = IActiveScriptError_GetExceptionInfo(pscripterror, &exception);
334 if (SUCCEEDED(hr))
335 {
336 ERR("script error: %s\n", debugstr_w(exception.bstrDescription));
337 SysFreeString(exception.bstrSource);
338 SysFreeString(exception.bstrDescription);
339 SysFreeString(exception.bstrHelpFile);
340 }
341
342 return S_OK;
343 }
344
345 static HRESULT WINAPI MsiActiveScriptSite_OnEnterScript(IActiveScriptSite* iface)
346 {
347 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
348 TRACE("(%p/%p)\n", This, iface);
349 return S_OK;
350 }
351
352 static HRESULT WINAPI MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite* iface)
353 {
354 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
355 TRACE("(%p/%p)\n", This, iface);
356 return S_OK;
357 }
358
359 static const struct IActiveScriptSiteVtbl ASS_Vtbl =
360 {
361 MsiActiveScriptSite_QueryInterface,
362 MsiActiveScriptSite_AddRef,
363 MsiActiveScriptSite_Release,
364 MsiActiveScriptSite_GetLCID,
365 MsiActiveScriptSite_GetItemInfo,
366 MsiActiveScriptSite_GetDocVersionString,
367 MsiActiveScriptSite_OnScriptTerminate,
368 MsiActiveScriptSite_OnStateChange,
369 MsiActiveScriptSite_OnScriptError,
370 MsiActiveScriptSite_OnEnterScript,
371 MsiActiveScriptSite_OnLeaveScript
372 };