File Management Functions


GetShortPathName Function

Retrieves the short path form of the specified path.

For more information about file and path names, see Naming Files, Paths, and Namespaces.

Syntax

C++
DWORD WINAPI GetShortPathName(
  __in   LPCTSTR lpszLongPath,
  __out  LPTSTR lpszShortPath,
  __in   DWORD cchBuffer
);

Parameters

lpszLongPath [in]

The path string.

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming Files, Paths, and Namespaces.

lpszShortPath [out]

A pointer to a buffer to receive the null-terminated short form of the path that lpszLongPath specifies.

Passing NULL for this parameter and zero for cchBuffer will always return the required buffer size for a specified lpszLongPath.

cchBuffer [in]

The size of the buffer that lpszShortPath points to, in TCHARs.

Set this parameter to zero if lpszShortPath is set to NULL.

Return Value

If the function succeeds, the return value is the length, in TCHARs, of the string that is copied to lpszShortPath, not including the terminating null character.

If the lpszShortPath buffer is too small to contain the path, the return value is the size of the buffer, in TCHARs, that is required to hold the path and the terminating null character.

If the function fails for any other reason, the return value is zero. To get extended error information, call GetLastError.

Remarks

The path that lpszLongPath specifies does not have to be a full or long path. The short form can be longer than the specified path.

If the return value is greater than the value specified in cchBuffer, you can call the function again with a buffer that is large enough to hold the path. For an example of this case in addition to using zero-length buffer for dynamic allocation, see the Example Code section.

Note  Although the return value in this case is a length that includes the terminating null character, the return value on success does not include the terminating null character in the count.

If the specified path is already in its short form and conversion is not needed, the function simply copies the specified path to the buffer specified by lpszShortPath.

You can set lpszShortPath to the same value as lpszLongPath; in other words, you can set the output buffer for the short path to the address of the input path string. Always ensure cchBuffer accurately represents the total size, in TCHARs, of this buffer.

You can obtain the long name of a file from the short name by calling the GetLongPathName function. Alternatively, where GetLongPathName is not available, you can call FindFirstFile on each component of the path to get the corresponding long name.

Examples

For an example that uses GetShortPathName, see the Example Code section for GetFullPathName.

The following C++ example shows how to use a dynamically allocated output buffer.

//...
    long     length = 0;
    TCHAR*   buffer = NULL;

// First obtain the size needed by passing NULL and 0.

    length = GetShortPathName(lpszPath, NULL, 0);
    if (length == 0) ErrorExit(TEXT("GetShortPathName"));

// Dynamically allocate the correct size 
// (terminating null char was included in length)

    buffer = new TCHAR[length];

// Now simply call again using same long path.

    length = GetShortPathName(lpszPath, buffer, length);
    if (length == 0) ErrorExit(TEXT("GetShortPathName"));

    _tprintf(TEXT("long name = %s shortname = %s"), lpszPath, buffer);
    
    delete [] buffer;
///...

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinBase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode and ANSI namesGetShortPathNameW (Unicode) and GetShortPathNameA (ANSI)

See Also

File Management Functions
Naming Files, Paths, and Namespaces
FindFirstFile
GetFullPathName
GetLongPathName
SetFileShortName

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Community Content

pushedx
GetShortPathName path existence notes
This may seem obvious to some, but the path must actually exist to get the short path name. I thought this function was more of a utility function, but remembering back to DOS days, the files present result in different file names being generated. Quite a gotcha if you are not paying attention ;)
Tags : contentbug

mccaffjt
Short filename not always available

GetShortPathName fails to create a short filename if the file doesn't have a short file name. The default behavior in Windows NT, Windows 95, and Windows 98 is to automatically create short file names (8.3 format) for files with long names. You can turn this option off by using the "System Policy Editor" (Poledit.exe). Certain file systems also don't support creation of short names by default.

Is there a way to force the OS to create a short filename alias for a file of interest without having to change system policy?

Tags : contentbug

dmex
vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> Public Shared Function GetShortPathName(ByVal lpszLongPath As String, ByVal lpszShortPath As StringBuilder, ByVal cchBuffer As UInt32) As UInt32
End Function
Tags :

dmex
C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer);
Tags :

Page view tracker