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
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.
60 IActiveScriptSite IActiveScriptSite_iface
;
64 } MsiActiveScriptSite
;
66 static inline MsiActiveScriptSite
*impl_from_IActiveScriptSite( IActiveScriptSite
*iface
)
68 return CONTAINING_RECORD(iface
, MsiActiveScriptSite
, IActiveScriptSite_iface
);
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
);
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
);
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
);
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
;
132 if (dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
133 if (!ppiunkItem
) return E_INVALIDARG
;
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
);
142 ITypeInfo_AddRef(*ppti
);
145 else if (dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
146 IDispatch_QueryInterface(This
->session
, &IID_IUnknown
, (void **)ppiunkItem
);
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
);
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
);
168 static HRESULT WINAPI
MsiActiveScriptSite_OnStateChange(IActiveScriptSite
* iface
, SCRIPTSTATE ssScriptState
)
170 switch (ssScriptState
) {
171 case SCRIPTSTATE_UNINITIALIZED
:
172 TRACE("State: Uninitialized.\n");
175 case SCRIPTSTATE_INITIALIZED
:
176 TRACE("State: Initialized.\n");
179 case SCRIPTSTATE_STARTED
:
180 TRACE("State: Started.\n");
183 case SCRIPTSTATE_CONNECTED
:
184 TRACE("State: Connected.\n");
187 case SCRIPTSTATE_DISCONNECTED
:
188 TRACE("State: Disconnected.\n");
191 case SCRIPTSTATE_CLOSED
:
192 TRACE("State: Closed.\n");
196 ERR("Unknown State: %d\n", ssScriptState
);
203 static HRESULT WINAPI
MsiActiveScriptSite_OnScriptError(IActiveScriptSite
* iface
, IActiveScriptError
* pscripterror
)
205 MsiActiveScriptSite
*This
= impl_from_IActiveScriptSite(iface
);
209 TRACE("(%p)->(%p)\n", This
, pscripterror
);
211 memset(&exception
, 0, sizeof(EXCEPINFO
));
212 hr
= IActiveScriptError_GetExceptionInfo(pscripterror
, &exception
);
215 ERR("script error: %s\n", debugstr_w(exception
.bstrDescription
));
216 SysFreeString(exception
.bstrSource
);
217 SysFreeString(exception
.bstrDescription
);
218 SysFreeString(exception
.bstrHelpFile
);
224 static HRESULT WINAPI
MsiActiveScriptSite_OnEnterScript(IActiveScriptSite
* iface
)
226 MsiActiveScriptSite
*This
= impl_from_IActiveScriptSite(iface
);
227 TRACE("(%p)\n", This
);
231 static HRESULT WINAPI
MsiActiveScriptSite_OnLeaveScript(IActiveScriptSite
* iface
)
233 MsiActiveScriptSite
*This
= impl_from_IActiveScriptSite(iface
);
234 TRACE("(%p)\n", This
);
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
);
261 object
= msi_alloc( sizeof(MsiActiveScriptSite
) );
263 return E_OUTOFMEMORY
;
265 object
->IActiveScriptSite_iface
.lpVtbl
= &activescriptsitevtbl
;
267 object
->installer
= NULL
;
268 object
->session
= NULL
;
275 static UINT
map_return_value(LONG val
)
281 case IDIGNORE
: return ERROR_SUCCESS
;
282 case IDCANCEL
: return ERROR_INSTALL_USEREXIT
;
283 case IDRETRY
: return ERROR_INSTALL_SUSPEND
;
285 default: return ERROR_INSTALL_FAILURE
;
292 DWORD
call_script(MSIHANDLE hPackage
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
295 IActiveScript
*pActiveScript
= NULL
;
296 IActiveScriptParse
*pActiveScriptParse
= NULL
;
297 MsiActiveScriptSite
*scriptsite
;
298 IDispatch
*pDispatch
= NULL
;
299 DISPPARAMS dispparamsNoArgs
= {NULL
, NULL
, 0, 0};
303 DWORD ret
= ERROR_INSTALL_FAILURE
;
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
);
326 ERR("Unknown script type %d\n", type
);
330 ERR("Could not find CLSID for Windows Script\n");
333 hr
= CoCreateInstance(&clsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IActiveScript
, (void **)&pActiveScript
);
335 ERR("Could not instantiate class for Windows Script\n");
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
));
377 /* If no function to be called, MSI behavior is to succeed */
383 if (pDispatch
) IDispatch_Release(pDispatch
);
384 if (pActiveScript
) IActiveScript_Release(pActiveScript
);
385 if (pActiveScriptParse
) IActiveScriptParse_Release(pActiveScriptParse
);
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 */