I have the problem where I want to pass a uint8_t[]
array as a parameter to a function pointer defined as `typedef void (dangerousC)(void); Also, I'm using Windows API headers.
Assume the variable raw
is a function pointer returned by GetProcAddress()
. Also assume that the parameters to foo()
are not known by the compiler.
Here is the full code:
#include <iostream>
#include <Windows.h>
typedef void (*dangerousC)(char*);
void foo(int a) {
std::cout << a << std::endl;
}
int main() {
auto raw = (FARPROC) foo;
auto* b = new uint8_t[4];
b[0] = 74;
b[1] = 35;
b[2] = 0;
b[3] = 0;
std::cout << *(int*)b << std::endl;
auto func = (dangerousC)raw;
func(reinterpret_cast<char *>(*b));
delete[] b;
}
When I call the function pointer with the parameters reinterpret_cast<char *>(*b)
, I only get one character, which is reasonable when I dereference a pointer.
But I also tried with a pointer and it does not print the result that I want, which is 9034.
How could I make the function print 9034 and fully interpret the byte array as a 32-bit int?
foo()
expects an int
, but you are passing it a char*
instead. As such, you need to pass in a char*
whose value is the integer you want, not an actual pointer to an integer.
#include <iostream>
#include <Windows.h>
typedef void (*dangerousC)(char*);
void foo(int a) {
std::cout << a << std::endl;
}
int main() {
auto raw = (FARPROC) foo;
uint8_t b[4];
b[0] = 74;
b[1] = 35;
b[2] = 0;
b[3] = 0;
int32_t i = *reinterpret_cast<int32_t*>(b);
std::cout << i << std::endl;
auto func = reinterpret_cast<dangerousC>(raw);
func(reinterpret_cast<char*>(i));
}