Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I load a dll in such a way that it can be deleted while it's loaded?

The title pretty much says it all..

What I'm trying to do is write a tool that will monitor a dll file containing a plugin and when I overwrite it, by recompiling, it should automatically reload it. I know I can make a copy, load the copy and monitor the original, but I thought there might be a better way.. If I understood it right the dll is completely loaded into memory so there shouldn't be a problem in deleting the file..

like image 481
Roald Avatar asked Sep 25 '11 16:09

Roald


People also ask

Can .dll files be deleted?

Right-click on the . dll file in the search results, and select "Delete" from the options that pop up. This will move the file to the Recycle Bin.

What happens if you delete a DLL file?

dll Files are installed by software programs while they are installed These files contain code that tells programs how to operate. If you delete . dll files programs may not work properly. We suggest you not to delete these files as they may cause serious issues with the proper functioning of the computer.

Can you load a DLL twice?

A dll can only be loaded into a processes memory once.

What happens when a DLL is loaded?

Every process that loads the DLL maps it into its virtual address space. After the process loads the DLL into its virtual address, it can call the exported DLL functions. The system maintains a per-process reference count for each DLL. When a thread loads the DLL, the reference count is incremented by one.

Can a DLL unload itself?

If your asking if you can safely unload/unmap a DLL loaded in a process from code in the DLL itself, the answer is no - there isn't really a safe way to do this. Think about it this way: Unloading a DLL is done by decrementing it's reference count using FreeLibrary().

Can a DLL load another DLL?

You can use load-time dynamic linking or run-time dynamic linking in your DLL in the same way as in the executable. The only restriction is not to call LoadLibrary from your DllMain function to avoid deadlocks.


1 Answers

No, that's not how Windows works. Loading a DLL merely creates a memory mapped file, nothing is actually read from the file beyond the relocations (if necessary). Not until your code calls an exported function. Which causes a page fault because the code wasn't loaded yet. Now the code gets read from the file into RAM. If space is needed for other processes then the pages simply gets unmapped. Getting reloaded again on the next page fault.

The MMF puts a hard lock on the file. You can only rename it, not overwrite or delete it. That would crash the program. Release the lock with FreeLibrary().

like image 160
Hans Passant Avatar answered Sep 30 '22 04:09

Hans Passant