提问人:block103 提问时间:4/10/2023 最后编辑:Remy Lebeaublock103 更新时间:4/10/2023 访问量:331
将 uint8_t* 作为参数传递给原始函数指针
Pass uint8_t* as parameter to raw function pointer
问:
我遇到了一个问题,我想将数组作为参数传递给定义为'typedef void (dangerousC)(void);另外,我正在使用 Windows API 标头。uint8_t[]
假设该变量是 返回的函数指针。此外,假设编译器不知道 to 的参数。raw
GetProcAddress()
foo()
以下是完整代码:
#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;
}
当我使用参数调用函数指针时,我只得到一个字符,这在我取消引用指针时是合理的。reinterpret_cast<char *>(*b)
但我也尝试使用指针,它没有打印我想要的结果,即 9034。
如何使函数打印 9034 并将字节数组完全解释为 32 位 int?
答:
0赞
273K
4/10/2023
#1
通常它是像下面这样完成的,正如我从您的评论中正确理解的那样,您处理字节流
int n;
std::memcpy(&n, b, sizeof(n));
func(b);
评论
0赞
block103
4/10/2023
我想通了,并让它使用 uint8_t* 指针。auto* ptr = 新uint8_t[4];memcpy(&ptr, b, 4);函数(PTR);出于某种奇怪的原因,这对我有用,但无论如何感谢您的回答。
0赞
Remy Lebeau
4/10/2023
@block103 您正在创建内存泄漏,因为您正在覆盖指向 d 内存的指针。你根本不应该使用。new
new
1赞
Remy Lebeau
4/10/2023
#2
foo()
期望一个 ,但你正在传递它。因此,您需要传入一个 其值是您想要的整数,而不是指向整数的实际指针。int
char*
char*
#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));
}
评论
*(int*)b
reinterpret_cast<char *>(*b)