How to access a python variable's data inside a C function? (Argument type: void *)

Folks,

I know I am doing something really stupid here but can't seem to find a way around this as I am relatively unfamiliar with ctypes in python. Simplified the code for better readability.

I have the following function in a shared library named 'LibTest.so':

#include<stdio.h>
#include<stdint.h>

void test(void *val1, void *val2) {

    printf("\nAddress of val1 = %x, val2 = %x\n", val1, val2);

    printf("\nval1 = %llu, val2 = %llu\n", *(uint64_t *)val1, *(uint64_t *)val2);
}

I am calling the above function in my Python (3.4.4) program using ctypes in the following manner:

import sys
from ctypes import *
lib = './LibTest.so'
dll = CDLL(lib)

dll.test.argtypes = [c_void_p, c_void_p]
dll.test.restype = None

def test(val1, val2):

# I am guessing the next 3 lines of code need a fix to make this work

    val1 = c_void_p()
    val2 = c_void_p()
    dll.test(byref(val1), byref(val2))


if __name__ == '__main__':

    val1 = 7122253
    val2 = 2147483647
    test(val1, val2)
    sys.exit(0)

For which I get the following (unexpected) output:

Address of val1 = 7d824780, val2 = 7d8249a0

val1 = 0, val2 = 0

But when I call the same function inside a C-program, it works:

  #include<stdio.h>
  #include<stdint.h>

  int main(){

  uint64_t val1 = 2147483647;
  uint64_t val2 = 7122253;

  void *ptr1, *ptr2;
  ptr1 = &val1;
  ptr2 = &val2;

  test(ptr1, ptr2);

  return(0);
}

And the output is:

Address of val1 = ffbff8a0, val2 = ffbff898

val1 = 2147483647, val2 = 7122253

Any suggestions on how to make this right? Any help is appreciated!


Click here view more answers http://ift.tt/1JAfiQG