Code: Show/Hide #ifndef ACTION_H
#define ACTION_H /** * File - Action.h * A very simple class which is used in my game. I need to be able to * export the enum ActionType, the class Action, and the 3 global << operator functions. */ #include <string> enum ActionType { ACTION_None, ACTION_Punch, ACTION_Kick, ACTION_Dive, ACTION_Jump, ACTION_Shoot }; class Action { private: ActionType action; std::string player; double amount; public: Action(){} Action( std::string NameOfPlayer, ActionType ActionByPlayer, double AmountOfForce = 0.0 ) { player = NameOfPlayer; action = ActionByPlayer; amount = AmountOfForce; } virtual ~Action(){} //Accessor Methods std::string getPlayerName() const { return player; } ActionType getAction() const { return action; } double getAmount() const { return amount; } bool operator==( const Action& act ); bool operator!=( const Action& act ); }; std::ostream& operator<<( std::ostream& out, const ActionType act ); std::ostream& operator<<( std::ostream& out, const Action& act ); std::ostream& operator<<( std::ostream& out, const Action* act ); #endif |
Code: Show/Hide #ifndef SPAWN_H
#define SPAWN_H #define STRING_CAST_CHAR #include "action.h" #include "..\dllcore.h" #include "..\clientprot.h" |
Code: Show/Hide // Bak
// Dll implementation // 12-12-05 #include <stdio.h> __declspec(dllexport) void myDllFunction() { printf("in dll!\n"); } |
Code: Show/Hide // Bak
// 12-12-05 // This program imports a dll and calls a function from it #include <stdio.h> #include <windows.h> int main() { const char* dllName = "dlltest.dll"; const char* functionName = "myDllFunction"; HMODULE module = LoadLibrary(dllName); // our function pointer for the function we defined in the dll void (*theDllFunc)() = 0; if (!module) { printf("%s not found or load failed.\n", dllName); exit(1); } // retrieve our function from the .dll by name theDllFunc = (void (*)())GetProcAddress(module, functionName); if (!theDllFunc) { printf("%s not found inside %s or load failed.\n", functionName, dllName); } else { // load was sucessful // we can now use the function pointer to call the function printf("hello dll!\n"); theDllFunc(); } // important, free the dll if (!FreeLibrary(module)) printf("error releasing the module loaded from %s\n", dllName); return 0; } |
Quote: |
To make it use classes, you may need to have an instance of the class defined and send a pointer to the class from the dll to the exe (as a void*). Then the exe will cast the pointer back to the class pointer type and should be able to run functions on it. |
Code: Show/Hide Action DecisionManager::getDecision( StatsDB* stats ) |
Code: Show/Hide const char* getPlayerName() const
{ return player.c_str(); } |
Code: Show/Hide Action DecisionManager::getDecision( StatsDB* stats )
{ Action a; dllFunction(&a, stats); return a; } |
Quote: |
Firstly, let me say you never want to return classes or structures (or pass them in as parameters for that matter). |
Quote: |
However, for your purpose I do not recommend this method. It looks like Action is just three pieces of data, a string, an int (the enum) and double. It would be much simpler to pass these as pointers to the dllFunction and have it modify the values based on stats. |
Bak wrote: |
ohhh... so it's not really a bot question ;)
basicly start a project as a win32 project VC++: |
Code: Show/Hide BOTAI_API Action getBotDecision(StatsDB* stats, bool verbose); |
Code: Show/Hide const char* dllName = "BotAI.dll";
const char* functionName = "getBotDecision"; HMODULE module = LoadLibrary(dllName); // our function pointer for the function we defined in the dll void (*theDllFunc)(StatsDB*,bool) = 0; if (!module) { cout << dllName << " not found or load failed.\n" << endl; Sleep( 10000 ); exit(1); } // retrieve our function from the .dll by name theDllFunc = (Action (*)(StatsDB*,bool))GetProcAddress(module, functionName); if (!theDllFunc) { cout << functionName << " not found inside " << dllName << " or load failed.\n" << endl; Sleep(10000); exit(1); } else { // load was sucessful // we can now use the function pointer to call the function return theDllFunc( stats, verbose ); } // important, free the dll if (!FreeLibrary(module)) cout << "error releasing the module loaded from " << dllName << endl; |
Code: Show/Hide void cpp_myDllFunction(void** myClassPtr)
{ *myClassPtr = new ChildClass; ChildClass* c = (ChildClass*)*myClassPtr; c->functionToOverload(); } extern "C" { __declspec(dllexport) void myDllFunction(void** myClassPtr) { cpp_myDllFunction(myClassPtr); } } |
Code: Show/Hide if (!theDllFunc)
{ cout << functionName << " not found inside " << dllName << " or load failed.\n" << endl; Sleep(10000); exit(1); } |