Having trouble using CTypes with a custom function

Chris Mellon arkanes at gmail.com
Wed Oct 4 14:25:41 EDT 2006


On 4 Oct 2006 11:18:16 -0700, tkondal at gmail.com <tkondal at gmail.com> wrote:
> Hi all.
>
> I just started looking at Python's ctypes lib and I am having trouble
> using it for a function.
>
> For starters, here's my Python code:
>
>
> from ctypes import*;
> myStringDLL= cdll.LoadLibrary("myStringDLL.dll");
>
> GetMyString = getattr(myStringDLL,
> "?GetMyString@@YA_NAAV?$basic_string at DU?$char_traits at D@std@@V?$allocator at D@2@@std@@@Z")
>
> strString =  create_string_buffer('\000' * 256);
> GetMyString.restype = c_int;
> GetMyString.argtypes = [c_char_p];
>
> bResult = GetMyString (strSerialNumber);
>
> print (bResult);
> print (strSerialNumber);
>
> #C++ Prototype of the function I want to call:
> #bool GetMyString (string& stringParam) ;
>
>
>
>
> I do not have access to the source code of this function so don't ask
> me to try different things in C++.  This DLL is working fine.
>
> The problem that I have is that print (strSerialNumber) does not seem
> to print the correct string.  What I get is some garbage value of
> unprintable characters.  Am I using this the correct way?
>




This function is expecting a C++ std::string object, not a regular C
style string. You'll need a wrapper function, and one which uses the
same compiler and STL as the C++ source.

> Thanks.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list