0

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";
asked Jul 2, 2013 at 5:31
2
  • 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. Commented Jul 2, 2013 at 5:35
  • can you send me true c++ code and python? Commented Jul 2, 2013 at 5:38

1 Answer 1

1

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.

answered Jul 2, 2013 at 5:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.