473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(byref(data), byref(size))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation writing 0x00000000
mydll.dll_foo(POINTER(data), byref(size))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 197, in
POINTER
return _pointer_type_cache[cls]
TypeError: unhashable type

out = c_char_p(data.raw)
mydll.dll_foo(byref(out), byref(size))
Traceback (most recent call last):
File "<interactive 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 "<interactive input>", line 1, in ?
WindowsError: exception: access violation writing 0x6C6C6568

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

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

Sep 28 '06 #1
2 8586
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(byref(data), byref(size))
Traceback (most recent call last):
File "<interactive 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_buffer('\000' * 8)
size = c_long()
dll_foo = mydll.dll_foo
dll_foo.argtypes = [POINTER(c_char_p), POINTER(c_long)]
status = dll_foo(byref(data), 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!
http://www.yahoo.com.ar/respuestas

Sep 28 '06 #2
Thanks for the help. I've figured it out. BTW, DLL_API is defined as

#ifdef MYDLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#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(byref(data), byref(size))

Sep 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
3
by: rubbishemail | last post by:
Hello, i've got a problem with pointers in the following function which i want to use: I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value) The function is supposed to read out...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId :...
0
by: chris.atlee | last post by:
Hello, I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the script below), you're passed in a pam_response** pointer. You're supposed to allocate...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
1
by: Shalini Bhalla | last post by:
i have 2 tables one is master and another is detail having Ins-no common and iwant that when master recordset is moved then detail recordset pointershould filter detail records correspondint to...
4
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c),...
0
by: gianluca | last post by:
I've a problem with dll function colled with python/ctypes. My functions (C) requred a typedef int "value_type" in tree different way: same as value_type; - mydll.foo1(value_type) same as...
2
by: Jean-Paul Calderone | last post by:
On Mon, 30 Jun 2008 09:13:42 -0700 (PDT), gianluca <geonomica@gmail.comwrote: POINTER takes a class and returns a new class which represents a pointer to input class. pointer takes an instance...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.