GetShortPathName() - displaying the path


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Displaying the short and long path name

To show: Using GetShortPathName()



// The GetShortPathName() example

#include <stdlib.h> // for MAX_PATH

// windows.h contain other def also, such as stdlib, malloc and tchar...

#include <windows.h>

#include <tchar.h> // for TCHAR

#include <stdio.h>

#include <malloc.h> // malloc()


int main(void)


DWORD retval = 0;

// testing Windows XP SP2 path, change accordingly...

// the path and the file are valid...

LPCWSTR szlongpath = L"C:\\Documents and Settings\\Johnny\\My Documents\\test.doc";

LPWSTR szshortpath = (LPWSTR) malloc(sizeof(MAX_PATH));

TCHAR buffer = MAX_PATH;


retval = GetShortPathName(szlongpath, szshortpath, buffer);


if (retval == 0)


printf("GetshortPathName() failed! Error : %d\n", retval);




// displaying short and long path

printf("The long path name = %S\n", szlongpath);

printf("The short path name = %S\n", szshortpath);

printf("The buffer length in TCHARs = %d\n", buffer);


if(szshortpath = NULL)

printf("memory freed!\n");



return 0;



Output example:


The long path name = C:\Documents and Settings\Johnny\My Documents\test.doc

The short path name = C:\DOCUME~1\Johnny\MYDOCU~1\test.doc

The buffer length in TCHARs = 260

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index