Detecting if .exe is running from non-local storage
What is the recommended method for detecting that a Delphi .exe file is started from a remote network share?
I’d like to raise a warning to the user and request him to copy the file locally before using it.
Check the Application.ExeName or ParamStr(0) if it starts with \
I wonder if you can tell from command line, just a thought, I’m also curios
On Windows I use the API function “GetDriveType”, with my executable path, which can return the values: (DRIVE_REMOVABLE, DRIVE_FIXED, DRIVE_REMOTE, DRIVE_CDROM, DRIVE_RAMDISK). If it is a local harddrive, it returns DRIVE_FIXED.
GetDriveType seems like the best option, but I guess I have to check that it isn’t a local URL as well, as \localmachinec$binmy.exe would be valid, but I think GetDriveType will report this as Drive_Remote.
If you map a local folder as a network drive, it will still be reported as being remote.
Exactly – so I need to do additional checking to verify that the “remote path” actually is remote, and not a local reference. Testing what I can get from GetVolumePathName now.
GetVolumePathName:
\servernamesharefilename gives \servernameshare
mapping X: to gives \servernameshare gives X:
Now – how do I get X:’s actual URL?
WNetGetUniversalName doesn’t give me anything :/
Can you build your EXE with IMAGE_FILE_NET_RUN_FROM_SWAP? Then (theoretically) there’s no reason to copy it – it will be copied and cached locally by the system. Note that you need the same flag in all PE files used by the program, ie all DLLs and BPLs it uses too.
Well, the reason I want it to be copied, and deny running from the share, is that the net share is for retrieving the latest version. When someone runs from the share, I can’t even rename the .exe to force a replace of it.
But, there is always those fumblefinger people which keep making shortcuts from the share, instead of from the local copy.
It is a temporary problem as we are moving the .exe upgrade mechanism into db replicated files this fall, but it is an annoying problem.
Ah, fair enough. I was hoping I could solve your problem through something inbuilt without you having to write any code at all. The best code is no code :p
David Millington – Absolutely! – Alternatively, code you can reuse 🙂
Couldn’t you simply remove the read-execute flag from the security settings of the folder? You would miss that warning, though.
Uwe Raabe – Not easily. There are a multitude of things shared and a multitude of shares. Inherited designs…
I’m not sure folder redirects will get you the right volume type.
Jeroen Wiert Pluimers – GetDriveType will indicate anything that looks “mounted” as remote. My current challenge is to map drive name to share to see if I find the update server in the URL.
FYI: WGetNetConnection gave me what I needed to check if a drive was mapped remotely.
Rudimentary function to check if running from network drive:
http://pastebin.com/WW3gcbRq