473,714 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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),bu ffer,16);
Encrypt(buffer) ;

//some code here

// retreive address tryme function
decryptbuffer = Decrypt(buffer) ;
unsigned int x = (unsigned int)atoi(decryp tbuffer );
funcptr restoreFuncPtr = (funcptr)x; // convert int to funcptr
address

}

}

Mar 16 '06 #1
10 5649
>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_cas t<> is safe

Checkout the following code snippet

typedef int (*FP)(void);
int add()
{
return 10;
}

main()
{
FP funPtr;
unsigned int num = reinterpret_cas t<unsigned int>(add);
funPtr = reinterpret_cas t<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),bu ffer,16);
Encrypt(buffer) ;

//some code here

// retreive address tryme function
decryptbuffer = Decrypt(buffer) ;
unsigned int x = (unsigned int)atoi(decryp tbuffer );
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 PointerNumericV alue;
void* Encrypt( void* p )
{
PointerNumericV alue value = reinterpret_cas t<unsigned long>(p);

//Now encrypt it:

p << 2;

p += 3;

//Now return it:

return reinterpret_cas t<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 PointerNumericV alue;


void* Encrypt( void* p )
{
PointerNumericV alue value = reinterpret_cas t<unsigned long>(p);

//Now encrypt it:

p << 2;

p += 3;

//Now return it:

return reinterpret_cas t<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*******@gmai l.com> wrote in
comp.lang.c++:
Hi,
Your code should work.
But, using reinterpret_cas t<> 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_cas t<unsigned int>(add);
funPtr = reinterpret_cas t<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_cas t 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.l earn.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
79605
by: Kay | last post by:
how to convert int to char * ? I have found this in a web site. However, it doesn't work even I change itoa to atoi including stdlib.h char str; int i=567; str=itoa(i, str, 10);
16
5842
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program compiles fine. What is the warning shown ?
3
9327
by: Mike Miller | last post by:
What is the best way to convert a managed unsigned int64 to an unsigned long? Basically I need to do the following: System::UInt64 managedInt = 10; unsigned long unmanagedInt; unmanagedInt = managedInt; Also it would be nice to know how to do this: System::UInt64 managedInt;
28
2440
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions 1: Why cannot I do the following:
12
2622
by: Abhishek | last post by:
now suppose I have declared an integer value inside a function as int x; now if the return type of the function is of type (void *) then can I write return((void *)x) in side the function? I came across this in a document on multithreading in C..I can post the exact portion of code which works correctly with such an assignment if you people want. Please clarify my doubt.I hope I have conveyed it properly.
8
8430
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer = "aabbccdd"; where aa is the LSB and dd is the MSB.
232
13286
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
8
6271
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get the following compiler error from Visual C++ 2005: error C2664: 'RasDialW' : cannot convert parameter 6 from 'cli::interior_ptr<Type>' to 'HRASCONN *'
8
16249
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc = {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
0
8815
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8713
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9318
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9187
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.