For example, I know the address of the function (lets say its 0x0183ad0
), and I would like to get an object from that function which returns an unknown type:
unknownType(*fname)() = (unknownType(*)())0x0183ad0
Is there any global type I can replace unknownType
with or any method to get that object as a byte array (I know the sizeof(unknownType)
)
The function return an object NOT A POINTER
"byte array" would be uint8_t*
or unsigned char*
.
If you know the size of unknownType
I would create a
struct unknownType {
uint8_t stuff[123];
};
so you can adapt the struct definition as you gain better understanding of its fields.
You could define a void function pointer and use that prototype for each of the functions that you have the address of: The return value in this example is to aa RANDOM STRUCTURE it could be the whole array of whatever you want bytes, ints, chars- This could of course be different for each function called, as long as the function signature was the same (you could always add a void pointer on the parameter list if they need different input)
#include <stdio.h>
typedef struct _RANDSTRUCT
{
int Num ;
} RANDSTRUCT ;
typedef void * UNKNOWNFUNC (void) ;
UNKNOWNFUNC *myFunc() ;
int main()
{
void *retP ;
UNKNOWNFUNC *fnP ;
retP = myFunc () ;
printf ("Num %d (direct call)\n", ((RANDSTRUCT *)retP)->Num) ;
fnP = (void *)&myFunc ;
retP = fnP () ;
printf ("Num %d (fn addr call)\n", ((RANDSTRUCT *)retP)->Num) ;
return 0;
}
UNKNOWNFUNC *myFunc () {
static RANDSTRUCT randstruct[2] = {100, 200} ;
return ((void *)&randstruct[0]) ;
}
How to get a byte array in c/c++ from a function with unknown return type?
Only way to call a function in C++ is to know its return type. If you don't know the return type, then you cannot call the function, and as such you cannot get the object returned by the function.
It worked thanks to Botje's answer
It may look like it works, but the behaviour of the program is undefined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.