469,636 Members | 1,544 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

How do i convert an integer to an address for a function pointer

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

}

}

Mar 16 '06 #1
10 5225
>Can this be done.

Compile and run your code and you'd find out.

Ben
Mar 16 '06 #2
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

Mar 16 '06 #3
ro************@gmail.com wrote:
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


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.
Mar 16 '06 #4
posted:
I am trying to convert a integer to an address of a function pointer.

typedef unsigned long PointerNumericValue;
void* Encrypt( void* p )
{
PointerNumericValue value = reinterpret_cast<unsigned long>(p);

//Now encrypt it:

p << 2;

p += 3;

//Now return it:

return reinterpret_cast<void*>(p);
}
Mar 16 '06 #5
Then u can take ,

unsigned _int64 (or)
long long unsigned int
........ here 64-bit pointers may fit.

:-)
Thanx,
Rama

Mar 16 '06 #6
Ramki wrote:
Then u can take ,

unsigned _int64 (or)
long long unsigned int
........ here 64-bit pointers may fit.

:-)


Yes, if you have stuff like "_int64". That, however,
raises the question of what happens if pointers are
bigger still, or ints are 16-bit?

What I meant to say was, you can't assume a 1:1
relationship between the size of a pointer and
sizeof(unsigned int).

- J.
Mar 16 '06 #7
Tomás wrote:
posted:

I am trying to convert a integer to an address of a function pointer.




typedef unsigned long PointerNumericValue;


void* Encrypt( void* p )
{
PointerNumericValue value = reinterpret_cast<unsigned long>(p);

//Now encrypt it:

p << 2;

p += 3;

//Now return it:

return reinterpret_cast<void*>(p);
}


Still, what if it doesn't fit inside an unsigned long?,
although it's the OP's problem.

- J.
Mar 16 '06 #8
On 16 Mar 2006 01:57:57 -0800, "Ramki" <wr*******@gmail.com> wrote in
comp.lang.c++:
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()
Your program is ill-formed, implicit int is no valid in C++, all
function definitions and declarations must specify a return type.
{
FP funPtr;
unsigned int num = reinterpret_cast<unsigned int>(add);
funPtr = reinterpret_cast<FP>(num);
printf("%d", funPtr());
}
Thanx,
Rama


Your compiler is non conforming, or you are not invoking it in
conforming mode, if it accepts this program without issuing a
diagnostic for the implicit int in the definition of main().

It is also non-conforming if it accepts the application of
reinterpret_cast on a function pointer type to any scalar type. This
is just plain not valid C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 17 '06 #9
Me
ro************@gmail.com wrote:
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.
Not guaranteed to work at all (see J.5.7 in the C standard)
typedef void (*funcptr)(void);
void tryme(void){}

<snip>

funcptr fn = tryme;
unsigned char buf[sizeof(fn)];
memcpy(buf, &fn, sizeof(fn));
encrypt_inplace(buf, sizeof(fn));

....

decrypt_inplace(buf, sizeof(fn));
memcpy(&fn, buf, sizeof(fn));
fn();

Mar 17 '06 #10
Thanks alot the method below worked.

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());
}

Thank you,

Rodrigo

Mar 17 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Kay | last post: by
16 posts views Thread by jose_luis_fdez_diaz_news | last post: by
3 posts views Thread by Mike Miller | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
8 posts views Thread by =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post: by
8 posts views Thread by Frank Liebelt | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.