Quoting from the docs:

Sometimes you have instances of incompatible types. In C, you can cast one type into another type. ctypes provides a cast() function which can be used in the same way.

So, the Python equivalent of the C++ code is:

license = cast(d.GetLicense(), ctypes.POINTER(AppLicense))
license.GetApplicationStatus()

However, often this isn't necessary; you may be able to just do this:

d.GetLicense.restype = ctypes.POINTER(AppLicense)

This looks like "cheating", but it really isn't. You're just telling it to call the POINTER(AppLicense) constructor with the result. And since POINTER(AppLicense) is a ctypes data type, it will not have to first assume the result is a C int.

Answer from abarnert on Stack Overflow
Top answer
1 of 2
4

Quoting from the docs:

Sometimes you have instances of incompatible types. In C, you can cast one type into another type. ctypes provides a cast() function which can be used in the same way.

So, the Python equivalent of the C++ code is:

license = cast(d.GetLicense(), ctypes.POINTER(AppLicense))
license.GetApplicationStatus()

However, often this isn't necessary; you may be able to just do this:

d.GetLicense.restype = ctypes.POINTER(AppLicense)

This looks like "cheating", but it really isn't. You're just telling it to call the POINTER(AppLicense) constructor with the result. And since POINTER(AppLicense) is a ctypes data type, it will not have to first assume the result is a C int.

2 of 2
1

I spent more time with this - from c++ when I want to work with the class instance the void pointer refers to I do something like this:

class AppLicense {
public:
    AppLicense() {}
    virtual LicenseStatus GetApplicationStatus() = 0;
}

But I couldn't figure out how to do that in Python. This does not work:

class AppLicense(object):
  def GetApplicationStatus(self):
    pass

So instead I exported another function in the dll like this:

extern "C" {
    int P_GetApplicationStatus(void *ptr) {
        try {
            AppLicenseImpl * ref = reinterpret_cast<AppLicenseImpl *>(ptr);
            return ref->GetApplicationStatus();
        } catch (...) {
            return 0; // License Error default.
        }
    }
}

Once I had that in place, using it from Python is accomplished like this:

d.GetLicense.restype = ctypes.c_void_p
p = d.GetLicense()

d.C_GetApplicationStatus.argtypes = [ctypes.c_void_p]
status = d.P_GetApplicationStatus(p)
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-pointers-in-python-using-ctypes
Using Pointers in Python using ctypes - GeeksforGeeks
July 23, 2025 - We can also create some void pointers of a specific type that doesn't hold any address while creating. ... import ctypes as ct # Creating a variable ptr2 which will act as a void pointer # We will not point it to any other memory location ptr2 = ct.POINTER(ct.c_int) # Trying to print a void ...
🌐
Things Of Some Value
mattgwwalker.wordpress.com › 2020 › 05 › 30 › pointer-manipulation-in-python
Pointer Manipulation in Python – Things Of Some Value
May 30, 2020 - Yes, but this time we have >>> # to use the value member: >>> oldAddress = p3.value >>> newAddress = oldAddress + 2 # again, because shorts are two bytes >>> p3 = ctypes.c_void_p(newAddress) >>> >>> # Let's view their values >>> oldAddress 4470827794 >>> newAddress 4470827796 >>> p3 c_void_p(4470827796) >>> >>> # And finally, what is the value of the element-being-pointed-to? >>> p5 = ctypes.cast(p3, ctypes.POINTER(ctypes.c_short)) >>> p5.contents c_short(42) >>> >>> # Isn't the definition of p5 the same as p4?
🌐
ProgramCreek
programcreek.com › python › example › 1192 › ctypes.c_void_p
Python Examples of ctypes.c_void_p
def c_handle_array(objs): """Create ctypes const void ** from a list of MXNet objects with handles. Parameters ---------- objs : list of NDArray/Symbol. MXNet objects. Returns ------- (ctypes.c_void_p * len(objs)) A void ** pointer that can be passed to C API.
🌐
SageMath
doc.sagemath.org › html › en › thematic_tutorials › numerical_sage › ctypes.html
Ctypes - Thematic Tutorials
Note here that a.ctypes.data_as(c_void_p) returns a ctypes object that is void pointer to the underlying array of a.
🌐
Stack Overflow
stackoverflow.com › questions › 42387109 › ctypes-void-pointer-array-in-python › 42388174
c++ - ctypes void pointer array in Python - Stack Overflow
February 22, 2017 - now I am using the model of ctypes in python to introduce a .dll file. In my source code I have a function in c++ as followings · bool getFrameFromStream(char* streamName, const void* framebuffer, int frame_number, int overwriteMaxPixelVal=-1) where framebuffer is a pointer pointing to an array of pixel data of flexible types, e.g.
🌐
Stack Overflow
stackoverflow.com › questions › 13845608 › const-void-pointer-in-ctypes
python - const void* pointer in ctypes - Stack Overflow
You can cast a Python string to char* with ctypes, and that points to actual memory there Python string data is stored. Feel free to double-cast. ... Sign up to request clarification or add additional context in comments. ... In the case of a void*, a str/unicode (Python 2.x) or bytes/str (Python 3.x) can just be passed directly to the function. They will be marshaled as a pointer to the bytes of the value, or in the case of a Unicode string, a pointer to the OS-appropriate UTF-encoded bytes of the string, e.g.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 49901708 › void-pointer-in-a-function-with-ctypes
python - Void pointer in a function with ctypes - Stack Overflow
April 18, 2018 - def _callback_pointer(handler): ...tHookEx(*args, **kwargs) print(res) return res cmp_func = CFUNCTYPE(c_int, c_int, wintypes.HINSTANCE, POINTER(c_void_p)) hinstDLL = ctypes.windll.LoadLibrary('Gdi32')._handle hkprcSysMsg = ctypes.windll.kernel32.GetProcAddress(hinstDLL, ...
🌐
Bytes
bytes.com › home › forum › topic › python
A Question about ctypes and a function f(void **) - Post.Byes
August 11, 2008 - > If i do d.contents i get c_void_p(306747 8024L) You almost answered your own question! d.contents is a c_void_p ie a (void *) so you can pass d.contents to QImage directly I would have thought. If you want to actually read the data from python then you'll need to cast it to some other pointer type than c_void_p first, eg ... >>> or use ctypes.memmove to copy the data out to somewhere else.
🌐
Terryoy
terryoy.github.io › 2014 › 03 › using-ctypes-to-wrap-c-library.html
Use Ctypes to Wrap C Libraries in Python - Hello, World!
# it's a good practice to declare the structures and the functions at first like in C # ... # declare Structures class Context(Structure): pass ContextPtr = ctypes.POINTER(Context) # ... # declare functions aa_init = libaa.aa_init aa_init.argtypes = [DriverPtr, HardwareSettingsPtr, ctypes.c_void_p] # parameters are (pointer of struct "Driver", pointer of struct HardwareSetting, and a pointer of "void") aa_init.restype = ContextPtr aa_fastrender = libaa.aa_fastrender aa_fastrender.argtypes = [ContextPtr] + 4 * [ctypes.c_int] # calling aa_init() and aa_fastrender() context = aa_init(ctypes.pointer(aa_mem_d), ctypes.pointer(settings), None) # ...
🌐
Stack Overflow
stackoverflow.com › questions › 21592156 › void-pointer-for-c-dll-written-in-ctypes
c# - VOID Pointer for C DLL written in CTYPES - Stack Overflow
I set the argtype of the DLL to c_void_p, and did a byref(data) then repr(data.raw) and returned '\x00\x00\x00\x00' the result from the code I am running should return the IP address of the device I am talking too. Any other thoughts? ... I fixed my problem.... I had to remove the argtype setting and just pass in the variables directly. ... Markku K. – Markku K. 2014-02-11 16:11:11 +00:00 Commented Feb 11, 2014 at 16:11 ... getFuncDLL.argtypes = [c_long, c_int, ctypes.POINTER(STRUCTURE_SETTINGS), c_void_p, c_ulonglong] getFuncDLL.restype = c_long
🌐
Stack Overflow
stackoverflow.com › questions › 25425760 › python-ctypes-retrieve-value-of-void-pointer-sent-to-shared-c-library
python - ctypes, retrieve value of void pointer sent to shared c library - Stack Overflow
August 21, 2014 - The size of the type is not needed, as the size of your types is known at compile time. Basically the function just converts the void* to a pointer of your desired type and then uses pointer arithmetic to iterate over the array.
🌐
OCaml
discuss.ocaml.org › ecosystem
Ctypes - how to cast a function pointer and then call it? - Ecosystem - OCaml
April 7, 2022 - To provide a simple example imagine we have a Simplistic C library: typedef void (*void_function)(void); typedef int (*adder)(int, int); int add(int a, int b){ return a + b; } void_function load(void){ return (void_function)add; } I can bind ...
🌐
Python
docs.python.org › 3 › library › ctypes.html
ctypes — A foreign function library for Python
This behavior can be customized by assigning to special attributes of the foreign function object. ... Assign a ctypes type to specify the result type of the foreign function. Use None for void, a function not returning anything.