Calling game functions with string variables - c++

I am trying to call this function in a c++ dll:
public static void AskTrade(string traderPartnerId)
What I do to call this is:
auto Trade = reinterpret_cast<void(*)(std::string)>(BaseAddress + 0xDE54B0);
Trade(D39DSAR2);
It is inside of a special void that makes the calling work. Because when I call functions without any variables or functions with int as variables, it does work.
But when I try to call this, nothing happens. The game is c# and I code in c++, that must be the reason I guess. I don't know how to fix it though.
I would very much appreciate it if any of you would know and tell me a solution.

Related

convert double (class::*)(const gsl_vector*, void*) to double (*)(const_gsl vector*,void*) [duplicate]

So here's the situation: I'm using C++, SDL and GLConsole in conjunction. I have a class, SDLGame, which has the Init(), Loop(), Render() etc - essentially, it holds the logic for my game class.
GLConsole is a nice library so far - it lets me define CVars and such, even inside my SDL class. However, when defining commands, I have to specify a ConsoleFunc, which is typedef'd as
typedef bool (*ConsoleFunc)( std::vector<std::string> *args);
Simple enough. However, like I said, my functions are all in my class, and I know I can't pass pointer-to-class-functions as pointer-to-function arguments. I can't define static functions or make functions outside my class because some of these ConsoleFuncs must access class data members to be useful. I'd like to keep it OOP, since - well, OOP is nice.
Well, I actually have this problem "solved" - but it's extremely ugly. I just have an instance of SDLGame declared as an extern variable, and use that in my ConsoleFuncs/main class.
So, the question is: Is there a way to do this that isn't stupid and dumb like the way I am doing it? (Alternatively: is there a console library like GLConsole that supports SDL and can do what I'm describing?)
If the only interface you have is that function pointer, then you're screwed.
A member function needs a this pointer to be called, and if you have no way of passing that, you're out of luck (I guess the std::vector<std::string>* args pointer is what you get passed from the library).
In other words, even though that library uses C++ containers, it's not a good C++ library, because it relies on free functions for callbacks. A good C++ library would use boost::function or something similar, or would at the very least let you pass a void* user_data pointer that gets passed through to your callback. If you had that, you could pass the this pointer of your class, cast it back inside the callback, and call the appropriate member function.

Class member function cannot call another member function of same class?

I'm writing a private member function for a class (function1) and for a chunk of the code I decided it'd be easier to put that chunk into it's own function (function2) , and call it from function1. However, the compiler is saying that function2 needs an object. This doesn't make sense to me, because an object has to be created in order for function1 to work, so the object for function2 seems like it should just be "this".
int ClassName::function1(args)
{
//some code;
//some code;
function2(args); //says I need an object to call the function here
//some code;
}
int ClassName::function2(args)
{
//some code;
}
The exact error code is "A nonstatic member reference must be relative to a specific object". I've been searching for hours and every instance of this comes from someone calling a class member function from outside of the class without using a object pointer. I cannot find anything for a class member function calling another class member function. I also thought I had called member functions from other member functions in the past without trouble, so I'm super confused. I could just delete function2 and do all calculations inside function2, but then I wouldn't learn anything!
I tried switching the order of the function definitions in the cpp file. The order of the prototypes in the header. Changed function2 from private to public. No dice. I've exhausted google, my own programming knowledge, and any experimentation that I could think of, so I have to ask you guys for help now. If you need extra info, let me know, although it seems like the exact code inside my functions would be irrelevant as far as the problem goes.

Force error (compile time) if anyone calls a method in C++

DISCLAIMER: CCNode class is part of the cocos2d-x framework, which i didn't desing.
Base class CCNode has a init method:
virtual bool init();
My derived class needs two arguments, so I declare a new init method:
virtual bool init(int, int);
I'd like to enforce the use of the new init(int a, int) instead of the original one.
I know I have the option to call the new one with default parameters, but it doesn't feel right in the context.
I'm searching for a way to tell the user "Call init(int, int) instead" if anyone tries to call that one. I'd rather get that at compile time that at runtime.
I've tried C++11's static_assert(false, "message"), but fails without calling it...
If your really want to prevent someone calling the standard node method I think you should inherit it privately. However, the more cocosy way of doing this would simply be to call the new init from your create method, which is the only one that should be called by outside code when constructing your object anyway.
Sounds like you have source code access, since you tried sticking a static assert in there? The only way I think you can do exactly what you want is to templatize the function in question. Placing a static assert in a templatize function is a good way to ensure it doesn't compile.
Another option would be to hide the declaration in the private section of your class.
Lastly, a run-time assertion is the most common way I ever achieve what you're asking to do.
If you don't have source code access to that init function, then I really don't think you can do what you're asking.

C++ class with private global kernel methods

In relation to another problem I was experiencing with linking and multiple defined functions, I wanted to simply wrap my cuda code in a singleton class.
Something along
class Singleton{
public:
__host__ void doSomething();
private:
__global__ void someKernel();
};
That apparently only device methods may be used and the above gives "invalid combination of memory qualifiers". I would like to wrap my code in a class to get more structured code, but if I need to place every kernel externally, it makes little sense.
The same question has been posed previously on nvidias site, but without an answer
http://forums.nvidia.com/index.php?showtopic=176623
I too understand the problem with the this pointer, but even a static method cannot be global.
If you want calling code to look more organized, you could probably call your kernel from a method, if it helps.

Change the address of a member function in C++

in C++, I can easily create a function pointer by taking the address of a member function. However, is it possible to change the address of that local function?
I.e. say I have funcA() and funcB() in the same class, defined differently. I'm looking to change the address of funcA() to that of funcB(), such that at run time calling funcA() actually results in a call to funcB(). I know this is ugly, but I need to do this, thanks!
EDIT----------
Background on what I'm trying to do:
I'm hoping to implement unit tests for an existing code base, some of the methods in the base class which all of my modules are inheriting from are non-virtual. I'm not allowed to edit any production code. I can fiddle with the build process and substitute in a base class with the relevant methods set to virtual but I thought I'd rather use a hack like this (which I thought was possible).
Also, I'm interested in the topic out of technical curiosity, as through the process of trying to hack around this problem I'm learning quite a bit about how things such as code generation & function look-up work under the hood, which I haven't had a chance to learn in school having just finished 2nd year of university. I'm not sure as to I'll ever be taught such things in school as I'm in a computer engineering program rather than CS.
Back on topic
The the method funcA() and funcB() do indeed have the same signature, so the problem is that I can only get the address of a function using the & operator? Would I be correct in saying that I can't change the address of the function, or swap out the contents at that address without corrupting portions of memory? Would DLL injection be a good approach for a situation like this if the functions are exported to a dll?
No. Functions are compiled into the executable, and their address is fixed throughout the life-time of the program.
The closest thing is virtual functions. Give us an example of what you're trying to accomplish, I promise there's a better way.
It cannot be done the way you describe it. The only way to change the target for a statically bound call is by modifying the actual executable code of your program. C++ language has no features that could accomplish that.
If you want function calls to be resolved at run-time you have to either use explicitly indirect calls (call through function pointers), or use language features that are based on run-time call resolution (like virtual functions), or you can use plain branching with good-old if or switch. Which is more appropriate in your case depends on your specific problem.
Technically it might be possible for virtual functions by modifying the vtable of the type, but you most certainly cannot do it without violating the standard (causing Undefined Behavior) and it would require knowledge of how your specific compiler handles vtables.
For other functions it is not possible because the addresses of the functions are directly written to program code, which is generally on a read-only memory area.
I am fairly sure this is impossible in pure C++. C++ is not a dynamic language.
What you want is a pointer to a function, you can point it to FuncA or FuncB assuming that they have the same signature.
You cannot do what you want to do directly. However, you can achieve a similar result with some slightly different criteria, using something you are already familiar with -- function pointers. Consider:
// This type could be whatever you need, including a member function pointer type.
typedef void (*FunctionPointer)();
struct T {
FunctionPointer Function;
};
Now you can set the Function member on any given T instance, and call it. This is about as close as you can reasonably get, and I presume that since you are already aware of function pointers you're already aware of this solution.
Why don't you edit your question with a more complete description of the problem you're trying to solve? As it stands it really sounds like you're trying to do something horrible.
Its simple!
For
at run time calling funcA() actually results in a call to funcB().
write funcA() similar to following:
int funcA( int a, int b) {
return funcB( a, b );
}
:-)