472,353 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Having trouble using CTypes with a custom function

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@DU?$char_trai ts@D@std@@V?$allocator@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?

Thanks.

Oct 4 '06 #1
5 3385
On 4 Oct 2006 11:18:16 -0700, tk*****@gmail.com <tk*****@gmail.comwrote:
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@DU?$char_trai ts@D@std@@V?$allocator@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
Oct 4 '06 #2
Would you have any example of a wrapper for such data types?

Thanks.

Chris Mellon wrote:
On 4 Oct 2006 11:18:16 -0700, tk*****@gmail.com <tk*****@gmail.comwrote:
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@DU?$char_trai ts@D@std@@V?$allocator@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
Oct 4 '06 #3
<please don't top post>

On 4 Oct 2006 11:35:11 -0700, tk*****@gmail.com <tk*****@gmail.comwrote:
Would you have any example of a wrapper for such data types?

Thanks.

Chris Mellon wrote:
On 4 Oct 2006 11:18:16 -0700, tk*****@gmail.com <tk*****@gmail.comwrote:
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@DU?$char_trai ts@D@std@@V?$allocator@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
>
in this case it'd be very simple:

bool WrapGetMyString(char * c) {
return GetMyString(c)
}
(Note: This isn't unicode safe).

The C++ compiler will handle converting from the char * to the std::string.


--
http://mail.python.org/mailman/listinfo/python-list
Oct 4 '06 #4
Hi Chris.

I know that it is easy to fix the problem using C++. However, I do not
want to code a wrapper DLL. I was wondering if there was a workaround
with Python. Everything has to be done in Python as we do not have the
tools for C++ (and we are not planning on getting any).

Thanks.

Chris Mellon wrote:
<please don't top post>

On 4 Oct 2006 11:35:11 -0700, tk*****@gmail.com <tk*****@gmail.comwrote:
Would you have any example of a wrapper for such data types?

Thanks.

Chris Mellon wrote:
On 4 Oct 2006 11:18:16 -0700, tk*****@gmail.com <tk*****@gmail.comwrote:
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@DU?$char_trai ts@D@std@@V?$allocator@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

in this case it'd be very simple:

bool WrapGetMyString(char * c) {
return GetMyString(c)
}
(Note: This isn't unicode safe).

The C++ compiler will handle converting from the char * to the std::string.


--
http://mail.python.org/mailman/listinfo/python-list
Oct 4 '06 #5
tk*****@gmail.com wrote:
Hi all.

I just started looking at Python's ctypes lib and I am having trouble
using it for a function.
[...]
#C++ Prototype of the function I want to call:
ctypes provides support for calling functions in C libraries, using C
datatypes. I don't see any reason to believe it would work with other
languages (e.g. C++).

Oct 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gregory L. Hansen | last post by:
I can't seem to make a queue of objects, using the STL queue. I'm trying to make a little event manager, and I just want someplace to store events....
8
by: Joakim Persson | last post by:
Hello all. I am involved in a project where we have a desire to improve our software testing tools, and I'm in charge of looking for solutions...
1
by: Dieter Vanderelst | last post by:
Dear all, Could anybody tell me whether there are ways to use an existing DLL file in Python without having access to the source code? I'm...
3
by: Martin P. Hellwig | last post by:
Hey all, I'd like to wrap libpam so that I can use that for authentication and password management. I build ctypes (0.9.9.6) on my platform via...
4
by: Richard Jones | last post by:
Currently ctypes can't play well with any C code that requires use of setjmp as part of its API. libpng is one of those libraries. Can anyone...
1
by: sjdevnull | last post by:
Hey, I'm trying to wrap GNU readline with ctypes (the Python readline library doesn't support the callback interface), but I can't figure out...
6
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over...
0
by: JimPGH | last post by:
I am trying to use the OpenCV cvFitLine() function using the Python SWIG interface to OpenCV. My problem has to do with the fact that the last...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.