Online Update of a DLL application
I have a plug-in application which is a C++ DLL running inside a (foreign) C++ host.开发者_StackOverflow I want to allow online updates of the plug-in. How can I do that?
One possible way is to separate the DLL to a wrapper DLL and a content DLL and to move to a new content DLL on each update.
Is there any better way to achieve that?
Thanks
If you have no control over the host, then you have to do the dynamic load/unload on your own. As you correctly noticed, you will need two DLLs for this. That's probably the easiest way, but you need to make sure that the host cannot do any harm while you are exchanging the DLLs. The simplest solution is probably to load both DLLs and then perform an atomic switch between them before unloading the old code.
One problem is how to get the host application to restart/reload plugins, and how to get it to replace your DLL with another one.
Under Windows (assuming Windows, since you said DLL), you have an issue insofar as you cannot replace an executable image (this includes DLLs) with another while it is loaded to memory. Under POSIX, that would be no problem (and this even works reliably without any side effects!), but Windows will lock the image for exclusive access.
Therefore, you have no other choice than to unload your DLL first, then replace it, and load it again. This can be done as you described.
One alternative, if the host application allows that, would be to write a second independent plugin which tells the application to unload the to-be-updated one, then updates it, and then tells the host application to re-load it.
Yes, you can't update the DLL in use directly. The solution with two DLLs works but might be sophisticated. At least I wouldn't unload and load the new DLL while the application is running. Do the download and load the new DLL when the plug in is loaded the next time.
Also, you could have your updater be an EXE (and no second DLL) which is in the autostart folder. It downloads the file if there's an update and copies it to the correct location. If that fails due to ERROR_ACCESS_DENIED, the user has started the application already and your update will do the copying the next time the user is logging on. I think this is easier, sufficient (you have to decide that) and transparent for the user - he can remove the update from the autostart folder or start it manually.
精彩评论