Copying a dll fails with error 32.

Ali AL-Darraji 0 Reputation points
2024-02-28T09:22:24.92+00:00

Edit:
all running code can be found here:
https://gist.github.com/Haiderahandali/6d39a601c547115e42c973e8796ed5d7

Original question:
I am trying to implement hot reloading in my 2D game engine, my engine is in main.cpp and my game is game.cpp and the way I do it is as follows: 1- I compile the game.dll and I generate a different pdb file (based on the current time) to avoid locking the pdb in the debugger.
2- I copy the game.dll to game0_.dll using CopyFile (this is used so a new compilation of game.dll does not fail because I am loading some code the library from it)
3- I load my game code from game0_.dll. marking LastWrite timestamp from GetFileAttributeEx

4- I check every frame if the last time the game.dll timestamp is different from my LastWrite.
5- I recompile my game.cpp code which overwrites the game.dll with the new code.6- after recompilation, the timestamps are different, so I unload the library (game0_.dll) Copy the game.dll to game0_dll and try reloading the game code from that library The copying of the file from game.dll to game0_.dll fails with error = 32 from GetLastError. Which is file is being used by another process.
I am unable to avoid this issue, I tried copying to a new file every frame (instead of game0_.dll, I change the name to game1_.dll and then game2_.dll and so on) but the same results happens, this suggests to me the issue is that somehow the compiler write the time stamps before it finishes compiling? otherwise why would game.dll being locked by another process since I am not using it in my game code to begin with?

a stripped version of source code can be found here:
https://pastebin.com/22Unqs4Z

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,585 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,690 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.