I want to call a C function from a C++ dll by its address.
I know how to do it where the return type is known from this separate question: Calling a function through its address in memory in c / c++.
However, if I do not know the return type, what can I do? I've tried "typedef auto", but it seems you cannot use auto like that.
If the returning type is really unknown or it doesn't matter, you can use
void
in your function definition, or if it is any pointer you can usevoid *
, but if the function is in a C coded DLL, and you'll use it in a C++ code, then you can utilize and share almost every type defined in your C code, because C++ it's a superset of C.That said, I prepared a small example with a structure called
PyObject
shared in C and C++ codes.To do this, is better creating a header with the shared types/definitions:
Let's suppose that the C code with the exported function is something like:
Finally the C++ code using the function and data created in the library would be:
Edit
As @raymondchen pointed in his comment, ignoring the return type when the C function returns a large aggregate (e.g. struct) it's not a good idea, because the C function expects that the caller already has had reserved stack space to store the returned aggregate, but if the caller treats the function as
void
or anything else, then compiler will not reserve that space, causing unpredictable effects (probably ending with Segmentation fault error).To avoid it, it's always better to define the correct type in both C and C++ codes (or in the common header), especially when the C function returns an aggregate.