Reputation: 1916
// SharpEngine.h
namespace SharpEngine {
class SharpInst {
public:
// Insert Game Engine Code.
// Use this format
// static __declspec(dllexport) type function(parameters);
static __declspec(dllexport) void saveGame(object_as_param_here)
};
}
Where it says 'object_as_param_here' I need to pass an object so that the function can access the object that contains data like level, experience, health, etc.
This is in a .dll as well, how would I make it so that I can use this with other code and still be able to call various objects?
Upvotes: 2
Views: 3316
Reputation: 4078
You can use a pointer as parameter, because the DLL is within the executable memory, so if you have the struct's address and prototype, you can access it directly from memory. I'll give you an example:
Suppose you have this simple prototype in your executable:
class Player
{
public:
int money;
float life;
char name[16];
};
You can just copy it to the DLL's source code, so you have a declaration and let the DLL know how to access the members when giving a pointer.
Then you can export the function to the executable, giving the example prototype:
static __declspec(dllexport) void saveGame(Player *data);
Now you can just call the DLL's function from the executable, like this:
Player *player = new Player;
player->money = 50000;
player->life = 100.0f;
saveGame(player);
Or if you don't use the player's class as a pointer in your executable code, you can still pass its address:
Player player;
player.money = 50000;
player.life = 100.0f;
saveGame(&player);
And in your saveGame
function, you would access the struct as a pointer:
data->money
Upvotes: 4