0

I have a fixed C libary as DLL file. The DLL file has some security functions which is why i cant look into the libary. I only know the functionname and the type of data is needed.

I need to give the function a Pointer to my var in Python, the adress in the memory. I try using ctypes but it wont work. For the loading of the lib. i use ctypes.

This is the function with all needed variables i need to start.

function  (
 const unsigned char* input_array,
 unsigned int multiply_by,
 unsigned char* output_array
);  

So i initialize my vars in Python like this:

input_array = "100"
multiply_by = 5
output_array = "000"

After that i load my lib and start the function.

lib = CDLL('func_lib.dll')
lib.function( hex(id(input_array)),
                 multiply_by,
                 hex(id(output_array))
             )

The function wont change the variable output_array, which is the goal of it. I can see that the function is called because no error is called. Do i handle the pointers wrong?

EDIT: I simplified the function to 3 vars, it would be the same for more. Also i think the input_array is the problem. The called function gets me a feedback-code which says, that the input_array is wrong. I dont know how to send the pointer adress to the function.

tryanderror
  • 153
  • 1
  • 9

1 Answers1

1

The parameter bigarray is declared const unsigned char * in the function signature. If this is correct, then there are few chances for the bigarray variable to change.

EDIT To summarize comments, here is a hopefully working sample, given the original function signature.

    # Load the lib
    lib = CDLL('func_lib.dll')

    # Defines signature, based on C one
    lib.function.argtypes = [ c_char_p, c_uint, c_char_p ]

    # Call parameters
    data_in = b'Sample input'
    data_out = create_string_buffer(16)

    # Call
    lib.function(data_in, 16, data_out)

One guess here, i supposed that the second parameter is a buffer size, hence in the sample call i gave the same value as to create_string_buffer.
The call to create_string_buffer is required here, has it is an output for your function. Beware that there is an obvious buffer overrun risk here.

Neywat
  • 103
  • 7
  • So u mean the problem is the "const"? I give the function a pointer to a start of a array from python which should not be protected or am i wrong? – tryanderror Feb 14 '20 at 06:45
  • What i mean is if the function signature is correct, then the first argument is not supposed to be modified by the function. Did you get the signature from DLL doc ? Or was it guessed ? – Neywat Feb 14 '20 at 09:38
  • thx u are right, i did post it wrong. i did a edit on my post – tryanderror Feb 14 '20 at 11:31
  • I dont think using ```hex(...)``` will give you the expected result. For input and output parameters i guess you need to use ctypes' ```byref()``` (see https://docs.python.org/2/library/ctypes.html#passing-pointers-or-passing-parameters-by-reference) or using ```c_char_p``` type. – Neywat Feb 14 '20 at 13:42
  • i tried using it like 'c_char_p(input_array)' but with this i get an error which says: bytes or integer address expected, so i tried, making a c var: 'c_input_array = c_wchar_p("100")' and use 'c_char_p(addressof(c_input_array))' in my function, but still no change – tryanderror Feb 14 '20 at 14:15
  • Maybe same problem as here : https://stackoverflow.com/questions/22873437/python-3-typeerror-bytes-or-integer-address-expected-instead-of-str-instance Also See there : https://stackoverflow.com/questions/37966432/how-to-pass-const-char-from-python-to-c-function Don't know if it helps, but did you use ```argtypes``` with your function definition (as described here : https://docs.python.org/2/library/ctypes.html#specifying-the-required-argument-types-function-prototypes) – Neywat Feb 14 '20 at 15:46
  • It still doesnt work or am i mistake by the use of ctypes? I the argtypes like this: `lib.function.argtypes = [ POINTER(c_char_p), c_uint, POINTER(c_char_p) ]` and made the vars for the function like this: `c_inputArray = c_char_p(b"100")` `c_multi = c_uint( 5 )` `c_output = c_char_p(b"000")` And finaly called the function with this: `lib.function( c_inputArray, c_multi, c_output`) – tryanderror Feb 17 '20 at 07:57
  • Sorry, not a ctypes specialist :) however, it seems that argtypes are not right, as your c_output parameter. Edited answer to clarify – Neywat Feb 17 '20 at 16:11
  • I think we are on the right way, but now i get another problem which says access violation reading 0x00000074 – tryanderror Feb 18 '20 at 08:22
  • Access violation indicates that the function is trying to read where it's not supposed to. It's difficult to know why without further information about the expected arguments. Maybe the input array is not large enough ? – Neywat Feb 18 '20 at 13:05
  • You where right, i just had a problem with the libary or better called false informations about the prototype, there was one arg to much in the functioncall. Thanks for the help – tryanderror Feb 20 '20 at 08:57