Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between GetLongPathName and GetFullPathName in WinAPI?

For determining the canonical path to a file specified by relative path or a path containing \..\ in the middle, stackoverflow suggests using GetFullPathName() here or GetLongPathName() here.

What is the difference between these functions?

The intent is to get paths starting with the drive letter from relative paths (like ..\someDir\someFile.txt and someOtherDir\someFile.txt) and to eliminate extra \..\ from the paths (like C:\dirA\dirB\..\someFile.txt -> C:\dirA\someFile.txt).

like image 913
Serge Rogatch Avatar asked Jul 06 '15 14:07

Serge Rogatch


1 Answers

GetFullPathName resolves file names and relative path names to absolute paths, by prepending the current working directory of the calling process.

GetLongPathName only resolves short (8.3) names to long names.

Note that the latter requires disk access, so a relative path will be probably be resolved by using the current working directory, too.

tl;dr:
Call GetFullPathName to resolve a relative path to an absolute one.
Call GetLongPathName to resolve an absolute path that may contain a short (8.3) name to long name form.


Be careful:

Current working directory is a per-process resource, and can get changed e.g. by the standard file open dialog. I would use this only for resolving command line arguments that may be relative to the CWD the program was started in.

A long path name may not exist for every 8.3 named file.

like image 94
peterchen Avatar answered Nov 03 '22 02:11

peterchen