2 * Implementation of OLE Automation 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"
36 #include "msiserver_dispids.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41 * AutomationObject - "base" class for all automation objects. For each interface, we implement Invoke function
42 * called from AutomationObject::Invoke, and pass this function to create_automation_object.
45 typedef interface AutomationObject AutomationObject;
47 interface AutomationObject {
49 * VTables - We provide IDispatch, IProvideClassInfo, IProvideClassInfo2, IProvideMultipleClassInfo
51 const IDispatchVtbl *lpVtbl;
52 const IProvideMultipleClassInfoVtbl *lpvtblIProvideMultipleClassInfo;
54 /* Object reference count */
57 /* Clsid for this class and it's appropriate ITypeInfo object */
61 /* The MSI handle of the current object */
64 /* A function that is called from AutomationObject::Invoke, specific to this type of object. */
65 HRESULT (STDMETHODCALLTYPE *funcInvoke)(
66 AutomationObject* This,
71 DISPPARAMS* pDispParams,
73 EXCEPINFO* pExcepInfo,
78 static const struct IDispatchVtbl AutomationObject_Vtbl;
79 static const struct IProvideMultipleClassInfoVtbl AutomationObject_IProvideMultipleClassInfo_Vtbl;
81 /* Load type info so we don't have to process GetIDsOfNames */
82 HRESULT load_type_info(IDispatch *iface, ITypeInfo **pptinfo, REFIID clsid, LCID lcid)
85 LPTYPELIB pLib = NULL;
86 LPTYPEINFO pInfo = NULL;
87 static const WCHAR szMsiServer[] = {'m','s','i','s','e','r','v','e','r','.','t','l','b'};
89 TRACE("(%p)->(%s,%d)\n", iface, debugstr_guid(clsid), lcid);
91 /* Load registered type library */
92 hr = LoadRegTypeLib(&LIBID_WindowsInstaller, 1, 0, lcid, &pLib);
94 hr = LoadTypeLib(szMsiServer, &pLib);
96 ERR("Could not load msiserver.tlb\n");
101 /* Get type information for object */
102 hr = ITypeLib_GetTypeInfoOfGuid(pLib, clsid, &pInfo);
103 ITypeLib_Release(pLib);
105 ERR("Could not load ITypeInfo for %s\n", debugstr_guid(clsid));
112 /* Create the automation object, placing the result in the pointer ppObj. The automation object is created
113 * with the appropriate clsid and invocation function. */
114 HRESULT create_automation_object(MSIHANDLE msiHandle, IUnknown *pUnkOuter, LPVOID *ppObj, REFIID clsid,
115 HRESULT (STDMETHODCALLTYPE *funcInvoke)(AutomationObject*,DISPID,REFIID,LCID,WORD,DISPPARAMS*,
116 VARIANT*,EXCEPINFO*,UINT*))
118 AutomationObject *object;
121 TRACE("(%ld,%p,%p,%s,%p)\n", (unsigned long)msiHandle, pUnkOuter, ppObj, debugstr_guid(clsid), funcInvoke);
124 return CLASS_E_NOAGGREGATION;
126 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AutomationObject));
128 /* Set all the VTable references */
129 object->lpVtbl = &AutomationObject_Vtbl;
130 object->lpvtblIProvideMultipleClassInfo = &AutomationObject_IProvideMultipleClassInfo_Vtbl;
133 /* Store data that was passed */
134 object->msiHandle = msiHandle;
135 object->clsid = (LPCLSID)clsid;
136 object->funcInvoke = funcInvoke;
138 /* Load our TypeInfo so we don't have to process GetIDsOfNames */
139 object->iTypeInfo = NULL;
140 hr = load_type_info((IDispatch *)object, &object->iTypeInfo, clsid, 0x0);
142 HeapFree(GetProcessHeap(), 0, object);
151 /* Macros to get pointer to AutomationObject from the other VTables. */
152 static inline AutomationObject *obj_from_IProvideMultipleClassInfo( IProvideMultipleClassInfo *iface )
154 return (AutomationObject *)((char*)iface - FIELD_OFFSET(AutomationObject, lpvtblIProvideMultipleClassInfo));
158 * AutomationObject methods
161 /*** IUnknown methods ***/
162 static HRESULT WINAPI AutomationObject_QueryInterface(IDispatch* iface, REFIID riid, void** ppvObject)
164 AutomationObject *This = (AutomationObject *)iface;
166 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
168 if (ppvObject == NULL)
173 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IDispatch) || IsEqualGUID(riid, This->clsid))
175 else if (IsEqualGUID(riid, &IID_IProvideClassInfo) ||
176 IsEqualGUID(riid, &IID_IProvideClassInfo2) ||
177 IsEqualGUID(riid, &IID_IProvideMultipleClassInfo))
178 *ppvObject = &This->lpvtblIProvideMultipleClassInfo;
181 TRACE("() : asking for unsupported interface %s\n",debugstr_guid(riid));
182 return E_NOINTERFACE;
186 * Query Interface always increases the reference count by one when it is
189 IClassFactory_AddRef(iface);
194 static ULONG WINAPI AutomationObject_AddRef(IDispatch* iface)
196 AutomationObject *This = (AutomationObject *)iface;
198 TRACE("(%p/%p)\n", iface, This);
200 return InterlockedIncrement(&This->ref);
203 static ULONG WINAPI AutomationObject_Release(IDispatch* iface)
205 AutomationObject *This = (AutomationObject *)iface;
206 ULONG ref = InterlockedDecrement(&This->ref);
208 TRACE("(%p/%p)\n", iface, This);
212 MsiCloseHandle(This->msiHandle);
213 HeapFree(GetProcessHeap(), 0, This);
219 /*** IDispatch methods ***/
220 static HRESULT WINAPI AutomationObject_GetTypeInfoCount(
224 AutomationObject *This = (AutomationObject *)iface;
226 TRACE("(%p/%p)->(%p)\n", iface, This, pctinfo);
231 static HRESULT WINAPI AutomationObject_GetTypeInfo(
237 AutomationObject *This = (AutomationObject *)iface;
238 TRACE("(%p/%p)->(%d,%d,%p)\n", iface, This, iTInfo, lcid, ppTInfo);
240 ITypeInfo_AddRef(This->iTypeInfo);
241 *ppTInfo = This->iTypeInfo;
245 static HRESULT WINAPI AutomationObject_GetIDsOfNames(
253 AutomationObject *This = (AutomationObject *)iface;
254 TRACE("(%p/%p)->(%p,%p,%d,%d,%p)\n", iface, This, riid, rgszNames, cNames, lcid, rgDispId);
256 if (!IsEqualGUID(riid, &IID_NULL)) return E_INVALIDARG;
257 return ITypeInfo_GetIDsOfNames(This->iTypeInfo, rgszNames, cNames, rgDispId);
260 /* Maximum number of allowed function parameters+1 */
261 #define MAX_FUNC_PARAMS 20
263 /* Some error checking is done here to simplify individual object function invocation */
264 static HRESULT WINAPI AutomationObject_Invoke(
270 DISPPARAMS* pDispParams,
272 EXCEPINFO* pExcepInfo,
275 AutomationObject *This = (AutomationObject *)iface;
277 unsigned int uArgErr;
278 VARIANT varResultDummy;
279 BSTR bstrName = NULL;
281 TRACE("(%p/%p)->(%d,%p,%d,%d,%p,%p,%p,%p)\n", iface, This, dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
283 if (!IsEqualIID(riid, &IID_NULL))
285 ERR("riid was %s instead of IID_NULL\n", debugstr_guid(riid));
286 return DISP_E_UNKNOWNNAME;
289 if (wFlags & DISPATCH_PROPERTYGET && !pVarResult)
291 ERR("NULL pVarResult not allowed when DISPATCH_PROPERTYGET specified\n");
292 return DISP_E_PARAMNOTOPTIONAL;
295 /* This simplifies our individual object invocation functions */
296 if (puArgErr == NULL) puArgErr = &uArgErr;
297 if (pVarResult == NULL) pVarResult = &varResultDummy;
299 /* Assume return type is void unless determined otherwise */
300 VariantInit(pVarResult);
302 /* If we are tracing, we want to see the name of the member we are invoking */
305 ITypeInfo_GetDocumentation(This->iTypeInfo, dispIdMember, &bstrName, NULL, NULL, NULL);
306 TRACE("Method %d, %s\n", dispIdMember, debugstr_w(bstrName));
309 hr = This->funcInvoke(This,dispIdMember,riid,lcid,wFlags,pDispParams,pVarResult,pExcepInfo,puArgErr);
311 if (hr == DISP_E_MEMBERNOTFOUND) {
312 if (bstrName == NULL) ITypeInfo_GetDocumentation(This->iTypeInfo, dispIdMember, &bstrName, NULL, NULL, NULL);
313 FIXME("Method %d, %s wflags %d not implemented, clsid %s\n", dispIdMember, debugstr_w(bstrName), wFlags, debugstr_guid(This->clsid));
315 else if (pExcepInfo &&
316 (hr == DISP_E_PARAMNOTFOUND ||
317 hr == DISP_E_EXCEPTION)) {
318 static const WCHAR szComma[] = { ',',0 };
319 static WCHAR szExceptionSource[] = {'M','s','i',' ','A','P','I',' ','E','r','r','o','r',0};
320 WCHAR szExceptionDescription[MAX_PATH];
321 BSTR bstrParamNames[MAX_FUNC_PARAMS];
325 if (FAILED(ITypeInfo_GetNames(This->iTypeInfo, dispIdMember, bstrParamNames,
326 MAX_FUNC_PARAMS, &namesNo)))
328 TRACE("Failed to retrieve names for dispIdMember %d\n", dispIdMember);
332 memset(szExceptionDescription, 0, sizeof(szExceptionDescription));
333 for (i=0; i<namesNo; i++)
335 if (bFirst) bFirst = FALSE;
337 lstrcpyW(&szExceptionDescription[lstrlenW(szExceptionDescription)], szComma);
339 lstrcpyW(&szExceptionDescription[lstrlenW(szExceptionDescription)], bstrParamNames[i]);
340 SysFreeString(bstrParamNames[i]);
343 memset(pExcepInfo, 0, sizeof(EXCEPINFO));
344 pExcepInfo->wCode = 1000;
345 pExcepInfo->bstrSource = szExceptionSource;
346 pExcepInfo->bstrDescription = szExceptionDescription;
347 hr = DISP_E_EXCEPTION;
351 /* Make sure we free the return variant if it is our dummy variant */
352 if (pVarResult == &varResultDummy) VariantClear(pVarResult);
354 /* Free function name if we retrieved it */
355 if (bstrName) SysFreeString(bstrName);
357 TRACE("Returning 0x%08x, %s\n", hr, SUCCEEDED(hr) ? "ok" : "not ok");
362 static const struct IDispatchVtbl AutomationObject_Vtbl =
364 AutomationObject_QueryInterface,
365 AutomationObject_AddRef,
366 AutomationObject_Release,
367 AutomationObject_GetTypeInfoCount,
368 AutomationObject_GetTypeInfo,
369 AutomationObject_GetIDsOfNames,
370 AutomationObject_Invoke
374 * IProvideMultipleClassInfo methods
377 static HRESULT WINAPI AutomationObject_IProvideMultipleClassInfo_QueryInterface(
378 IProvideMultipleClassInfo* iface,
382 AutomationObject *This = obj_from_IProvideMultipleClassInfo(iface);
383 return AutomationObject_QueryInterface((IDispatch *)This, riid, ppvoid);
386 static ULONG WINAPI AutomationObject_IProvideMultipleClassInfo_AddRef(IProvideMultipleClassInfo* iface)
388 AutomationObject *This = obj_from_IProvideMultipleClassInfo(iface);
389 return AutomationObject_AddRef((IDispatch *)This);
392 static ULONG WINAPI AutomationObject_IProvideMultipleClassInfo_Release(IProvideMultipleClassInfo* iface)
394 AutomationObject *This = obj_from_IProvideMultipleClassInfo(iface);
395 return AutomationObject_Release((IDispatch *)This);
398 static HRESULT WINAPI AutomationObject_IProvideMultipleClassInfo_GetClassInfo(IProvideMultipleClassInfo* iface, ITypeInfo** ppTI)
400 AutomationObject *This = obj_from_IProvideMultipleClassInfo(iface);
401 TRACE("(%p/%p)->(%p)\n", iface, This, ppTI);
402 return load_type_info((IDispatch *)This, ppTI, This->clsid, 0);
405 static HRESULT WINAPI AutomationObject_IProvideMultipleClassInfo_GetGUID(IProvideMultipleClassInfo* iface, DWORD dwGuidKind, GUID* pGUID)
407 AutomationObject *This = obj_from_IProvideMultipleClassInfo(iface);
408 TRACE("(%p/%p)->(%d,%s)\n", iface, This, dwGuidKind, debugstr_guid(pGUID));
410 if (dwGuidKind != GUIDKIND_DEFAULT_SOURCE_DISP_IID)
413 *pGUID = *This->clsid;
418 static HRESULT WINAPI AutomationObject_GetMultiTypeInfoCount(IProvideMultipleClassInfo* iface, ULONG* pcti)
420 AutomationObject *This = obj_from_IProvideMultipleClassInfo(iface);
422 TRACE("(%p/%p)->(%p)\n", iface, This, pcti);
427 static HRESULT WINAPI AutomationObject_GetInfoOfIndex(IProvideMultipleClassInfo* iface,
430 ITypeInfo** pptiCoClass,
432 ULONG* pcdispidReserved,
436 AutomationObject *This = obj_from_IProvideMultipleClassInfo(iface);
438 TRACE("(%p/%p)->(%d,%d,%p,%p,%p,%p,%p)\n", iface, This, iti, dwFlags, pptiCoClass, pdwTIFlags, pcdispidReserved, piidPrimary, piidSource);
443 if (dwFlags & MULTICLASSINFO_GETTYPEINFO)
444 load_type_info((IDispatch *)This, pptiCoClass, This->clsid, 0);
446 if (dwFlags & MULTICLASSINFO_GETNUMRESERVEDDISPIDS)
449 *pcdispidReserved = 0;
452 if (dwFlags & MULTICLASSINFO_GETIIDPRIMARY){
453 *piidPrimary = *This->clsid;
456 if (dwFlags & MULTICLASSINFO_GETIIDSOURCE){
457 *piidSource = *This->clsid;
463 static const IProvideMultipleClassInfoVtbl AutomationObject_IProvideMultipleClassInfo_Vtbl =
465 AutomationObject_IProvideMultipleClassInfo_QueryInterface,
466 AutomationObject_IProvideMultipleClassInfo_AddRef,
467 AutomationObject_IProvideMultipleClassInfo_Release,
468 AutomationObject_IProvideMultipleClassInfo_GetClassInfo,
469 AutomationObject_IProvideMultipleClassInfo_GetGUID,
470 AutomationObject_GetMultiTypeInfoCount,
471 AutomationObject_GetInfoOfIndex