-2

I'm trying to get the name of a file using Win32 handle in C++.

I have two more concerns, the file might be empty (opening the file may not work) and I can't use all the fancy functions (e.g. GetFinalPathNameByHandle) which are supported by Windows Vista and above.

Thanks for the help!

Alex
  • 9,891
  • 11
  • 53
  • 87
igal k
  • 1,883
  • 2
  • 28
  • 57
  • 2
    What do you mean "Handler"? POSIX file descriptor? C stdio FILE? C++ iostreams fstream? Win32 API something? Qt something else? – Z.T. Apr 16 '12 at 06:23
  • 1
    I think he means a Win32 `HANDLE` receieved from `CreateFile`? – ta.speot.is Apr 16 '12 at 06:24
  • He should say that! And tag the question with `winapi`. It's not a C++ question, it's a Windows programming question. – Z.T. Apr 16 '12 at 06:26
  • I edited the question in order to clarify it. Unfortunately, I am still unable to provide an answer to it with the amount of informations you gave us. Could you also clarify what do you mean by "use all the fancy functions"? Do you mean that you cannot use STL? – ForceMagic Apr 16 '12 at 06:38
  • hey, fancy ways means i can't use GetFinalPathNameByHandle for example, handler means win32 handler, (C++ HANDLE type). as far as i understood using files mapping wont work since the file may be empty of content. – igal k Apr 16 '12 at 06:42
  • A `HANDLE` is a reference to something, a _handler_ is a processor, very different. – Peter Wood Apr 16 '12 at 06:50
  • Peter Wood: thanks for the *fix, i meant HANDLE – igal k Apr 16 '12 at 06:52
  • See http://stackoverflow.com/q/5193579/1084416 – Peter Wood Apr 16 '12 at 07:26

3 Answers3

0

I don't know win32, but I do know about sysinternals process explorer. Google came up with an open source alternative: http://processhacker.sourceforge.net/

Maybe studying that code will enable you do this.

Z.T.
  • 939
  • 8
  • 20
0

For XP+ you can map the file then call GetMappedFileName which will return the name (although not for directories). See Obtaining a File Name From a File Handle.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

GetMappedFilename() is not the best solution although recommended in the MSDN.

I have posted the code that does exactly what you want ready for copy and paste here: How to get name associated with open HANDLE

Community
  • 1
  • 1
Elmue
  • 7,602
  • 3
  • 47
  • 57