ctypes pointer to pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Podi

    #1

    ctypes pointer to pointer

    Hi,

    I have a C function in the dll does the following:

    DLL_API int dll_foo(char **data, size_t *size)
    {
    strcpy(*data, "hello");
    *size = strlen(*data);

    return 0;
    }


    So I would call the function as such,

    char data[8];
    size_t size;

    int status = dll_foo(&data, &size);

    I have tried the following in Python but not able to get it working...
    Any help would be appreciated!

    from ctypes import *
    mydll = windll.mydll
    array = c_char * 8
    data = array()
    size = c_long()
    status = mydll.dll_foo(b yref(data), byref(size))
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    WindowsError: exception: access violation writing 0x00000000


    mydll.dll_foo(P OINTER(data), byref(size))
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    File "C:\Python24\Li b\site-packages\ctypes \__init__.py", line 197, in
    POINTER
    return _pointer_type_c ache[cls]
    TypeError: unhashable type

    out = c_char_p(data.r aw)
    mydll.dll_foo(b yref(out), byref(size))
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    ValueError: Procedure probably called with too many arguments (8 bytes
    in excess)

    mydll..dll_foo( out, byref(size))
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    WindowsError: exception: access violation writing 0x6C6C6568

    temp = create_string_b uffer('\000' * 32)
    mydll.dll_foo(t emp, byref(size))
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    WindowsError: exception: access violation writing 0x00000000

    mydll.dll_foo(b yref(temp), byref(size))
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    WindowsError: exception: access violation writing 0x00000000

  • Gabriel Genellina

    #2
    Re: ctypes pointer to pointer

    At Wednesday 27/9/2006 22:58, Podi wrote:
    >I have a C function in the dll does the following:
    >
    >DLL_API int dll_foo(char **data, size_t *size)
    >{
    strcpy(*data, "hello");
    *size = strlen(*data);
    >
    return 0;
    >}
    >
    >
    >So I would call the function as such,
    >
    >char data[8];
    >size_t size;
    >
    >int status = dll_foo(&data, &size);
    >
    >I have tried the following in Python but not able to get it working...
    >Any help would be appreciated!
    >
    >from ctypes import *
    >mydll = windll.mydll
    >array = c_char * 8
    >data = array()
    >size = c_long()
    >status = mydll.dll_foo(b yref(data), byref(size))
    >Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    >WindowsError : exception: access violation writing 0x00000000
    I'd use your last try. But since it didnt work, use a prototype and
    see what happens:

    data = create_string_b uffer('\000' * 8)
    size = c_long()
    dll_foo = mydll.dll_foo
    dll_foo.argtype s = [POINTER(c_char_ p), POINTER(c_long)]
    status = dll_foo(byref(d ata), byref(size))

    Are you sure your function is compiled using the stdcall calling
    convention? I'm not sure what means DLL_API.



    Gabriel Genellina
    Softlab SRL





    _______________ _______________ _______________ _____
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!


    Comment

    • Podi

      #3
      Re: ctypes pointer to pointer

      Thanks for the help. I've figured it out. BTW, DLL_API is defined as

      #ifdef MYDLL_EXPORTS
      #define DLL_API __declspec(dlle xport)
      #else
      #define DLL_API __declspec(dlli mport)
      #endif


      size = c_int()
      data = c_char_p('\0' * 8)
      mydll = cdll.mydll # Use cdll instead of windll to avoid warnings
      status = mydll.dll_foo(b yref(data), byref(size))

      Comment

      Working...