Serge Rogatch
Serge Rogatch

Reputation: 15070

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).

Upvotes: 7

Views: 5060

Answers (1)

peterchen
peterchen

Reputation: 41106

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.

Upvotes: 9

Related Questions