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 * struct script_site - Our IActiveScriptSite implementation.
57 IActiveScriptSite IActiveScriptSite_iface
;
63 static inline struct script_site
*impl_from_IActiveScriptSite( IActiveScriptSite
*iface
)
65 return CONTAINING_RECORD(iface
, struct script_site
, IActiveScriptSite_iface
);
71 static HRESULT WINAPI
MsiActiveScriptSite_QueryInterface(IActiveScriptSite
* iface
, REFIID riid
, void** obj
)
73 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
75 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), obj
);
77 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
78 IsEqualGUID(riid
, &IID_IActiveScriptSite
))
80 IActiveScriptSite_AddRef(iface
);
90 static ULONG WINAPI
MsiActiveScriptSite_AddRef(IActiveScriptSite
* iface
)
92 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
93 ULONG ref
= InterlockedIncrement(&This
->ref
);
94 TRACE( "(%p)->(%lu)\n", This
, ref
);
98 static ULONG WINAPI
MsiActiveScriptSite_Release(IActiveScriptSite
* iface
)
100 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
101 ULONG ref
= InterlockedDecrement(&This
->ref
);
103 TRACE( "(%p)->(%lu)\n", This
, ref
);
111 static HRESULT WINAPI
MsiActiveScriptSite_GetLCID(IActiveScriptSite
* iface
, LCID
* plcid
)
113 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
114 TRACE("(%p)->(%p)\n", This
, plcid
);
115 return E_NOTIMPL
; /* Script will use system-defined locale */
118 static HRESULT WINAPI
MsiActiveScriptSite_GetItemInfo(IActiveScriptSite
* iface
, LPCOLESTR pstrName
, DWORD dwReturnMask
, IUnknown
** ppiunkItem
, ITypeInfo
** ppti
)
120 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
122 TRACE( "(%p)->(%p, %lu, %p, %p)\n", This
, pstrName
, dwReturnMask
, ppiunkItem
, ppti
);
124 /* Determine the kind of pointer that is requested, and make sure placeholder is valid */
125 if (dwReturnMask
& SCRIPTINFO_ITYPEINFO
) {
126 if (!ppti
) return E_INVALIDARG
;
129 if (dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
130 if (!ppiunkItem
) return E_INVALIDARG
;
134 /* Are we looking for the session object? */
135 if (!wcscmp(L
"Session", pstrName
)) {
136 if (dwReturnMask
& SCRIPTINFO_ITYPEINFO
) {
137 HRESULT hr
= get_typeinfo(Session_tid
, ppti
);
139 ITypeInfo_AddRef(*ppti
);
142 else if (dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
143 IDispatch_QueryInterface(This
->session
, &IID_IUnknown
, (void **)ppiunkItem
);
148 return TYPE_E_ELEMENTNOTFOUND
;
151 static HRESULT WINAPI
MsiActiveScriptSite_GetDocVersionString(IActiveScriptSite
* iface
, BSTR
* pbstrVersion
)
153 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
154 TRACE("(%p)->(%p)\n", This
, pbstrVersion
);
158 static HRESULT WINAPI
MsiActiveScriptSite_OnScriptTerminate(IActiveScriptSite
* iface
, const VARIANT
* pvarResult
, const EXCEPINFO
* pexcepinfo
)
160 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
161 TRACE("(%p)->(%p, %p)\n", This
, pvarResult
, pexcepinfo
);
165 static HRESULT WINAPI
MsiActiveScriptSite_OnStateChange(IActiveScriptSite
* iface
, SCRIPTSTATE ssScriptState
)
167 switch (ssScriptState
) {
168 case SCRIPTSTATE_UNINITIALIZED
:
169 TRACE("State: Uninitialized.\n");
172 case SCRIPTSTATE_INITIALIZED
:
173 TRACE("State: Initialized.\n");
176 case SCRIPTSTATE_STARTED
:
177 TRACE("State: Started.\n");
180 case SCRIPTSTATE_CONNECTED
:
181 TRACE("State: Connected.\n");
184 case SCRIPTSTATE_DISCONNECTED
:
185 TRACE("State: Disconnected.\n");
188 case SCRIPTSTATE_CLOSED
:
189 TRACE("State: Closed.\n");
193 ERR("Unknown State: %d\n", ssScriptState
);
200 static HRESULT WINAPI
MsiActiveScriptSite_OnScriptError(IActiveScriptSite
* iface
, IActiveScriptError
* pscripterror
)
202 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
206 TRACE("(%p)->(%p)\n", This
, pscripterror
);
208 memset(&exception
, 0, sizeof(EXCEPINFO
));
209 hr
= IActiveScriptError_GetExceptionInfo(pscripterror
, &exception
);
212 ERR("script error: %s\n", debugstr_w(exception
.bstrDescription
));
213 SysFreeString(exception
.bstrSource
);
214 SysFreeString(exception
.bstrDescription
);
215 SysFreeString(exception
.bstrHelpFile
);
221 static HRESULT WINAPI
MsiActiveScriptSite_OnEnterScript(IActiveScriptSite
* iface
)
223 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
224 TRACE("(%p)\n", This
);
228 static HRESULT WINAPI
MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite
* iface
)
230 struct script_site
*This
= impl_from_IActiveScriptSite(iface
);
231 TRACE("(%p)\n", This
);
235 static const struct IActiveScriptSiteVtbl activescriptsitevtbl
=
237 MsiActiveScriptSite_QueryInterface
,
238 MsiActiveScriptSite_AddRef
,
239 MsiActiveScriptSite_Release
,
240 MsiActiveScriptSite_GetLCID
,
241 MsiActiveScriptSite_GetItemInfo
,
242 MsiActiveScriptSite_GetDocVersionString
,
243 MsiActiveScriptSite_OnScriptTerminate
,
244 MsiActiveScriptSite_OnStateChange
,
245 MsiActiveScriptSite_OnScriptError
,
246 MsiActiveScriptSite_OnEnterScript
,
247 MsiActiveScriptSite_OnLeaveScript
250 static HRESULT
create_activescriptsite(struct script_site
**obj
)
252 struct script_site
*object
;
254 TRACE("(%p)\n", obj
);
258 object
= malloc(sizeof(*object
));
260 return E_OUTOFMEMORY
;
262 object
->IActiveScriptSite_iface
.lpVtbl
= &activescriptsitevtbl
;
264 object
->installer
= NULL
;
265 object
->session
= NULL
;
272 static UINT
map_return_value(LONG val
)
278 case IDIGNORE
: return ERROR_SUCCESS
;
279 case IDCANCEL
: return ERROR_INSTALL_USEREXIT
;
280 case IDRETRY
: return ERROR_INSTALL_SUSPEND
;
282 default: return ERROR_INSTALL_FAILURE
;
289 DWORD
call_script(MSIHANDLE hPackage
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
292 IActiveScript
*pActiveScript
= NULL
;
293 IActiveScriptParse
*pActiveScriptParse
= NULL
;
294 struct script_site
*scriptsite
;
295 IDispatch
*pDispatch
= NULL
;
296 DISPPARAMS dispparamsNoArgs
= {NULL
, NULL
, 0, 0};
300 DWORD ret
= ERROR_INSTALL_FAILURE
;
304 /* Create MsiActiveScriptSite object */
305 hr
= create_activescriptsite(&scriptsite
);
306 if (hr
!= S_OK
) goto done
;
308 /* Create an installer object */
309 hr
= create_msiserver(NULL
, (void**)&scriptsite
->installer
);
310 if (hr
!= S_OK
) goto done
;
312 /* Create a session object */
313 hr
= create_session(hPackage
, scriptsite
->installer
, &scriptsite
->session
);
314 if (hr
!= S_OK
) goto done
;
316 /* Create the scripting engine */
317 type
&= msidbCustomActionTypeJScript
|msidbCustomActionTypeVBScript
;
318 if (type
== msidbCustomActionTypeJScript
)
319 hr
= CLSIDFromProgID(L
"JScript", &clsid
);
320 else if (type
== msidbCustomActionTypeVBScript
)
321 hr
= CLSIDFromProgID(L
"VBScript", &clsid
);
323 ERR("Unknown script type %d\n", type
);
327 ERR("Could not find CLSID for Windows Script\n");
330 hr
= CoCreateInstance(&clsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IActiveScript
, (void **)&pActiveScript
);
332 ERR("Could not instantiate class for Windows Script\n");
336 hr
= IActiveScript_QueryInterface(pActiveScript
, &IID_IActiveScriptParse
, (void **)&pActiveScriptParse
);
337 if (FAILED(hr
)) goto done
;
339 hr
= IActiveScript_SetScriptSite(pActiveScript
, &scriptsite
->IActiveScriptSite_iface
);
340 if (FAILED(hr
)) goto done
;
342 hr
= IActiveScriptParse_InitNew(pActiveScriptParse
);
343 if (FAILED(hr
)) goto done
;
345 hr
= IActiveScript_AddNamedItem(pActiveScript
, L
"Session", SCRIPTITEM_GLOBALMEMBERS
|SCRIPTITEM_ISVISIBLE
);
346 if (FAILED(hr
)) goto done
;
348 hr
= IActiveScriptParse_ParseScriptText(pActiveScriptParse
, script
, NULL
, NULL
, NULL
, 0, 0, 0L, NULL
, NULL
);
349 if (FAILED(hr
)) goto done
;
351 hr
= IActiveScript_SetScriptState(pActiveScript
, SCRIPTSTATE_CONNECTED
);
352 if (FAILED(hr
)) goto done
;
354 /* Call a function if necessary through the IDispatch interface */
355 if (function
&& function
[0]) {
356 TRACE("Calling function %s\n", debugstr_w(function
));
358 hr
= IActiveScript_GetScriptDispatch(pActiveScript
, NULL
, &pDispatch
);
359 if (FAILED(hr
)) goto done
;
361 hr
= IDispatch_GetIDsOfNames(pDispatch
, &IID_NULL
, (WCHAR
**)&function
, 1,LOCALE_USER_DEFAULT
, &dispid
);
362 if (FAILED(hr
)) goto done
;
364 hr
= IDispatch_Invoke(pDispatch
, dispid
, &IID_NULL
, LOCALE_USER_DEFAULT
, DISPATCH_METHOD
, &dispparamsNoArgs
, &var
, NULL
, NULL
);
365 if (FAILED(hr
)) goto done
;
367 hr
= VariantChangeType(&var
, &var
, 0, VT_I4
);
368 if (FAILED(hr
)) goto done
;
370 ret
= map_return_value(V_I4(&var
));
374 /* If no function to be called, MSI behavior is to succeed */
380 if (pDispatch
) IDispatch_Release(pDispatch
);
381 if (pActiveScript
) IActiveScript_Release(pActiveScript
);
382 if (pActiveScriptParse
) IActiveScriptParse_Release(pActiveScriptParse
);
385 if (scriptsite
->session
) IDispatch_Release(scriptsite
->session
);
386 if (scriptsite
->installer
) IDispatch_Release(scriptsite
->installer
);
387 IActiveScriptSite_Release(&scriptsite
->IActiveScriptSite_iface
);
389 CoUninitialize(); /* must call even if CoInitialize failed */