ntdll: Load .so builtin modules without using libwine.
[wine/zf.git] / dlls / msi / script.c
blob67ad8b51c522d8599f81bd0b5741b7dae5ef3a3c
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
52 static const WCHAR szJScript[] = { 'J','S','c','r','i','p','t',0};
53 static const WCHAR szVBScript[] = { 'V','B','S','c','r','i','p','t',0};
54 static const WCHAR szSession[] = {'S','e','s','s','i','o','n',0};
57 * MsiActiveScriptSite - Our IActiveScriptSite implementation.
59 typedef struct {
60 IActiveScriptSite IActiveScriptSite_iface;
61 IDispatch *installer;
62 IDispatch *session;
63 LONG ref;
64 } MsiActiveScriptSite;
66 static inline MsiActiveScriptSite *impl_from_IActiveScriptSite( IActiveScriptSite *iface )
68 return CONTAINING_RECORD(iface, MsiActiveScriptSite, IActiveScriptSite_iface);
72 * MsiActiveScriptSite
74 static HRESULT WINAPI MsiActiveScriptSite_QueryInterface(IActiveScriptSite* iface, REFIID riid, void** obj)
76 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
78 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
80 if (IsEqualGUID(riid, &IID_IUnknown) ||
81 IsEqualGUID(riid, &IID_IActiveScriptSite))
83 IActiveScriptSite_AddRef(iface);
84 *obj = iface;
85 return S_OK;
88 *obj = NULL;
90 return E_NOINTERFACE;
93 static ULONG WINAPI MsiActiveScriptSite_AddRef(IActiveScriptSite* iface)
95 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
96 ULONG ref = InterlockedIncrement(&This->ref);
97 TRACE("(%p)->(%d)\n", This, ref);
98 return ref;
101 static ULONG WINAPI MsiActiveScriptSite_Release(IActiveScriptSite* iface)
103 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
104 ULONG ref = InterlockedDecrement(&This->ref);
106 TRACE("(%p)->(%d)\n", This, ref);
108 if (!ref)
109 msi_free(This);
111 return ref;
114 static HRESULT WINAPI MsiActiveScriptSite_GetLCID(IActiveScriptSite* iface, LCID* plcid)
116 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
117 TRACE("(%p)->(%p)\n", This, plcid);
118 return E_NOTIMPL; /* Script will use system-defined locale */
121 static HRESULT WINAPI MsiActiveScriptSite_GetItemInfo(IActiveScriptSite* iface, LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppiunkItem, ITypeInfo** ppti)
123 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
125 TRACE("(%p)->(%p, %d, %p, %p)\n", This, pstrName, dwReturnMask, ppiunkItem, ppti);
127 /* Determine the kind of pointer that is requested, and make sure placeholder is valid */
128 if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
129 if (!ppti) return E_INVALIDARG;
130 *ppti = NULL;
132 if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
133 if (!ppiunkItem) return E_INVALIDARG;
134 *ppiunkItem = NULL;
137 /* Are we looking for the session object? */
138 if (!wcscmp(szSession, pstrName)) {
139 if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
140 HRESULT hr = get_typeinfo(Session_tid, ppti);
141 if (SUCCEEDED(hr))
142 ITypeInfo_AddRef(*ppti);
143 return hr;
145 else if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
146 IDispatch_QueryInterface(This->session, &IID_IUnknown, (void **)ppiunkItem);
147 return S_OK;
151 return TYPE_E_ELEMENTNOTFOUND;
154 static HRESULT WINAPI MsiActiveScriptSite_GetDocVersionString(IActiveScriptSite* iface, BSTR* pbstrVersion)
156 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
157 TRACE("(%p)->(%p)\n", This, pbstrVersion);
158 return E_NOTIMPL;
161 static HRESULT WINAPI MsiActiveScriptSite_OnScriptTerminate(IActiveScriptSite* iface, const VARIANT* pvarResult, const EXCEPINFO* pexcepinfo)
163 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
164 TRACE("(%p)->(%p, %p)\n", This, pvarResult, pexcepinfo);
165 return S_OK;
168 static HRESULT WINAPI MsiActiveScriptSite_OnStateChange(IActiveScriptSite* iface, SCRIPTSTATE ssScriptState)
170 switch (ssScriptState) {
171 case SCRIPTSTATE_UNINITIALIZED:
172 TRACE("State: Uninitialized.\n");
173 break;
175 case SCRIPTSTATE_INITIALIZED:
176 TRACE("State: Initialized.\n");
177 break;
179 case SCRIPTSTATE_STARTED:
180 TRACE("State: Started.\n");
181 break;
183 case SCRIPTSTATE_CONNECTED:
184 TRACE("State: Connected.\n");
185 break;
187 case SCRIPTSTATE_DISCONNECTED:
188 TRACE("State: Disconnected.\n");
189 break;
191 case SCRIPTSTATE_CLOSED:
192 TRACE("State: Closed.\n");
193 break;
195 default:
196 ERR("Unknown State: %d\n", ssScriptState);
197 break;
200 return S_OK;
203 static HRESULT WINAPI MsiActiveScriptSite_OnScriptError(IActiveScriptSite* iface, IActiveScriptError* pscripterror)
205 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
206 EXCEPINFO exception;
207 HRESULT hr;
209 TRACE("(%p)->(%p)\n", This, pscripterror);
211 memset(&exception, 0, sizeof(EXCEPINFO));
212 hr = IActiveScriptError_GetExceptionInfo(pscripterror, &exception);
213 if (SUCCEEDED(hr))
215 ERR("script error: %s\n", debugstr_w(exception.bstrDescription));
216 SysFreeString(exception.bstrSource);
217 SysFreeString(exception.bstrDescription);
218 SysFreeString(exception.bstrHelpFile);
221 return S_OK;
224 static HRESULT WINAPI MsiActiveScriptSite_OnEnterScript(IActiveScriptSite* iface)
226 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
227 TRACE("(%p)\n", This);
228 return S_OK;
231 static HRESULT WINAPI MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite* iface)
233 MsiActiveScriptSite *This = impl_from_IActiveScriptSite(iface);
234 TRACE("(%p)\n", This);
235 return S_OK;
238 static const struct IActiveScriptSiteVtbl activescriptsitevtbl =
240 MsiActiveScriptSite_QueryInterface,
241 MsiActiveScriptSite_AddRef,
242 MsiActiveScriptSite_Release,
243 MsiActiveScriptSite_GetLCID,
244 MsiActiveScriptSite_GetItemInfo,
245 MsiActiveScriptSite_GetDocVersionString,
246 MsiActiveScriptSite_OnScriptTerminate,
247 MsiActiveScriptSite_OnStateChange,
248 MsiActiveScriptSite_OnScriptError,
249 MsiActiveScriptSite_OnEnterScript,
250 MsiActiveScriptSite_OnLeaveScript
253 static HRESULT create_activescriptsite(MsiActiveScriptSite **obj)
255 MsiActiveScriptSite* object;
257 TRACE("(%p)\n", obj);
259 *obj = NULL;
261 object = msi_alloc( sizeof(MsiActiveScriptSite) );
262 if (!object)
263 return E_OUTOFMEMORY;
265 object->IActiveScriptSite_iface.lpVtbl = &activescriptsitevtbl;
266 object->ref = 1;
267 object->installer = NULL;
268 object->session = NULL;
270 *obj = object;
272 return S_OK;
275 static UINT map_return_value(LONG val)
277 switch (val)
279 case 0:
280 case IDOK:
281 case IDIGNORE: return ERROR_SUCCESS;
282 case IDCANCEL: return ERROR_INSTALL_USEREXIT;
283 case IDRETRY: return ERROR_INSTALL_SUSPEND;
284 case IDABORT:
285 default: return ERROR_INSTALL_FAILURE;
290 * Call a script.
292 DWORD call_script(MSIHANDLE hPackage, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action)
294 HRESULT hr;
295 IActiveScript *pActiveScript = NULL;
296 IActiveScriptParse *pActiveScriptParse = NULL;
297 MsiActiveScriptSite *scriptsite;
298 IDispatch *pDispatch = NULL;
299 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
300 DISPID dispid;
301 CLSID clsid;
302 VARIANT var;
303 DWORD ret = ERROR_INSTALL_FAILURE;
305 CoInitialize(NULL);
307 /* Create MsiActiveScriptSite object */
308 hr = create_activescriptsite(&scriptsite);
309 if (hr != S_OK) goto done;
311 /* Create an installer object */
312 hr = create_msiserver(NULL, (void**)&scriptsite->installer);
313 if (hr != S_OK) goto done;
315 /* Create a session object */
316 hr = create_session(hPackage, scriptsite->installer, &scriptsite->session);
317 if (hr != S_OK) goto done;
319 /* Create the scripting engine */
320 type &= msidbCustomActionTypeJScript|msidbCustomActionTypeVBScript;
321 if (type == msidbCustomActionTypeJScript)
322 hr = CLSIDFromProgID(szJScript, &clsid);
323 else if (type == msidbCustomActionTypeVBScript)
324 hr = CLSIDFromProgID(szVBScript, &clsid);
325 else {
326 ERR("Unknown script type %d\n", type);
327 goto done;
329 if (FAILED(hr)) {
330 ERR("Could not find CLSID for Windows Script\n");
331 goto done;
333 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IActiveScript, (void **)&pActiveScript);
334 if (FAILED(hr)) {
335 ERR("Could not instantiate class for Windows Script\n");
336 goto done;
339 hr = IActiveScript_QueryInterface(pActiveScript, &IID_IActiveScriptParse, (void **)&pActiveScriptParse);
340 if (FAILED(hr)) goto done;
342 hr = IActiveScript_SetScriptSite(pActiveScript, &scriptsite->IActiveScriptSite_iface);
343 if (FAILED(hr)) goto done;
345 hr = IActiveScriptParse_InitNew(pActiveScriptParse);
346 if (FAILED(hr)) goto done;
348 hr = IActiveScript_AddNamedItem(pActiveScript, szSession, SCRIPTITEM_GLOBALMEMBERS|SCRIPTITEM_ISVISIBLE);
349 if (FAILED(hr)) goto done;
351 hr = IActiveScriptParse_ParseScriptText(pActiveScriptParse, script, NULL, NULL, NULL, 0, 0, 0L, NULL, NULL);
352 if (FAILED(hr)) goto done;
354 hr = IActiveScript_SetScriptState(pActiveScript, SCRIPTSTATE_CONNECTED);
355 if (FAILED(hr)) goto done;
357 /* Call a function if necessary through the IDispatch interface */
358 if (function && function[0]) {
359 TRACE("Calling function %s\n", debugstr_w(function));
361 hr = IActiveScript_GetScriptDispatch(pActiveScript, NULL, &pDispatch);
362 if (FAILED(hr)) goto done;
364 hr = IDispatch_GetIDsOfNames(pDispatch, &IID_NULL, (WCHAR **)&function, 1,LOCALE_USER_DEFAULT, &dispid);
365 if (FAILED(hr)) goto done;
367 hr = IDispatch_Invoke(pDispatch, dispid, &IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &var, NULL, NULL);
368 if (FAILED(hr)) goto done;
370 hr = VariantChangeType(&var, &var, 0, VT_I4);
371 if (FAILED(hr)) goto done;
373 ret = map_return_value(V_I4(&var));
375 VariantClear(&var);
376 } else {
377 /* If no function to be called, MSI behavior is to succeed */
378 ret = ERROR_SUCCESS;
381 done:
383 if (pDispatch) IDispatch_Release(pDispatch);
384 if (pActiveScript) IActiveScript_Release(pActiveScript);
385 if (pActiveScriptParse) IActiveScriptParse_Release(pActiveScriptParse);
386 if (scriptsite)
388 if (scriptsite->session) IDispatch_Release(scriptsite->session);
389 if (scriptsite->installer) IDispatch_Release(scriptsite->installer);
390 IActiveScriptSite_Release(&scriptsite->IActiveScriptSite_iface);
392 CoUninitialize(); /* must call even if CoInitialize failed */
393 return ret;