Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add code in runtime

Tags:

c++

windows

I was searching through stackoverflow questions but none of them answered my question. I have a game engine and I want to load player AI (written in c++) in runtime.

  1. Click on button, file dialog appears
  2. Choose file with AI (.dll or something?)
  3. Click on 'start' button, game starts using AI's that I add.

AI could be a method or whole class, it doesn't matter. I think I should generate .dll but I not sure how to do that. This class should look like this:

class PlayerAI
{
    void computeSomething(list of argument, Object& output)
    {
        // some logic
    }
}
like image 797
Isaac Avatar asked Jan 18 '26 03:01

Isaac


1 Answers

Assuming pure Windows platform since none specified -

If you want to inject DLL, first obtain a handle to it using LoadLibrary-function like so:

HINSTANCE handleLib; 
handleLib = LoadLibrary(TEXT("YourDLL.dll")); 

You may then obtain a function pointer to a specific function in the lib. Like this:

FUNC_PTR func;
func = (FUNC_PTR) GetProcAddress(handleLib, "yourFunc");

Then you can call the function like so:

 (func) (L"TESTSTRING HERE"); 

When done, call FreeLibrary(libhandle)

How to declare a function as exported is in VS for instance like this (this is needed to mark your function in your DLL that you precompile:

__declspec(dllexport) int __cdecl yourFunc(LPWSTR someString)
{
   //Code here... 
}
like image 84
Richard Tyregrim Avatar answered Jan 19 '26 18:01

Richard Tyregrim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!