如何将整数转换为函数指针的地址 [英] How do i convert an integer to an address for a function pointer
问题描述
我正在尝试将整数转换为函数指针的地址。
我想加密指针,然后进行一些验证,之后我就是
会将指针反转为一个地址。可以做到这一点。下面
是我的一些代码。
typedef void(* funcptr)(void);
void tryme(void){};
void main()
{
char * buffer = new char [30];
//将tryme功能的地址保存到字符串中
itoa((unsigned int)(& tryme),buffer,16);
加密(缓冲区) );
//这里的一些代码
// retreive地址tryme函数
decryptbuffer = Decrypt(缓冲区) );
unsigned int x =(unsigned int)atoi(decryptbuffer);
funcptr restoreFuncPtr =(funcptr)x; //将int转换为funcptr
地址
}
}
I am trying to convert a integer to an address of a function pointer.
I want to encrypt the pointer and then do some validation, afterwards i
will decrpyt the pointer back to an address. Can this be done. Below
is some of my code.
typedef void (*funcptr)(void);
void tryme(void){};
void main()
{
char *buffer = new char[30];
// save address of tryme fuction into a string
itoa ( (unsigned int)(&tryme),buffer,16);
Encrypt(buffer);
//some code here
// retreive address tryme function
decryptbuffer = Decrypt(buffer);
unsigned int x = (unsigned int)atoi(decryptbuffer );
funcptr restoreFuncPtr = (funcptr)x; // convert int to funcptr
address
}
}
推荐答案
>可以这样做吗。
编译并运行你的代码,你会发现它。 />
Ben
>Can this be done.
Compile and run your code and you''d find out.
Ben
您好,
您的代码应该有效。
但是,使用reinterpret_cast<>很安全
查看以下代码片段
typedef int(* FP)(void);
int添加()
{
返回10;
}
main()
{
FP funPtr;
unsigned int num = reinterpret_cast< unsigned int>(add);
funPtr = reinterpret_cast< FP>(num);
printf("%d",funPtr());
}
Thanx,
Rama
Hi,
Your code should work.
But, using reinterpret_cast<> is safe
Checkout the following code snippet
typedef int (*FP)(void);
int add()
{
return 10;
}
main()
{
FP funPtr;
unsigned int num = reinterpret_cast<unsigned int>(add);
funPtr = reinterpret_cast<FP>(num);
printf("%d", funPtr());
}
Thanx,
Rama
ro************@gmail.com 写道:
我试图将整数转换为函数指针的地址。
我想加密指针,然后进行一些验证,之后我会将指针反转为一个地址。可以做到这一点。下面
是我的一些代码。
typedef void(* funcptr)(void);
void tryme(void){};
void main()
{char * buffer = new char [30];
//将tryme功能的地址保存到字符串中
itoa((unsigned int) (& tryme),缓冲区,16);
加密(缓冲区);
//这里的一些代码
// retreive地址tryme函数> decryptbuffer = Decrypt(缓冲区);
unsigned int x =(unsigned int)atoi(decryptbuffer);
funcptr restoreFuncPtr =(funcptr)x; //将int转换为funcptr
地址
I am trying to convert a integer to an address of a function pointer.
I want to encrypt the pointer and then do some validation, afterwards i
will decrpyt the pointer back to an address. Can this be done. Below
is some of my code.
typedef void (*funcptr)(void);
void tryme(void){};
void main()
{
char *buffer = new char[30];
// save address of tryme fuction into a string
itoa ( (unsigned int)(&tryme),buffer,16);
Encrypt(buffer);
//some code here
// retreive address tryme function
decryptbuffer = Decrypt(buffer);
unsigned int x = (unsigned int)atoi(decryptbuffer );
funcptr restoreFuncPtr = (funcptr)x; // convert int to funcptr
address
是什么让你认为指针的值将无符号整数内的FIT
?
你可能有64位指针和32位无符号整数,
那么呢?
HTH,
- J.
What makes you think the value of a pointer will FIT
inside an unsigned integer?
You might have 64-bit pointers and 32-bit unsigned ints,
what then?
HTH,
- J.
这篇关于如何将整数转换为函数指针的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!