Jump to content
  • Advertisement
Sign in to follow this  
codder88

C++ Cast error

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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"
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
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 this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!