Nish’s thoughts on Visual C++, C++/CX, C# and .NET

GetProcessImageFileName/QueryDosDevice trivia

If you’ve ever used GetProcessImageFileName, you’d have been a little stunned to see filenames like \Device\HarddiskVolume1\Program Files\Abc.exe – though when you think of it, it’s not all that surprising, because these are the real paths as far as the OS is concerned, all that kiddy-type C-drive, D-drive usage is mere eye-candy for us non-kernel-programmer types. Anyway, you can’t get a DOS drive name directly from this sort of path, instead you’d have to use QueryDosDevice with each drive letter from A to Z and if what it returns matches the path you have, you’ve got your drive letter. Here’s a little copy/paste-able function you can use :-

@Nish: thanks! I translated this snippet to Python using ctypes and it works like a charm🙂

@Chris: for some reason GetModuleFileNameEx is not always working for me… maybe it has something to do with this (from MSDN):

“If the module list in the target process is not yet initialized, or if the module list changes during the function call as a result of DLLs being loaded or unloaded, GetModuleFileNameEx may fail or return incorrect information. ”

Warning: this code needs some tweaking if you start application from a network drive. If application is started over the network (\\myserver\test.exe) then GetProcessImageFileName returns something like \Device\…\myserver\test.exe, and QueryDosDevice won’t help.