473,406 Members | 2,208 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,406 software developers and data experts.

C pointers from python

8
Hi, How to call pointer variable of c++ in python or how to call referance variables?
Feb 23 '07 #1
4 2853
bartonc
6,596 Expert 4TB
Hi, How to call pointer variable of c++ in python or how to call referance variables?
Are you using swig or the ctype module? The ctypes module is so much easier (that's why I ask).
Feb 23 '07 #2
bartonc
6,596 Expert 4TB
Hi, How to call pointer variable of c++ in python or how to call referance variables?
If you use better titles, then, even if you leave the specifics out of the question, we'll know better how to help you. And I wont have to change them later.
Feb 23 '07 #3
dshimer
136 Expert 100+
I'm not clear on what you need to do or what modules you are using but let me post a couple of lines that I used this week while working with an external DLL that needed C pointers. I only barely understand it at this point because it is new to me, from a friend, and I haven't worked in C in 10 years, but it worked and may lead you in the right direction.

The DLL expected C pointers for input and output to it's functions.
from ctypes import *
Because he's using ctypes.

ExternalLib = windll.LoadLibrary("LibName.dll")
To give me access to the DLL functions.

tmpStr = create_string_buffer(256)
I'm assuming this is a ctypes function that creates a C string pointer.

inx = c_double(1000.00)
This DLL works with coordinates, I'm going to pass a double pointer into one of the functions so must declare it as such.

ExternalLib.SetXIn(inx)
SetXIn() is one of the libraries functions so I'm passing in the c double pointer which I set above.

ExternalLib.GetXOut.restype = c_double
As I understand this (which I really don't) The GetXOut will be returning a pointer to a C double so I have to declare it as such.

outx = ExternalLib.GetXOut()
Then make the call which now give me a regular double stored in python.

I also read a bit of the python Ctypes Tutorial, though only enough to get a basic understanding for now (which is probably obvious).
Feb 24 '07 #4
bartonc
6,596 Expert 4TB
I'm not clear on what you need to do or what modules you are using but let me post a couple of lines that I used this week while working with an external DLL that needed C pointers. I only barely understand it at this point because it is new to me, from a friend, and I haven't worked in C in 10 years, but it worked and may lead you in the right direction.

The DLL expected C pointers for input and output to it's functions.
from ctypes import *
Because he's using ctypes.

ExternalLib = windll.LoadLibrary("LibName.dll")
To give me access to the DLL functions.

tmpStr = create_string_buffer(256)
I'm assuming this is a ctypes function that creates a C string pointer.

inx = c_double(1000.00)
This DLL works with coordinates, I'm going to pass a double pointer into one of the functions so must declare it as such.

ExternalLib.SetXIn(inx)
SetXIn() is one of the libraries functions so I'm passing in the c double pointer which I set above.

ExternalLib.GetXOut.restype = c_double
As I understand this (which I really don't) The GetXOut will be returning a pointer to a C double so I have to declare it as such.

outx = ExternalLib.GetXOut()
Then make the call which now give me a regular double stored in python.

I also read a bit of the python Ctypes Tutorial, though only enough to get a basic understanding for now (which is probably obvious).
Awesome, D. I'll move a copy of your post to the new Articles sub section.
Feb 24 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: km | last post by:
Hi all, may i know if there is any plans of introducing the concept of pointers into python language as in C ? (atleast in future versions ?) regards, thanks KM
0
by: zde | last post by:
Hi, What's the preferred way to wrap handles to C structures in python? (opaque 32-bit pointers with LSBs=00, that python should never deref) Convert directly to PyLong? (seems most natural,...
94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
2
by: Benji99 | last post by:
Hi guys, I'm starting to learn Python and so far am very impressed with it's possibilities. I do however need some help with certain things I'm trying to do which as of yet haven't managed to find...
8
by: joe | last post by:
Hello, I am trying to convert some C code into python. Here's the C code... typedef enum { CODE1 = 0x1, CODE2 = 0x2 } CODE testit(unsigned char *buffer,
19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
18
by: chankl | last post by:
Can anyone explain what's an opaque pointer and how it's implemented in C? I read about this concept in the book "C interfaces and implementations". Here's an example from the book (list.h -...
3
by: abhishek | last post by:
Hi group any idea on HOW TO HANDLE POINTERS FROM NON-LOCAL HEAPS?? Thank you
3
by: Ron Garret | last post by:
CTypes on a 64-bit machine appears to be truncating pointers to 32 bits: $ uname -a Linux monster1 2.6.18-6-amd64 #1 SMP Mon Jun 16 22:30:01 UTC 2008 x86_64 GNU/Linux $ cat foo.c void*...
0
by: supa | last post by:
Hi. I've coded some stuff a long time ago, and didn't have reason to take up programming until now. I've learned Python 'on-the-go' and now ran into the idea of avoiding global variables of any kind....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.