How to pass an object in a .dll Class Method parameter?

时间:2022-04-22 16:58:47
// 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.

如果它说'object_as_param_here',我需要传递一个对象,以便该函数可以访问包含级别,经验,健康等数据的对象。

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?

这也是一个.dll,我将如何制作它以便我可以将其与其他代码一起使用并仍然可以调用各种对象?

1 个解决方案

#1


4  

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:

您可以使用指针作为参数,因为DLL位于可执行内存中,因此如果您拥有结构的地址和原型,则可以直接从内存中访问它。我举个例子:

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.

您可以将其复制到DLL的源代码,因此您有一个声明,让DLL知道如何在给出指针时访问成员。

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:

现在你可以从可执行文件中调用DLL的函数,如下所示:

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:

在saveGame函数中,您将访问结构作为指针:

data->money

#1


4  

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:

您可以使用指针作为参数,因为DLL位于可执行内存中,因此如果您拥有结构的地址和原型,则可以直接从内存中访问它。我举个例子:

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.

您可以将其复制到DLL的源代码,因此您有一个声明,让DLL知道如何在给出指针时访问成员。

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:

现在你可以从可执行文件中调用DLL的函数,如下所示:

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:

在saveGame函数中,您将访问结构作为指针:

data->money