C++ Cast error

Started by
10 comments, last by wqking 13 years, 8 months ago
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
Advertisement
Quote:Original post by codder88
The 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"}
Quote:Original post by _fastcall
Quote:Original post by codder88
The 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
What do you hope to accomplish by hooking an arbitrary memory location of your program?
Quote:Original post by _fastcall
What 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...
please help me :(
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).
Quote:Original post by rip-off
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).


I can't understand :(
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.
Quote:Original post by rip-off
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.


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?

This topic is closed to new replies.

Advertisement