I have a function that resides in a class in an application my objective is to inject a dll into the target proccess and call that member function by its address.
Here is the function:
void GameAudio::StopAllSounds(void) // this is at address 0x004A0656
I have tried calling it like this
typedef void(__stdcall * caller)(void);
caller call = (caller)0x004A0656;
I have used everything: __stdcall , __cdecl, __thiscall you name it.
Class's non-static member function signature is different than the normal function.
For example,
class X {
public:
void foo ();
};
void foo ();
In above case X::foo() and ::foo() are different from each other. Remember that, X::foo() under the hood is somewaht like:
void X::foo (X* const this);
^^^^^ implicitly added
Thus what you are attempting to do would result in an undefined behavior.
If you insists to call the member function using its address, then better to make it a static member function.
class X {
public:
static void foo ();
};
void foo ();
Signature for X::foo() and ::foo() are same.
Edit: From your comment, it seems that you don't have the control over the source code. I would suggest to use the proper calling convention for the function signature instead of hard coded address. Pseudo code
typedef void (GameAudio::*caller_type)();
caller_type caller = &GameAudio::StopAllSounds;
GameAudio object;
(object.*caller)();
There is no syntax in C++ for what you are doing.
Note that the function could even be virtual, and so you'd be calling the wrong function.
The easiest way is to change it/ into/add a non member function, or a static member function, with an additional GameAudio* parameter. That function could be easily be called using a pointer-to function.
If you still want to call the member function directly your best bet is probably to use assembler. The problem then is the portability: your solution will depend on the compiler, SO, architecture and maybe even the ABI version.
Related
I use Visual Studio 2012 Express to debug a 64-bit app. Let's say both 'foo' and 'bar' are some member functions of a class C.
'foo' looks like:
void foo() {
bar(); // change to this->bar() works!
}
My program crashed because 'this' pointer is changed when it went inside 'bar'. The problem can be fixed by changing to 'this->bar()'.
Any idea how I should debug this problem? Thanks
You mean that the following code works?
void foo() {
this->bar(); // instead of bar()
}
So, presuming foo() is a member function that calls another member function bar() for the same object, check two things: (1) whether there is another non-member function bar() having same signature of member function bar(), if so exist, change the non-member function name op always qualify with this-> for member function call as a good programming practice (2) When you call foo() for some object instance or pointer to object instance, check whether the object has been allocated and initialized properly.
Basically, I have an already compiled a file that is written assembly; .obj file. It has an exported function f. In C++, I wrote a class and want to use the exported function f as a member function.
If it were to be used as a global function, I know that one just writesextern "C" f(). However, this doesn't work with a member function, at least I didn't figure out how to do it.
So, how do I do it?
Note: The function f is written properly. i.e. it takes into account the this pointer, etc. It should work correctly at assembly level.
If the function is written as a free function, you will have to declare it as a free function the way you're accustomed to. Just as you can't build a library with void foo() and then somehow declare it as void bar() and expect it to work, you can't take a free function and turn it into a member function. What you can do, though, is add an inline forwarder into your class like so:
struct S;
extern "C" void f( S * );
struct S {
void f() {
using ::f;
f(this);
}
};
That's the best you can do, as far as I'm aware.
void foo(int)
{
}
class X
{
void foo()
{
}
void bar()
{
foo(42);
// error: no matching function for call to 'X::foo(int)'
// note: candidate is:
// note: void X::foo()
// note: candidate expects 0 arguments, 1 provided
}
};
Why is C++ unable to call the free function (which is the only one with the correct signature)?
Because the two identifiers are defined in different scopes, and overload resolution only concerns about functions in the same scope. Once the compiler finds that the class has a foo, it stops climbing up to wider scopes (C++11 §3.4.1/1), so the free function foo is hidden.
You need to use a qualified name to refer to the global foo:
::foo(42);
The logical reason is Consistency.
Suppose as per the suggestion, compiler resolves foo(42) to
::foo(int).
Now after sometime, if you change X::foo() to X::foo(int) then
foo(42) will be resolved to X::foo(int). Which is not consistent.
That is the also the reason why derived class function hides base class function when there are similar names.
Such cases can be resolved in 2 ways;
(1) Give fully qualified name (e.g. ::foo(42))
(2) Use using utility; e.g.
void bar()
{
using ::foo;
foo(42);
}
A name in an inner scope hides names in outer scopes. It doesn't matter if it is a function or something else, or if you are in a class or a namespace.
Only if the name lookup finds several functions with the same name will the overload resolution kick in to try to select the one that is the best match for the call.
Really like your question. Also I could say, use this syntax:
::foo(42);
But I can say that in my opinion it's more elegant and good programming, set namespaces, so you can write something like this:
namespace MyNameSpace
{
void foo(int){}
class X
{
void foo(){}
void bar()
{
MyNameSpace::foo(42);
}
};
};
This is a good thing because Namespaces allow to group classes, objects and functions under a name.
PS: Then this help you to understand the meaning of write ::foo(42); when you haven't any namespace.
I cannot answer the why part of your question -- I do not know what was the rationale behind that in the language spec.
To call the global function in your example, use the :: syntax:
::foo(42);
The reason for this is the fact, that the compiler will look for a matching function name first, ignoring return values and parameters. When inside a class, it will try to look for a matching member there (in fact, it will look through all scopes going "upwards"; local scope(s), function scope, class scope, namespace scope, global scope, etc.).
X::foo is the first matching name. THEN (not before) it will try to pick the right overload (if there are multiple declarations) based on the parameters (which is the reason you can overload the same function with different parameters but not different return values only) and then it will check the return value (if there's any).
What does it means when I in my code declare something like :
Foo * foo();
What benefits it gives me ?
Ist not simple pointer variable as you see .
Also
What does it mean if I declare it in the c++ source above all methods for example :
// Test cpp class
.
#include "Foo.h"
Foo * foo();
Test::Test() {}
Test::GetFooMethods()
{
foo()->someMethod();
}
…
…
Foo * foo(); is a declaration to a function named foo which returns a pointer to a Foo object. It is not a function pointer.
The code Foo()->someMethod(); is probably a typo. You probably meant foo()->someMethod();. In this case, the code calls foo() which as stated above returns a pointer to a Foo object, and then you call the Foo::someMethod() method through that pointer.
If in fact you did mean Foo()->someMethod(); things get a little crazy. What this means is first a Foo object is default initialized on the stack. Then Foo::operator->() is called, returning some object which has a method named someMethod(), and that function is called. See why I guess that it was a typo? :)
EDIT:
To answer the last question in your post, when you declare a function without defining it, you are telling the compiler, "I've got a function named foo that takes no parameters and returns a pointer to a Foo, but I'm not going to tell you how that function is implemented yet." This allows you to write code that uses that function and the compiler will be happy. The linker however will be quite grumpy with you if you do not someplace provide a definition for this function.
Foo * foo();
This is not a function pointer. This is a declaration of a function named foo that takes no arguments and returns a pointer to a Foo.
A pointer to a function of this type would look like:
Foo* (*foo_ptr)() = foo;
Just like
int getX();
is a declaration of a function that returns an int, so too
Foo * foo();
is a declaration of a function that returns a Foo pointer;
What does it means when I in my code
declare something like : Foo * foo();
Foo * foo();
Very tough to say without knowing what is Foo.
If Foo is a type e.g. struct Foo{};, then this declares a function named 'foo' which returns a pointer to some type called Foo. This looks to be the case in your post based on your usage subsequently (and also the title of the post).
If Foo is a #define e.g. #define Foo 1, then this is not a declaration but an expression statement which multiplies 1 by the return value of 'foo' in case operator * is defined for the types of the operands involved.
What benefits it gives me ?
Tough to say again without knowing what Foo is.
Also What does it mean if I declare it
in the c++ source above all methods
Declaring 'foo' (if it is indeed a declaration) as high as possible in the translation unit means larger the tentative scope of the name 'foo'.
void f1(){g();} // not OK, 'g' not known
void f(){}
void g(){f();} // OK, f() is known
BTW, does Foo (caps F) have an overloaded operator ->?
Why having function in my class doesn't change size of this class? This info must be stored somewhere, but where?
You can think of a member function as being just like any other function, except that it has an extra, hidden parameter that takes a pointer to the instance on which the member function was called.
For example, this:
class C
{
void f(int i) { }
};
might be implemented (at least conceptually) as:
void C_f(C* this, int i) { }
If it was a const member function, then the hidden parameter would have the type const C* instead. Note that the situation isn't nearly this simple for virtual member functions.
The sizeof(TheClass) is affected only by the data members within the class, plus the vtable if there is any, plus padding bytes if there is any. So adding a nonvirtual function to your class does not affect its size. And if the class already contains a virtual function, adding a second one would not change sizeof(TheClass) either.
Me think (and I tend to be wrong most of the time) that if you only declare a non-virtual function in a class but no implementation the linker will probably remove it all together.
class Toto
{
int foo();
};
M.