# C++ Cast error

This topic is 2858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

class Testing{public:	void hook_Func(void)	{		(this->*orig_Func)();	}	static void (Testing::* orig_Func)(void);};void (Testing::* Testing::orig_Func)(void)=(void(Testing::*)(void))0x0A3DF50;

The compiler give me error C2440 :(
It can't covert 'int' to 'void (__thiscall Testing::* )(void)'

Help me

##### Share on other sites
Quote:
 Original post by codder88The compiler give me error C2440 :(It can't covert 'int' to 'void (__thiscall Testing::* )(void)'

As it should. Converting an arbitrary location in memory is incredibly dangerous and completely unreliable. Doing something like this is asking for trouble.

What does the number 0x0A3DF50 signify? How do you know that 0x0A3DF50 is the address of a function? If it happens to be a function, how can you guarantee that this address is the same across all systems and platforms? How can you guarantee that this magic number doesn't change when you change the code?

Did you mean to do this?
#include <iostream>using namespace std;struct Testing {	void foobar() {		cout << "foobar" << endl;	}		void hookFunc() {		(this->*origFunc)();	}		static void (Testing::* origFunc)();};void (Testing::* Testing::origFunc)() = &Testing::foobar;int main() {	Testing t;		t.hookFunc(); // Prints "foobar"}

##### Share on other sites
Quote:
Original post by _fastcall
Quote:
 Original post by codder88The compiler give me error C2440 :(It can't covert 'int' to 'void (__thiscall Testing::* )(void)'

As it should. Converting an arbitrary location in memory is incredibly dangerous and completely unreliable. Doing something like this is asking for trouble.

What does the number 0x0A3DF50 signify? How do you know that 0x0A3DF50 is the address of a function? If it happens to be a function, how can you guarantee that this address is the same across all systems and platforms? How can you guarantee that this magic number doesn't change when you change the code?

Did you mean to do this?
*** Source Snippet Removed ***

0x0A3DF50 is the address of a __thiscall function... i want to hook it

##### Share on other sites
What do you hope to accomplish by hooking an arbitrary memory location of your program?

##### Share on other sites
Quote:
 Original post by _fastcallWhat do you hope to accomplish by hooking an arbitrary memory location of your program?

No... In fact i inject my dll into another process and i need to hook that address...

##### Share on other sites
Member function pointers can be, and are in practise, more than one machine word in length.

Typically member functions are implemented as a free function with a "this" parameter explicitly included, you might be able to get that to work but that will not help if the function is virtual.

Other than that you're going to need to do some research on how to set up the call yourself. Its pretty simple if you are sure of the calling conventions involved and how the compiler accesses virtual functions (if necessary).

##### Share on other sites
Quote:
 Original post by rip-offMember function pointers can be, and are in practise, more than one machine word in length.Typically member functions are implemented as a free function with a "this" parameter explicitly included, you might be able to get that to work but that will not help if the function is virtual.Other than that you're going to need to do some research on how to set up the call yourself. Its pretty simple if you are sure of the calling conventions involved and how the compiler accesses virtual functions (if necessary).

I can't understand :(

##### Share on other sites
Then you won't be hooking this function any time soon.

Look, this is not trivial. If you don't understand the basics of what I'm talking about, then you cannot write it. I don't think anyone else here is interested in writing it, not without getting paid at least. I certainly am not.

You need to do some research. If you had pointed at some particular aspect of my post and said, "hey, I almost get what you mean, but I'm not sure about XXX" then I might be able to be more helpful.

As it was you just quoted my entire post as if you haven't a clue what it means. It would take too long to simplify such a complex topic to someone with an unknown grasp of the fundamentals.

##### Share on other sites
Quote:
 Original post by rip-offThen you won't be hooking this function any time soon.Look, this is not trivial. If you don't understand the basics of what I'm talking about, then you cannot write it. I don't think anyone else here is interested in writing it, not without getting paid at least. I certainly am not.You need to do some research. If you had pointed at some particular aspect of my post and said, "hey, I almost get what you mean, but I'm not sure about XXX" then I might be able to be more helpful.As it was you just quoted my entire post as if you haven't a clue what it means. It would take too long to simplify such a complex topic to someone with an unknown grasp of the fundamentals.

I only want to fix that compiler error...
The error is here
void (Testing::* Testing::orig_Func)(void)=(void(Testing::*)(void))0x0A3DF50;

I can't set to the member that address because it must be "Testing::*" but how can i do this?

• 32
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999483
×