psteiner
psteiner

Reputation: 187

Recompile MFC DLL while client exe is running

Is it possible to recompile an MFC DLL while its "client" executable is running, and have the executable detect and pick up the new changes? If it's possible, is it foolish? Being able to recompile the DLL without restarting the exe would save some time in my coding workflow. I am using Visual Studio 2008, code is written in native C++/MFC. My code changes are entirely contained in the DLL, not the EXE.

Thanks!

Upvotes: 2

Views: 369

Answers (2)

Jim Lamb
Jim Lamb

Reputation: 25805

Yes, it's possible. You'll need to make sure the executable explicitly loads your DLL (via LoadLibrary). If your executable implicitly loads your DLL you'll have the issues that Franci described.

To update the library while the executable is running:

  • Define some convention for staging the new version of the DLL. It could be in a separate folder, or with a different file name/extension.
  • Have a means of checking for a new version of the DLL. This could be in response to some specific gesture in the user interface, or you could monitor the directory for changes from a background thread.
  • When you see a new version, unload the old version (FreeLibrary), then delete it and move the new version to the desired location and reload it (LoadLibrary).

If your DLL implements any COM objects, let me know and I'll give you some additional tips.

Upvotes: 1

Franci Penov
Franci Penov

Reputation: 76021

Unfortunately, unless the executable has support for hot-swapping DLLs, you can't do it. The standard DLL loading mechanism in Windows will load it either at the start of the process or at first use of a function exported by the DLL and will not watch the file for changes in order to reload it. Also, depending on how the DLL is loaded, the file might be locked for changes.

You will have to stop your client executable before recompiling.

Upvotes: 2

Related Questions