0

Problem Description

I have an application that loads a dll at runtime. I want to allow a user to change code in that dll while the app is running. When the user clicks the "compile" button in the app it would free all the data from the dll. After freeing up the dll the app would then rebuild the dll. Essentially acting as a Run time compiled program.

Ideal Implementation

void ReCompile()
{
    //hDLL contains dll that was already loaded.
    FreeLibrary(hDLL);

    //code to rebuild MyDll.DLL would go here.

    LPCWSTR dll = L"MyDll.dll";
    hDLL = LoadLibrary(dll);
}

This currently works i just need to know how to rebuild the dll through code so it feels much more smooth and intuitive.

Current Implementation

void Free() 
{
    FreeLibrary(hDLL);
}

//I go into my other visual studio that's running and press the build button myself.

void ReloadDll()
{
    LPCWSTR dll = L"MyDll.dll";
    hDLL = LoadLibrary(dll);
}
Andrew Velez
  • 89
  • 1
  • 10
  • 2
    Include a compiler, linker and build tools with your application. Oh and an editor and possibly and IDE. – Richard Critten Mar 11 '17 at 01:33
  • @RichardCritten just need to build a C++ project into a dll. – Andrew Velez Mar 11 '17 at 02:05
  • 1
    Well, you could just call out to Visual Studio's command line for invoking a build. – TheUndeadFish Mar 11 '17 at 02:10
  • @TheUndeadFish currently at the point, something like "cl.exe /LD MyDll.cpp". There's an error i'm running into with includes though. I'm not sure how to tell the cmd prompt where to look for the files the dll needs. – Andrew Velez Mar 11 '17 at 02:14
  • ***There's an error i'm running into with includes though.*** If you want help you need to put the text of the error in your question. – drescherjm Mar 11 '17 at 05:04
  • 1
    Rather than working down at the level of cl.exe, you might consider researching how to invoke building the entire project in one go. I believe that would be through devenv.exe (and probably other ways too). – TheUndeadFish Mar 11 '17 at 05:30
  • @TheUndeadFish You are the man i got it to build in the cmd prompt. Lets see if i can get it to work through code now. – Andrew Velez Mar 12 '17 at 17:54

1 Answers1

0

Credit goes to TheUndeadFish.

void ReCompile()
{
    //hDLL contains dll that was already loaded.
    FreeLibrary(hDLL);

    //my solution
    system("msbuild \"D:\\Neumont\\Imagine\\RenderEngine\\Imagine.sln\" /property:Configuration=Debug /property:platform=Win32 ");
    //general purpose
    system("msbuild <path to your sln> /property:Configuration=build /property:platform=YourPlatform");


    LPCWSTR dll = L"MyDll.dll";
    hDLL = LoadLibrary(dll);
}

You need to add msbuild path to your enviorment variables. Mine was located in C:\Program Files (x86)\MSBuild\14.0\Bin. you might have to run vcvars32.bat

video of building DLL at runtime in action https://youtu.be/mFSv0tf6Vwc

Andrew Velez
  • 89
  • 1
  • 10