Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have this C++ code in linux ubuntu i want use this method in python by ctype but can not send parameter to ctype.cdl.funcion

C++ code :

extern "C"  unsigned char* getuserdata(int code,unsigned char* globalkey,unsigned char* key)
{
        unsigned char data[256];
        KeyA *keya;         
        keya=new KeyA;
        keya->OpenDevice(0);
        keya->Init(globalkey,globalkey,globalkey,globalkey);
        keya->ReadUserMemory( 0,256,key,data);
        return data;    
 }

sample use this function in C++:

 unsigned char g_user[16] = { 0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22 };
 unsigned char publickey[16] = { 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55 };
 printf("function Return: %s\n", getuserdata(0,publickey,g_user));

and my python source code (not Worked!!) is :

from ctypes import *
dl=cdll.LoadLibrary('/home/iman/KCore/kcore/hkey.so');
dl.getuserdata.restype = c_char_p
string_buffers = [addressof(create_string_buffer(16)) ]
string_buffers[0]= "5555555555555555";
string_buffers2 = [addressof(create_string_buffer(16)) ]
string_buffers2[0]="2222222222222222";
st= dl.getuserdata(0,string_buffers,string_buffers2);
print st+"\n";
share|improve this question
2  
data is on the stack and destroyed after the function returns. You are returning a pointer to an object that doesn't exist any more. In other words, this function is broken regardless of how you call it, ctypes doesn't have any influence on it. Oh, and keya is leaked, which is another reason to rewrite it. – Ulrich Eckhardt Jul 2 at 5:35
can you send me true c++ code and python? – iman mir Jul 2 at 5:38

1 Answer

Let's look at the code...

string_buffers = [addressof(create_string_buffer(16)) ]

This line creates a Python list containing the address of a 16-byte string buffer (or maybe it's not bytes but characters, please find that out yourself).

string_buffers[0]= "5555555555555555";

This line replaces the pointer from above with the string "555555555555555".

dl.getuserdata(0,string_buffers,string_buffers2);

Here, you pass the list with a string to the function, while the function takes a pointer to bytes. Question is what you want to achieve here, i.e. whether you want the buffer to be written to or not. If not, use const in C++ and simply pass "22222" as parameter, ctypes will do the rest for you automatically.

That said, it could be that I'm guessing wrong, since you haven't explained what exactly is happening (quote the error messages!) and how exactly you determined that something doesn't work. Further, you should clean up your broken C++ code or temporarily replace it with something smaller that is more suitable to explain the exact problem.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.