stenci
stenci

Reputation: 8501

Creating a COM object with comtypes

I am trying to port to Python some old VBA code.

In VBA I add Geo3D.dll as a project reference, then I use this:

Set P1 = New GEO3DLib.Point
P1.Set 1, 2, 3, 0.001

In Python I tried this:

import comtypes
import comtypes.client as cc
cc.GetModule('C:\\Program Files (x86)\\think3\\2009.3\\thinkdesign\\bin\\Geo3d.dll')
import comtypes.gen.GEO3DLib as Geo3d
pt = cc.CreateObject('Geo3d.Point', None, None, Geo3d.Point)

But I get this error:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    pt = cc.CreateObject('Geo3d.Point', None, None, Geo3d.Point)
  File "C:\Anaconda3\lib\site-packages\comtypes\client\__init__.py", line 238, in CreateObject
    obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
  File "C:\Anaconda3\lib\site-packages\comtypes\__init__.py", line 1217, in CoCreateInstance
    iid = interface._iid_
AttributeError: type object 'Point' has no attribute '_iid_'

Replacing the last line with:

pt = Geo3d.Point
pt.Set(1., 2., 3., 0.001)

I get this error:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    pt.Set(1., 2., 3., 0.001)
AttributeError: type object 'Point' has no attribute 'Set'

Any idea?

Upvotes: 1

Views: 4518

Answers (1)

Matteo Boscolo
Matteo Boscolo

Reputation: 577

Enure that the python implementation and the thinkdesign library are compiled for the same platform type: 64 bit or 32 bit.

I tested this as follows:

>>> p=Dispatch('Geo3d.Point')
>>> p
<win32com.gen_py.think3 thinkdesign Type Library.IPoint instance at 0x59554312>
>>> p.Set(0,0,0)
>>> p.X
0.0

or with comtypes:

>>> point=comtypes.CoCreateInstance(comtypes.GUID.from_progid('Geo3d.Point'))
>>> ipoint=point.QueryInterface(Geo3d.IPoint)
>>> ipoint.Set(0,0,0)
0
>>> ipoint.X
0.0

Upvotes: 3

Related Questions