2012-11-03 28 views
2
// 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) 
    }; 
} 

凡說「object_as_param_here」我需要傳遞一個對象,這樣的函數可以訪問包含類似水平,經驗,健康數據的對象,等如何在.dll類Method方法參數中傳遞一個對象?

這是一個.dll爲好,我怎樣才能使它與其他代碼一起使用,並仍能夠調用各種對象?

回答

4

您可以使用指針作爲參數,因爲DLL位於可執行內存中,所以如果您有結構的地址和原型,可以直接從內存訪問它。我給你舉個例子:

假設你有這個簡單的原型在可執行文件:

class Player 
{ 
public: 
    int money; 
    float life; 
    char name[16]; 
}; 

你只需將其複製到DLL的源代碼,讓你有一個聲明,讓DLL知道如何在提供指針時訪問成員。

然後你可以export the function to the executable,給人的例子原型:

static __declspec(dllexport) void saveGame(Player *data); 

現在你只需調用DLL的函數從可執行文件,如:

Player *player = new Player; 
player->money = 50000; 
player->life = 100.0f; 
saveGame(player); 

或者,如果你不使用玩家的類作爲你的可執行代碼中的指針,你仍然可以通過它的地址:

Player player; 
player.money = 50000; 
player.life = 100.0f; 
saveGame(&player); 

並且在您的saveGame函數中,您將訪問該結構作爲指針:

data->money 
+0

謝謝!這真的有幫助!教我很多! – Tux

相關問題