James Teh schrieb:
> Hi all,
> 
> I've had cause to instantiate a COM class which is not registered on the 
> system. To do this, one uses DllGetClassObject on the dll and then calls 
> CreateInstance on the resulting IClassFactory.
> 
> IClassFactory::CreateInstance is defined as:
> HRESULT CreateInstance(
>    [in]   IUnknown *pUnkOuter,
>    [in]   REFIID riid,
>    [out]  void **ppvObject
> );
> 
> However, in comtypes.server.IClassFactory, CreateInstance is defined as 
> follows:
> _methods_ = [
>          comtypes.STDMETHOD(comtypes.HRESULT, "CreateInstance",
>                             [ctypes.c_int, 
> ctypes.POINTER(comtypes.GUID), ctypes.POINTER(ctypes.c_ulong)]),
> ...
> 
> The final parameter being declared as c_ulong makes life a little 
> painful. In order to use it, you have to do something like:
> instance = ctypes.c_ulong()
> factory.CreateInstance(0, interface._iid_, ctypes.byref(instance))
> instance = ctypes.POINTER(interface)(instance.value)
> 
> First of all, is there a reason that last parameter is c_ulong instead 
> of c_void_p or the like?

I do not remember the details, but it looks strange.  And the first parameter
type is also wrong, it should be a pointer.

> Second, is there any chance we could make this more Pythonic like the 
> recent IServiceProvider.QueryInterface? Something like:
> instance = factory.CreateInstance(interface=interface)
> I'm curious as to how frequently this is already used, as such a change 
> would break existing usage.

I don't think anyone was using the IClassFactory::CreateInstance method,
otherwise I think I've heard this complaint earlier.

So, I have now in svn implemented the following:

- Fixed the IClassFactory::CreateInstance method
- Implemented a Pythonic interface for it, also including 
  the nice 'dynamic=False' parameter.
- Implemented a low-level comtypes.CoGetClassObject() function, and
  a high level comtypes.client.GetClassObject()

Please have a look at it and report your observations.

-- 
Thanks,
Thomas


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to