Retrieving BSTR * from a DLL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mzdude

    Retrieving BSTR * from a DLL

    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(comtype s.BSTR)
    >>>
    >>hdl = windll.MyDll.Fo o
    >>hdl.rettype = None
    >>hdl.argtype s = [comtypes.BSTR, LPBSTR]
    >>>
    >>inStr = comtypes.BSTR(u 'Some Silly String')
    >>out = comtypes.BSTR
    >>>
    >>hdl(inStr,byr ef(out))
    Traceback (most recent call last):
    File "<pyshell#1 4>", line 1, in <module>
    hdl(inStr,byref (out))
    TypeError: byref() argument must be a ctypes instance, not
    '_ctypes.Simple Type'

    Also tried the following
    >>out = comtypes.BSTR(u '')
    >>p = pointer(out)
    >>hdl(inStr,p )
    Traceback (most recent call last):
    File "<pyshell#1 9>", line 1, in <module>
    hdl(inStr,p)
    ValueError: Procedure probably called with too many arguments (8 bytes
    in excess)

    Any feedback would be appreciated.
  • Andrew MacIntyre

    #2
    Re: Retrieving BSTR * from a DLL

    mzdude wrote:
    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(comtype s.BSTR)
    >>>>
    >>>hdl = windll.MyDll.Fo o
    >>>hdl.rettyp e = None
    >>>hdl.argtyp es = [comtypes.BSTR, LPBSTR]
    >>>>
    >>>inStr = comtypes.BSTR(u 'Some Silly String')
    >>>out = comtypes.BSTR
    out = comtypes.BSTR()
    >>>hdl(inStr,by ref(out))
    >
    Traceback (most recent call last):
    File "<pyshell#1 4>", line 1, in <module>
    hdl(inStr,byref (out))
    TypeError: byref() argument must be a ctypes instance, not
    '_ctypes.Simple Type'
    comtypes.BSTR is a type; the type error makes clear you need
    an instance, as above.
    Also tried the following
    >
    >>>out = comtypes.BSTR(u '')
    >>>p = pointer(out)
    >>>hdl(inStr, p)
    >
    Traceback (most recent call last):
    File "<pyshell#1 9>", 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.

    --
    -------------------------------------------------------------------------
    Andrew I MacIntyre "These thoughts are mine alone..."
    E-mail: andymac@bullsey e.apana.org.au (pref) | Snail: PO Box 370
    andymac@pcug.or g.au (alt) | Belconnen ACT 2616
    Web: http://www.andymac.org/ | Australia

    Comment

    • mzdude

      #3
      Re: Retrieving BSTR * from a DLL

      On Jul 10, 6:15 am, Andrew MacIntyre <andy...@bullse ye.apana.org.au >
      wrote:
      <snip>
      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.

      Comment

      Working...