mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / msi / script.c
blob4585c2b5922717c36621422749e076f17ae244fe
1 /*
2 * Implementation of scripting for Microsoft Installer (msi.dll)
4 * Copyright 2007 Misha Koshelev
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.
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.
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
21 #define COBJMACROS
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"
34 #include "msiserver.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 #ifdef _WIN64
40 #define IActiveScriptParse_Release IActiveScriptParse64_Release
41 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
42 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
44 #else
46 #define IActiveScriptParse_Release IActiveScriptParse32_Release
47 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
48 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
50 #endif
53 * MsiActiveScriptSite - Our IActiveScriptSite implementation.
55 typedef struct {
56 IActiveScriptSite IActiveScriptSite_iface;
57 IDispatch *installer;
58 IDispatch *session;
59 LONG ref;
60 } MsiActiveScriptSite;
62 static inline MsiActiveScriptSite *impl_from_IActiveScriptSite( IActiveScriptSite *iface )
64 return CONTAINING_RECORD(iface, MsiActiveScriptSite, IActiveScriptSite_iface);
68 * MsiActiveScriptSite
70 static HRESULT WINAPI MsiActiveScriptSite_QueryInterface(IActiveScriptSite* iface, REFIID riid, void** obj)
72 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
74 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
76 if (IsEqualGUID(riid, &IID_IUnknown) ||
77 IsEqualGUID(riid, &IID_IActiveScriptSite))
79 IActiveScriptSite_AddRef(iface);
80 *obj = iface;
81 return S_OK;
84 *obj = NULL;
86 return E_NOINTERFACE;
89 static ULONG WINAPI MsiActiveScriptSite_AddRef(IActiveScriptSite* iface)
91 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
92 ULONG ref = InterlockedIncrement(&This->ref);
93 TRACE("(%p)->(%d)\n", This, ref);
94 return ref;
97 static ULONG WINAPI MsiActiveScriptSite_Release(IActiveScriptSite* iface)
99 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
100 ULONG ref = InterlockedDecrement(&This->ref);
102 TRACE("(%p)->(%d)\n", This, ref);
104 if (!ref)
105 msi_free(This);
107 return ref;
110 static HRESULT WINAPI MsiActiveScriptSite_GetLCID(IActiveScriptSite* iface, LCID* plcid)
112 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
113 TRACE("(%p)->(%p)\n", This, plcid);
114 return E_NOTIMPL; /* Script will use system-defined locale */
117 static HRESULT WINAPI MsiActiveScriptSite_GetItemInfo(IActiveScriptSite* iface, LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppiunkItem, ITypeInfo** ppti)
119 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
121 TRACE("(%p)->(%p, %d, %p, %p)\n", This, pstrName, dwReturnMask, ppiunkItem, ppti);
123 /* Determine the kind of pointer that is requested, and make sure placeholder is valid */
124 if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
125 if (!ppti) return E_INVALIDARG;
126 *ppti = NULL;
128 if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
129 if (!ppiunkItem) return E_INVALIDARG;
130 *ppiunkItem = NULL;
133 /* Are we looking for the session object? */
134 if (!wcscmp(L"Session", pstrName)) {
135 if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
136 HRESULT hr = get_typeinfo(Session_tid, ppti);
137 if (SUCCEEDED(hr))
138 ITypeInfo_AddRef(*ppti);
139 return hr;
141 else if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
142 IDispatch_QueryInterface(This->session, &IID_IUnknown, (void **)ppiunkItem);
143 return S_OK;
147 return TYPE_E_ELEMENTNOTFOUND;
150 static HRESULT WINAPI MsiActiveScriptSite_GetDocVersionString(IActiveScriptSite* iface, BSTR* pbstrVersion)
152 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
153 TRACE("(%p)->(%p)\n", This, pbstrVersion);
154 return E_NOTIMPL;
157 static HRESULT WINAPI MsiActiveScriptSite_OnScriptTerminate(IActiveScriptSite* iface, const VARIANT* pvarResult, const EXCEPINFO* pexcepinfo)
159 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
160 TRACE("(%p)->(%p, %p)\n", This, pvarResult, pexcepinfo);
161 return S_OK;
164 static HRESULT WINAPI MsiActiveScriptSite_OnStateChange(IActiveScriptSite* iface, SCRIPTSTATE ssScriptState)
166 switch (ssScriptState) {
167 case SCRIPTSTATE_UNINITIALIZED:
168 TRACE("State: Uninitialized.\n");
169 break;
171 case SCRIPTSTATE_INITIALIZED:
172 TRACE("State: Initialized.\n");
173 break;
175 case SCRIPTSTATE_STARTED:
176 TRACE("State: Started.\n");
177 break;
179 case SCRIPTSTATE_CONNECTED:
180 TRACE("State: Connected.\n");
181 break;
183 case SCRIPTSTATE_DISCONNECTED:
184 TRACE("State: Disconnected.\n");
185 break;
187 case SCRIPTSTATE_CLOSED:
188 TRACE("State: Closed.\n");
189 break;
191 default:
192 ERR("Unknown State: %d\n", ssScriptState);
193 break;
196 return S_OK;
199 static HRESULT WINAPI MsiActiveScriptSite_OnScriptError(IActiveScriptSite* iface, IActiveScriptError* pscripterror)
201 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
202 EXCEPINFO exception;
203 HRESULT hr;
205 TRACE("(%p)->(%p)\n", This, pscripterror);
207 memset(&exception, 0, sizeof(EXCEPINFO));
208 hr = IActiveScriptError_GetExceptionInfo(pscripterror, &exception);
209 if (SUCCEEDED(hr))
211 ERR("script error: %s\n", debugstr_w(exception.bstrDescription));
212 SysFreeString(exception.bstrSource);
213 SysFreeString(exception.bstrDescription);
214 SysFreeString(exception.bstrHelpFile);
217 return S_OK;
220 static HRESULT WINAPI MsiActiveScriptSite_OnEnterScript(IActiveScriptSite* iface)
222 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
223 TRACE("(%p)\n", This);
224 return S_OK;
227 static HRESULT WINAPI MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite* iface)
229 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
230 TRACE("(%p)\n", This);
231 return S_OK;
234 static const struct IActiveScriptSiteVtbl activescriptsitevtbl =
236 MsiActiveScriptSite_QueryInterface,
237 MsiActiveScriptSite_AddRef,
238 MsiActiveScriptSite_Release,
239 MsiActiveScriptSite_GetLCID,
240 MsiActiveScriptSite_GetItemInfo,
241 MsiActiveScriptSite_GetDocVersionString,
242 MsiActiveScriptSite_OnScriptTerminate,
243 MsiActiveScriptSite_OnStateChange,
244 MsiActiveScriptSite_OnScriptError,
245 MsiActiveScriptSite_OnEnterScript,
246 MsiActiveScriptSite_OnLeaveScript
249 static HRESULT create_activescriptsite(MsiActiveScriptSite **obj)
251 MsiActiveScriptSite* object;
253 TRACE("(%p)\n", obj);
255 *obj = NULL;
257 object = msi_alloc( sizeof(MsiActiveScriptSite) );
258 if (!object)
259 return E_OUTOFMEMORY;
261 object->IActiveScriptSite_iface.lpVtbl = &activescriptsitevtbl;
262 object->ref = 1;
263 object->installer = NULL;
264 object->session = NULL;
266 *obj = object;
268 return S_OK;
271 static UINT map_return_value(LONG val)
273 switch (val)
275 case 0:
276 case IDOK:
277 case IDIGNORE: return ERROR_SUCCESS;
278 case IDCANCEL: return ERROR_INSTALL_USEREXIT;
279 case IDRETRY: return ERROR_INSTALL_SUSPEND;
280 case IDABORT:
281 default: return ERROR_INSTALL_FAILURE;
286 * Call a script.
288 DWORD call_script(MSIHANDLE hPackage, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action)
290 HRESULT hr;
291 IActiveScript *pActiveScript = NULL;
292 IActiveScriptParse *pActiveScriptParse = NULL;
293 MsiActiveScriptSite *scriptsite;
294 IDispatch *pDispatch = NULL;
295 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
296 DISPID dispid;
297 CLSID clsid;
298 VARIANT var;
299 DWORD ret = ERROR_INSTALL_FAILURE;
301 CoInitialize(NULL);
303 /* Create MsiActiveScriptSite object */
304 hr = create_activescriptsite(&scriptsite);
305 if (hr != S_OK) goto done;
307 /* Create an installer object */
308 hr = create_msiserver(NULL, (void**)&scriptsite->installer);
309 if (hr != S_OK) goto done;
311 /* Create a session object */
312 hr = create_session(hPackage, scriptsite->installer, &scriptsite->session);
313 if (hr != S_OK) goto done;
315 /* Create the scripting engine */
316 type &= msidbCustomActionTypeJScript|msidbCustomActionTypeVBScript;
317 if (type == msidbCustomActionTypeJScript)
318 hr = CLSIDFromProgID(L"JScript", &clsid);
319 else if (type == msidbCustomActionTypeVBScript)
320 hr = CLSIDFromProgID(L"VBScript", &clsid);
321 else {
322 ERR("Unknown script type %d\n", type);
323 goto done;
325 if (FAILED(hr)) {
326 ERR("Could not find CLSID for Windows Script\n");
327 goto done;
329 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IActiveScript, (void **)&pActiveScript);
330 if (FAILED(hr)) {
331 ERR("Could not instantiate class for Windows Script\n");
332 goto done;
335 hr = IActiveScript_QueryInterface(pActiveScript, &IID_IActiveScriptParse, (void **)&pActiveScriptParse);
336 if (FAILED(hr)) goto done;
338 hr = IActiveScript_SetScriptSite(pActiveScript, &scriptsite->IActiveScriptSite_iface);
339 if (FAILED(hr)) goto done;
341 hr = IActiveScriptParse_InitNew(pActiveScriptParse);
342 if (FAILED(hr)) goto done;
344 hr = IActiveScript_AddNamedItem(pActiveScript, L"Session", SCRIPTITEM_GLOBALMEMBERS|SCRIPTITEM_ISVISIBLE);
345 if (FAILED(hr)) goto done;
347 hr = IActiveScriptParse_ParseScriptText(pActiveScriptParse, script, NULL, NULL, NULL, 0, 0, 0L, NULL, NULL);
348 if (FAILED(hr)) goto done;
350 hr = IActiveScript_SetScriptState(pActiveScript, SCRIPTSTATE_CONNECTED);
351 if (FAILED(hr)) goto done;
353 /* Call a function if necessary through the IDispatch interface */
354 if (function && function[0]) {
355 TRACE("Calling function %s\n", debugstr_w(function));
357 hr = IActiveScript_GetScriptDispatch(pActiveScript, NULL, &pDispatch);
358 if (FAILED(hr)) goto done;
360 hr = IDispatch_GetIDsOfNames(pDispatch, &IID_NULL, (WCHAR **)&function, 1,LOCALE_USER_DEFAULT, &dispid);
361 if (FAILED(hr)) goto done;
363 hr = IDispatch_Invoke(pDispatch, dispid, &IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &var, NULL, NULL);
364 if (FAILED(hr)) goto done;
366 hr = VariantChangeType(&var, &var, 0, VT_I4);
367 if (FAILED(hr)) goto done;
369 ret = map_return_value(V_I4(&var));
371 VariantClear(&var);
372 } else {
373 /* If no function to be called, MSI behavior is to succeed */
374 ret = ERROR_SUCCESS;
377 done:
379 if (pDispatch) IDispatch_Release(pDispatch);
380 if (pActiveScript) IActiveScript_Release(pActiveScript);
381 if (pActiveScriptParse) IActiveScriptParse_Release(pActiveScriptParse);
382 if (scriptsite)
384 if (scriptsite->session) IDispatch_Release(scriptsite->session);
385 if (scriptsite->installer) IDispatch_Release(scriptsite->installer);
386 IActiveScriptSite_Release(&scriptsite->IActiveScriptSite_iface);
388 CoUninitialize(); /* must call even if CoInitialize failed */
389 return ret;