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"
34 #include "msiserver.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
40 #define IActiveScriptParse_Release IActiveScriptParse64_Release
41 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
42 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
46 #define IActiveScriptParse_Release IActiveScriptParse32_Release
47 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
48 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
53 * MsiActiveScriptSite - Our IActiveScriptSite implementation.
56 IActiveScriptSite IActiveScriptSite_iface
;
60 } MsiActiveScriptSite
;
62 static inline MsiActiveScriptSite
*impl_from_IActiveScriptSite( IActiveScriptSite
*iface
)
64 return CONTAINING_RECORD(iface
, MsiActiveScriptSite
, IActiveScriptSite_iface
);
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
);
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
);
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
);
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
;
128 if (dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
129 if (!ppiunkItem
) return E_INVALIDARG
;
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
);
138 ITypeInfo_AddRef(*ppti
);
141 else if (dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
142 IDispatch_QueryInterface(This
->session
, &IID_IUnknown
, (void **)ppiunkItem
);
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
);
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
);
164 static HRESULT WINAPI
MsiActiveScriptSite_OnStateChange(IActiveScriptSite
* iface
, SCRIPTSTATE ssScriptState
)
166 switch (ssScriptState
) {
167 case SCRIPTSTATE_UNINITIALIZED
:
168 TRACE("State: Uninitialized.\n");
171 case SCRIPTSTATE_INITIALIZED
:
172 TRACE("State: Initialized.\n");
175 case SCRIPTSTATE_STARTED
:
176 TRACE("State: Started.\n");
179 case SCRIPTSTATE_CONNECTED
:
180 TRACE("State: Connected.\n");
183 case SCRIPTSTATE_DISCONNECTED
:
184 TRACE("State: Disconnected.\n");
187 case SCRIPTSTATE_CLOSED
:
188 TRACE("State: Closed.\n");
192 ERR("Unknown State: %d\n", ssScriptState
);
199 static HRESULT WINAPI
MsiActiveScriptSite_OnScriptError(IActiveScriptSite
* iface
, IActiveScriptError
* pscripterror
)
201 MsiActiveScriptSite
*This
= impl_from_IActiveScriptSite(iface
);
205 TRACE("(%p)->(%p)\n", This
, pscripterror
);
207 memset(&exception
, 0, sizeof(EXCEPINFO
));
208 hr
= IActiveScriptError_GetExceptionInfo(pscripterror
, &exception
);
211 ERR("script error: %s\n", debugstr_w(exception
.bstrDescription
));
212 SysFreeString(exception
.bstrSource
);
213 SysFreeString(exception
.bstrDescription
);
214 SysFreeString(exception
.bstrHelpFile
);
220 static HRESULT WINAPI
MsiActiveScriptSite_OnEnterScript(IActiveScriptSite
* iface
)
222 MsiActiveScriptSite
*This
= impl_from_IActiveScriptSite(iface
);
223 TRACE("(%p)\n", This
);
227 static HRESULT WINAPI
MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite
* iface
)
229 MsiActiveScriptSite
*This
= impl_from_IActiveScriptSite(iface
);
230 TRACE("(%p)\n", This
);
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
);
257 object
= msi_alloc( sizeof(MsiActiveScriptSite
) );
259 return E_OUTOFMEMORY
;
261 object
->IActiveScriptSite_iface
.lpVtbl
= &activescriptsitevtbl
;
263 object
->installer
= NULL
;
264 object
->session
= NULL
;
271 static UINT
map_return_value(LONG val
)
277 case IDIGNORE
: return ERROR_SUCCESS
;
278 case IDCANCEL
: return ERROR_INSTALL_USEREXIT
;
279 case IDRETRY
: return ERROR_INSTALL_SUSPEND
;
281 default: return ERROR_INSTALL_FAILURE
;
288 DWORD
call_script(MSIHANDLE hPackage
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
291 IActiveScript
*pActiveScript
= NULL
;
292 IActiveScriptParse
*pActiveScriptParse
= NULL
;
293 MsiActiveScriptSite
*scriptsite
;
294 IDispatch
*pDispatch
= NULL
;
295 DISPPARAMS dispparamsNoArgs
= {NULL
, NULL
, 0, 0};
299 DWORD ret
= ERROR_INSTALL_FAILURE
;
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
);
322 ERR("Unknown script type %d\n", type
);
326 ERR("Could not find CLSID for Windows Script\n");
329 hr
= CoCreateInstance(&clsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IActiveScript
, (void **)&pActiveScript
);
331 ERR("Could not instantiate class for Windows Script\n");
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
));
373 /* If no function to be called, MSI behavior is to succeed */
379 if (pDispatch
) IDispatch_Release(pDispatch
);
380 if (pActiveScript
) IActiveScript_Release(pActiveScript
);
381 if (pActiveScriptParse
) IActiveScriptParse_Release(pActiveScriptParse
);
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 */