On 5/08/2009 5:55 AM, Thomas Heller wrote:
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.
This is (almost) what I was looking for. Thanks! However, there is one issue. Currently, CreateInstance always tries to return the best interface, even if the interface was explicitly specified. In my case, I want a specific interface, but due to the fact that the typelib is not registered on the system, comtypes ends up returning a dispatch, which is not what I want. I've attached a patch against svn trunk to fix this.

Thanks.

Jamie

--
James Teh
Email/MSN Messenger/Jabber: ja...@jantrid.net
Web site: http://www.jantrid.net/
Index: comtypes/server/__init__.py
===================================================================
--- comtypes/server/__init__.py (revision 531)
+++ comtypes/server/__init__.py (working copy)
@@ -16,14 +16,20 @@
         if dynamic:
             if interface is not None:
                 raise ValueError("interface and dynamic are mutually 
exclusive")
-            interface = comtypes.automation.IDispatch
+            realInterface = comtypes.automation.IDispatch
         elif interface is None:
-            interface = comtypes.IUnknown
-        obj = ctypes.POINTER(interface)()
-        self.__com_CreateInstance(punkouter, interface._iid_, 
ctypes.byref(obj))
+            realInterface = comtypes.IUnknown
+        else:
+            realInterface = interface
+        obj = ctypes.POINTER(realInterface)()
+        self.__com_CreateInstance(punkouter, realInterface._iid_, 
ctypes.byref(obj))
         if dynamic:
             return comtypes.client.dynamic.Dispatch(obj)
-        return comtypes.client.GetBestInterface(obj)
+        elif interface is None:
+            # An interface was not specified, so return the best.
+            return comtypes.client.GetBestInterface(obj)
+        # An interface was specified and obj is already that interface.
+        return obj
 
 ##class IExternalConnection(IUnknown):
 ##    _iid_ = GUID("{00000019-0000-0000-C000-000000000046}")
------------------------------------------------------------------------------
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