[Tutor] Python C extension - which method?

Brad M thebigwurst at gmail.com
Sat May 5 20:39:06 EDT 2018

Does this have any downside? I have noticed that printf("HI") in my .DLL;
doesn't really print anything.
I am on windows.


My next question is that I need to return an array or a list of address or
int or some type of data, but
if I returned a pointer to the array of some data type, how does the python
cope with it?

For instance, my plan is to have 3 functions:

1) a function that return all addresses that have a given value to python,
say, 9001
2) then I do something with python so that values change, the target value
is now 1009.
3) a second function that takes all the addresses as input and then return
adddress that have values of 1009.

4) repeat 2 and 3 until there is only 1 or so of address which correct
5) A 3rd function that takes only 1 address and simply return the values at
that address.

So the whole point of having 1-4 is to find the one address that contain
that values I want, and then
all I do is to call the 3rd function to find the values at that address.

So basically what I need to know is if the method I am using,
is proper or I need some other method, and then I would like to find out
how to handle the data returned.


On Sun, May 6, 2018 at 5:44 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:

> Hi,
> Brad M schrieb am 04.05.2018 um 11:30:
> > I want to create a C-based memory scanner for Python, and so far this is
> > how I do it:
> >
> > Python:
> >
> > from ctypes import cdll
> > mydll = cdll.LoadLibrary('hello.dll')
> > print(mydll.say_something())
> >
> > and hello.dll:
> >
> > #include <stdio.h>
> > __declspec(dllexport) int say_something()
> > {
> >     return 1980;
> > }
> >
> > so the printout is "1980"
> >
> > Is this alright?
> Depends on your needs and your C/C++ knowledge.
> If you have a shared library that provides the ready-made functionality,
> and accessing that native code at all is more important than calling it
> very quickly (e.g. you only do a few longish-running calls into it), then
> wrapping a shared library with ctypes (or preferably cffi) is a good way to
> do it.
> Otherwise, try either a native wrapper generator like pybind11, or write
> your wrapper in Cython.
> Specifically, if you are not just calling into an external library 1:1, but
> need to do (or can benefit from doing) non-trivial operations in native
> code, definitely use Cython.
> http://cython.org
> > I am aware that there is another much more complicated
> > method such as this:
> >
> > https://tutorialedge.net/python/python-c-extensions-
> tutorial/#building-and-installing-our-module
> Well, yes, it exists, but I advise against wrapping C code manually that
> way. It's just too cumbersome and error prone. Leave it to the experts who
> have already written their tools for you.
> Stefan
> Disclosure: I'm a Cython core dev, so I'm biased and I absolutely know what
> I'm talking about.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list