From: Doug Harrison [MVP] on
On Tue, 21 Jun 2005 18:00:47 +0200, Mihajlo CvetanoviĆ½ wrote:

> But I haven't mentioned GetModuleFileName. If we want to do this "right"
> we have to make a function that'll access the version info resource by
> its module handle (basically all that GetFileVersionInfo does, except
> load/access the module by its name).

Yes, the FindResource stuff doesn't use the filename. What I was responding
to was this:

<q>
> So to avoid this we can't
> use GetFileVersionInfo, and instead we should use something that deals
> with HMODULE, like FindResourceEx-LoadResource.
</q>

Sorry if it wasn't clear I was thinking about the reliability of
GetFileVersionInfo when the filename was obtained from the HMODULE. It's a
safe bet the resources will remain good despite renaming the image file,
but I was wondering if the system keeps GetModuleFileName up to date as
well. Apparently, it does not, which is kinda bad.

--
Doug Harrison
Microsoft MVP - Visual C++
From: Alexander Grigoriev on
I think in NT+ OS you cannot rename a file if it's open without
FILE_SHARE_DELETE flag. Mapped executables and DLLs are opened without it.
It worked in Win9x, IIRC.

A big mistake of Microsoft was implementing /SWAPRUN flag for CD and network
files only, whereas the really useful feature would be having a file running
from swap no matter what media it's loaded from. This would solve the wicked
problem of deleting loaded executables.

"Mihajlo CvetanoviĆ½" <mac(a)RnEeMtOsVeEt.co.yu> wrote in message
news:O4ez75kdFHA.412(a)tk2msftngp13.phx.gbl...
> Joseph M. Newcomer wrote:
>> OK, I'm curious. How can you replace a currently-installed DLL? (Note
>> that I think I know
>> one trick, but I'm not sure it still works).
>>
>> Note that if the resources are in use, it is normally impossible to
>> replace a DLL.
>
> Suppose we want to update some example.dll (place another with the same
> name). Many processes have it loaded and we can't just call MoveFile on
> it. Suppose further that we choose not to use MoveFileEx with
> MOVEFILE_DELAY_UNTIL_REBOOT, which would be the logical thing to do (but
> requires administrator privileges).
>
> So, we do this (and I believe this is the trick you're referring to):
> rename example.dll to old_example.dll, copy there new example.dll, and
> tell each process to either reload the library, or to restart itself (if
> it's a service) or we just close the process and open it again. When all
> the processes load new example.dll we can safely delete old_example.dll.
>
> In the mean time some processes will have old_example.dll loaded, but if
> they call GetFileVersionInfo they will "think" they have new example.dll,
> which may have undesired results. So to avoid this we can't use
> GetFileVersionInfo, and instead we should use something that deals with
> HMODULE, like FindResourceEx-LoadResource.
>
> The point is that IF we can restart the computer OR we can stop all
> processes, replace the file, and start all processes OR there aren't any
> undesired results with GetFileVersionInfo THEN we don't need
> FindResourceEx.


From: Doug Harrison [MVP] on
On Tue, 21 Jun 2005 21:10:51 -0700, Alexander Grigoriev wrote:

> I think in NT+ OS you cannot rename a file if it's open without
> FILE_SHARE_DELETE flag. Mapped executables and DLLs are opened without it.
> It worked in Win9x, IIRC.

I tried it earlier today in WinXP. I wrote a console program that printed
its filename from GetModuleFileName(), slept for 20 sec giving me time to
rename the file in Explorer, and again printed its filename from
GetModuleFileName(). The rename was successful, but GetModuleFileName
didn't notice. :(

--
Doug Harrison
Microsoft MVP - Visual C++