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
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "msiserver.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41 #define IActiveScriptParse_Release IActiveScriptParse64_Release
42 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
43 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
47 #define IActiveScriptParse_Release IActiveScriptParse32_Release
48 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
49 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
53 static const WCHAR szJScript[] = { 'J','S','c','r','i','p','t',0};
54 static const WCHAR szVBScript[] = { 'V','B','S','c','r','i','p','t',0};
55 static const WCHAR szSession[] = {'S','e','s','s','i','o','n',0};
58 * MsiActiveScriptSite - Our IActiveScriptSite implementation.
62 IActiveScriptSite lpVtbl;
63 IDispatch *pInstaller;
66 } MsiActiveScriptSite;
68 static const struct IActiveScriptSiteVtbl ASS_Vtbl;
70 static HRESULT create_ActiveScriptSite(IUnknown *pUnkOuter, LPVOID *ppObj)
72 MsiActiveScriptSite* object;
74 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
77 return CLASS_E_NOAGGREGATION;
79 object = msi_alloc_zero( sizeof(MsiActiveScriptSite) );
81 object->lpVtbl.lpVtbl = &ASS_Vtbl;
83 object->pInstaller = NULL;
84 object->pSession = NULL;
94 DWORD call_script(MSIHANDLE hPackage, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action)
97 IActiveScript *pActiveScript = NULL;
98 IActiveScriptParse *pActiveScriptParse = NULL;
99 MsiActiveScriptSite *pActiveScriptSite = NULL;
100 IDispatch *pDispatch = NULL;
101 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
105 DWORD ret = ERROR_INSTALL_FAILURE;
109 /* Create MsiActiveScriptSite object */
110 hr = create_ActiveScriptSite(NULL, (void **)&pActiveScriptSite);
111 if (hr != S_OK) goto done;
113 /* Create an installer object */
114 hr = create_msiserver(NULL, (LPVOID *)&pActiveScriptSite->pInstaller);
115 if (hr != S_OK) goto done;
117 /* Create a session object */
118 hr = create_session(hPackage, pActiveScriptSite->pInstaller, &pActiveScriptSite->pSession);
119 if (hr != S_OK) goto done;
121 /* Create the scripting engine */
122 if ((type & 7) == msidbCustomActionTypeJScript)
123 hr = CLSIDFromProgID(szJScript, &clsid);
124 else if ((type & 7) == msidbCustomActionTypeVBScript)
125 hr = CLSIDFromProgID(szVBScript, &clsid);
127 ERR("Unknown script type %d\n", type);
131 ERR("Could not find CLSID for Windows Script\n");
134 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IActiveScript, (void **)&pActiveScript);
136 ERR("Could not instantiate class for Windows Script\n");
140 hr = IActiveScript_QueryInterface(pActiveScript, &IID_IActiveScriptParse, (void **)&pActiveScriptParse);
141 if (FAILED(hr)) goto done;
143 hr = IActiveScript_SetScriptSite(pActiveScript, (IActiveScriptSite *)pActiveScriptSite);
144 if (FAILED(hr)) goto done;
146 hr = IActiveScriptParse_InitNew(pActiveScriptParse);
147 if (FAILED(hr)) goto done;
149 hr = IActiveScript_AddNamedItem(pActiveScript, szSession, SCRIPTITEM_GLOBALMEMBERS|SCRIPTITEM_ISVISIBLE);
150 if (FAILED(hr)) goto done;
152 hr = IActiveScriptParse_ParseScriptText(pActiveScriptParse, script, NULL, NULL, NULL, 0, 0, 0L, NULL, NULL);
153 if (FAILED(hr)) goto done;
155 hr = IActiveScript_SetScriptState(pActiveScript, SCRIPTSTATE_CONNECTED);
156 if (FAILED(hr)) goto done;
158 /* Call a function if necessary through the IDispatch interface */
159 if (function != NULL && strlenW(function) > 0) {
160 TRACE("Calling function %s\n", debugstr_w(function));
162 hr = IActiveScript_GetScriptDispatch(pActiveScript, NULL, &pDispatch);
163 if (FAILED(hr)) goto done;
165 hr = IDispatch_GetIDsOfNames(pDispatch, &IID_NULL, (WCHAR **)&function, 1,LOCALE_USER_DEFAULT, &dispid);
166 if (FAILED(hr)) goto done;
168 hr = IDispatch_Invoke(pDispatch, dispid, &IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &var, NULL, NULL);
169 if (FAILED(hr)) goto done;
171 /* Check return value, if it's not IDOK we failed */
172 hr = VariantChangeType(&var, &var, 0, VT_I4);
173 if (FAILED(hr)) goto done;
175 if (V_I4(&var) == IDOK)
177 else ret = ERROR_INSTALL_FAILURE;
181 /* If no function to be called, MSI behavior is to succeed */
187 if (pDispatch) IDispatch_Release(pDispatch);
188 if (pActiveScript) IActiveScript_Release(pActiveScript);
189 if (pActiveScriptParse) IActiveScriptParse_Release(pActiveScriptParse);
190 if (pActiveScriptSite)
192 if (pActiveScriptSite->pSession) IDispatch_Release(pActiveScriptSite->pSession);
193 if (pActiveScriptSite->pInstaller) IDispatch_Release(pActiveScriptSite->pInstaller);
194 IActiveScriptSite_Release((IActiveScriptSite *)pActiveScriptSite);
196 CoUninitialize(); /* must call even if CoInitialize failed */
201 * MsiActiveScriptSite
204 /*** IUnknown methods ***/
205 static HRESULT WINAPI MsiActiveScriptSite_QueryInterface(IActiveScriptSite* iface, REFIID riid, void** ppvObject)
207 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
209 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
211 if (IsEqualGUID(riid, &IID_IUnknown) ||
212 IsEqualGUID(riid, &IID_IActiveScriptSite))
214 IActiveScriptSite_AddRef(iface);
219 TRACE("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
221 return E_NOINTERFACE;
224 static ULONG WINAPI MsiActiveScriptSite_AddRef(IActiveScriptSite* iface)
226 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
228 TRACE("(%p/%p)\n", iface, This);
230 return InterlockedIncrement(&This->ref);
233 static ULONG WINAPI MsiActiveScriptSite_Release(IActiveScriptSite* iface)
235 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
236 ULONG ref = InterlockedDecrement(&This->ref);
238 TRACE("(%p/%p)\n", iface, This);
246 /*** IActiveScriptSite methods **/
247 static HRESULT WINAPI MsiActiveScriptSite_GetLCID(IActiveScriptSite* iface, LCID* plcid)
249 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
250 TRACE("(%p/%p)->(%p)\n", This, iface, plcid);
251 return E_NOTIMPL; /* Script will use system-defined locale */
254 static HRESULT WINAPI MsiActiveScriptSite_GetItemInfo(IActiveScriptSite* iface, LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppiunkItem, ITypeInfo** ppti)
256 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
257 TRACE("(%p/%p)->(%p,%d,%p,%p)\n", This, iface, pstrName, dwReturnMask, ppiunkItem, ppti);
259 /* Determine the kind of pointer that is requested, and make sure placeholder is valid */
260 if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
261 if (!ppti) return E_INVALIDARG;
264 if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
265 if (!ppiunkItem) return E_INVALIDARG;
269 /* Are we looking for the session object? */
270 if (!strcmpW(szSession, pstrName)) {
271 if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
272 HRESULT hr = get_typeinfo(Session_tid, ppti);
274 ITypeInfo_AddRef(*ppti);
277 else if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
278 IDispatch_QueryInterface(This->pSession, &IID_IUnknown, (void **)ppiunkItem);
283 return TYPE_E_ELEMENTNOTFOUND;
286 static HRESULT WINAPI MsiActiveScriptSite_GetDocVersionString(IActiveScriptSite* iface, BSTR* pbstrVersion)
288 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
289 TRACE("(%p/%p)->(%p)\n", This, iface, pbstrVersion);
293 static HRESULT WINAPI MsiActiveScriptSite_OnScriptTerminate(IActiveScriptSite* iface, const VARIANT* pvarResult, const EXCEPINFO* pexcepinfo)
295 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
296 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pvarResult, pexcepinfo);
300 static HRESULT WINAPI MsiActiveScriptSite_OnStateChange(IActiveScriptSite* iface, SCRIPTSTATE ssScriptState)
302 switch (ssScriptState) {
303 case SCRIPTSTATE_UNINITIALIZED:
304 TRACE("State: Uninitialized.\n");
307 case SCRIPTSTATE_INITIALIZED:
308 TRACE("State: Initialized.\n");
311 case SCRIPTSTATE_STARTED:
312 TRACE("State: Started.\n");
315 case SCRIPTSTATE_CONNECTED:
316 TRACE("State: Connected.\n");
319 case SCRIPTSTATE_DISCONNECTED:
320 TRACE("State: Disconnected.\n");
323 case SCRIPTSTATE_CLOSED:
324 TRACE("State: Closed.\n");
328 ERR("Unknown State: %d\n", ssScriptState);
335 static HRESULT WINAPI MsiActiveScriptSite_OnScriptError(IActiveScriptSite* iface, IActiveScriptError* pscripterror)
337 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
341 TRACE("(%p/%p)->(%p)\n", This, iface, pscripterror);
343 memset(&exception, 0, sizeof(EXCEPINFO));
344 hr = IActiveScriptError_GetExceptionInfo(pscripterror, &exception);
347 ERR("script error: %s\n", debugstr_w(exception.bstrDescription));
348 SysFreeString(exception.bstrSource);
349 SysFreeString(exception.bstrDescription);
350 SysFreeString(exception.bstrHelpFile);
356 static HRESULT WINAPI MsiActiveScriptSite_OnEnterScript(IActiveScriptSite* iface)
358 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
359 TRACE("(%p/%p)\n", This, iface);
363 static HRESULT WINAPI MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite* iface)
365 MsiActiveScriptSite *This = (MsiActiveScriptSite *)iface;
366 TRACE("(%p/%p)\n", This, iface);
370 static const struct IActiveScriptSiteVtbl ASS_Vtbl =
372 MsiActiveScriptSite_QueryInterface,
373 MsiActiveScriptSite_AddRef,
374 MsiActiveScriptSite_Release,
375 MsiActiveScriptSite_GetLCID,
376 MsiActiveScriptSite_GetItemInfo,
377 MsiActiveScriptSite_GetDocVersionString,
378 MsiActiveScriptSite_OnScriptTerminate,
379 MsiActiveScriptSite_OnStateChange,
380 MsiActiveScriptSite_OnScriptError,
381 MsiActiveScriptSite_OnEnterScript,
382 MsiActiveScriptSite_OnLeaveScript