Retrieving BSTR * from a DLL

M

mzdude

I need to interface with a windows DLL that has the following
signature

extern "C" void Foo( BSTR in, BSTR *out )

Code so far
from ctypes import *
import comtypes
LPBSTR = POINTER(comtypes.BSTR)

hdl = windll.MyDll.Foo
hdl.rettype = None
hdl.argtypes = [comtypes.BSTR, LPBSTR]

inStr = comtypes.BSTR(u'Some Silly String')
out = comtypes.BSTR

hdl(inStr,byref(out))

Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
hdl(inStr,byref(out))
TypeError: byref() argument must be a ctypes instance, not
'_ctypes.SimpleType'

Also tried the following

Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
hdl(inStr,p)
ValueError: Procedure probably called with too many arguments (8 bytes
in excess)

Any feedback would be appreciated.
 
A

Andrew MacIntyre

mzdude said:
I need to interface with a windows DLL that has the following
signature

extern "C" void Foo( BSTR in, BSTR *out )

Code so far
from ctypes import *
import comtypes
LPBSTR = POINTER(comtypes.BSTR)

hdl = windll.MyDll.Foo
hdl.rettype = None
hdl.argtypes = [comtypes.BSTR, LPBSTR]

inStr = comtypes.BSTR(u'Some Silly String')
out = comtypes.BSTR

out = comtypes.BSTR()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
hdl(inStr,byref(out))
TypeError: byref() argument must be a ctypes instance, not
'_ctypes.SimpleType'

comtypes.BSTR is a type; the type error makes clear you need
an instance, as above.
Also tried the following


Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
hdl(inStr,p)
ValueError: Procedure probably called with too many arguments (8 bytes
in excess)

This likely indicates that the DLL is using the C calling convention
and not the stdcall calling convention. Use CDLL rather than WinDLL
to load the DLL.

You might like to join the ctypes-users mailing list at sourceforge.

--
 
M

mzdude

On Jul 10, 6:15 am, Andrew MacIntyre <[email protected]>
wrote:
This likely indicates that the DLL is using the C calling convention
and not the stdcall calling convention.  Use CDLL rather than WinDLL
to load the DLL.

using cdll got me over the calling hurdle. However, I'm not seeing the
returned BSTR.
You might like to join the ctypes-users mailing list at sourceforge.

I did, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top