0

I have been wanting to make a game in C++ about programming little ships to fight together.

One of the things, though, is I wanted to be able to edit the game while it was running, instead of having to exit out and recompile the entire thing. My father told me that I can use a DLL to do this, by having the game copy the selected DLL file and run functions from that, then in the game when I press reload it will stop the script and copy in the new DLL and the continue the game.

To test this idea, I made a simple program and DLL project in Codeblocks. The console program called a function thisIsAFunction() from the DLL once, which would return a number to print to the console, like "1", then I would replace the DLL file with another DLL file that has the same function name but instead returns a different number to print to console.

It isn't working. Trying to replace the DLL file or the library.a file while the console application was running, Windows says it is open in another process.

Is there any way of accomplishing what I want to do? That is, to change a function return value during the program using DLL files or anything else?

EDIT: First of all thank you, Remy Lebeau, for helping fix all the grammar mistakes. I will be gathering the code soon to put here for reference but it seems like some of you have recommended using a scripting language instead which might work too. So thank you Ill be looking into that as well. So is it really worth it to go through all the trouble with DLLs or should I really just use something like Python or Lua?

  • It is rather difficult to tell what you have done wrong without having seen any code. Please post a [mcve]. – n. m. could be an AI Jun 18 '19 at 05:08
  • 1
    I'm quite sure you have to unload the DLL before the DLL file can be overwritten. This is what I recognize in VS in general: while a DLL is in use (in a running application) the file cannot be deleted or overwritten. I wonder about your concerns concerning the lib. For plug-ins, (which are loaded at runtime with `LoadLibrary()`), we extract the function pointers with `GetProcAddress()`. A lib file would be needed at compile time but the plug-ins are compiled/linked separately from executable (hence no lib file involved). – Scheff's Cat Jun 18 '19 at 05:35
  • I once wrote an answer for [SO: difference between Load DLL and Direct Call](https://stackoverflow.com/a/50894337/7478597) and a longer for [SO: GetProcAddress vs __declspec( dllimport )](https://stackoverflow.com/a/52341759/7478597) which might be of help. – Scheff's Cat Jun 18 '19 at 05:37
  • I think you would be better off with another approach, e.g. using LUA or some other engine for that part. see https://www.lua.org/ – AndersK Jun 18 '19 at 05:41
  • This might be interesting as well: [SO: Update a dll without stopping the service](https://stackoverflow.com/q/226581/7478597) (although it doesn't change what already has been stated). – Scheff's Cat Jun 18 '19 at 09:54
  • @Scheff: True, but .Net instead of C++. That makes it not a duplicate, – MSalters Jun 18 '19 at 12:28
  • @MSalters Not posted as possible duplicate - just as _interesting as well_. ;-) (I told a collaborator and he gave me the hint with shadow copying which I was not aware of.) – Scheff's Cat Jun 18 '19 at 12:31

1 Answers1

4

The idea is theoretically feasible, but...

You can't use the simple DLL support, as that will load thisIsAFunction() once. You need to reload that function after changing the DLL. That's a manual process. You will have to call LoadLibrary, GetProcAddress and FreeLibrary.

Also, besides functions, you will need to make sure that all objects created by the DLL are gone. Since you unload the DLL, you unload all destructors in there as well, so all objects needing those destructors have to be destroyed while the destructor is still loaded.

MSalters
  • 173,980
  • 10
  • 155
  • 350