0

My application makes use of CopyFileExW for fast copying, and includes the LPPROGRESS_ROUTINE callback to track progress. Since I can copy files simultaneously, I need to keep track of file progress in a global sense.

To do this, I would use a QMap<QString, quint64> as a map of filepath / file hash and written bytes per file. In order to get the file path or file hash, I need to get the file path. One suggestion is to use GetFinalPathNameByHandleA which looks promising.

Problem:

It appears I do not have access to the GetFinalPathNameByHandleA function found in <fileapi.h> as seen in the image below:

enter image description here

How can I get a file path from a file HANDLE (in Windows spedcifically)?

fyi: Using Windows 10 x64 with MinGW 32bit compiler and Qt Creator 4.12

CybeX
  • 2,060
  • 3
  • 48
  • 115
  • What windows version are you running? GetFinalPathNameByHandleA should be there since Windows Vista... – Thrasher Aug 18 '20 at 11:18
  • *Since I can copy files simultaneously, I need to keep track of file progress in a global sense* - this is 100% design error – RbMm Aug 18 '20 at 11:24
  • @Thrasher Windows 10 x64 with static Qt 5.13.1 build – CybeX Aug 18 '20 at 11:27
  • no any problem get to file path by handle. but this is not need in your case. you have all context without this. you **already** have file path if you call `CopyFileExW` - so for what you need do this again ?! – RbMm Aug 18 '20 at 11:34
  • Maybe look into this [Update WINVER and _WIN32_WINNT](https://learn.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt?view=vs-2019). You should have this function available – Thrasher Aug 18 '20 at 11:34
  • 2
    The 32-bit MinGW is the worst possible compiler environment you could choose for Windows development. It comes with headers that haven't been updated in decades, and uses defaults as if we were still primarily be targeting Windows 95. Pretty much *any* other IDE/development platform is a better fit (just don't pick Code::Blocks for the same reasons). – IInspectable Aug 18 '20 at 14:04
  • 2
    `CopyFileEx()` allows you to pass user-defined data into the progress callback routine. You could use that feature to pass in a pointer to each `quint64` in your `QMap` so the callback can update them. You wouldn't need to obtain the file path at all, except for the call to `CopyFileEx()` itself. – Remy Lebeau Aug 18 '20 at 19:07
  • @IInspectable tthanks, will keep that in mind. The reason for chosing MinGW was since I need a portable application with statically compiled (and built in ) SSL which to this day I have been able to get right, see if you are interested https://stackoverflow.com/q/62180840/4628115 – CybeX Aug 19 '20 at 03:37
  • @RemyLebeau silly me - https://learn.microsoft.com/en-us/windows/win32/api/winbase/nc-winbase-lpprogress_routine#parameters has the `lpData` where I simply passed a pointer to *this. Will use a Pair setup to pass *this and the file path or file hash for each access, thank you – CybeX Aug 19 '20 at 03:43
  • Now, it seems your question is how to get the file path from the file handle. [If you need to do this on earlier releases of Windows, the following example obtains a file name from a handle to a file object using a file mapping object. It uses the CreateFileMapping and MapViewOfFile functions to create the mapping. Next, it uses the GetMappedFileName function to obtain the file name.](https://learn.microsoft.com/en-us/windows/win32/memory/obtaining-a-file-name-from-a-file-handle) – Strive Sun Aug 19 '20 at 06:53
  • Hi, have your question been solved? The reason why ·GetFinalPathNameByHandleA· cannot be found may be that the precompiled header of `MINGW` is too old, you can add `#define WINVER 0x0A00 #define _WIN32_WINNT 0x0A00` and try again. – Strive Sun Aug 26 '20 at 07:19

0 Answers0