The function ::GetLongPathName()
is really two functions: ::GetLongPathNameA()
(ANSI) and ::GetLongPathNameW()
(Wide).
In the include file fileapi.h
there is some code:
#ifdef UNICODE
#define GetLongPathName GetLongPathNameW
#else
#define GetLongPathName GetLongPathNameA
#endif // !UNICODE
Only GetLongPathNameW()
handles the longer path names.
You need to ensure that "UNICODE" is defined, or else to specifically call GetLongPathNameW()
, not GetLongPathName()
I did some testing on my home LAN.
CALCITE
is an external hard disk. It runs some type of Unix/Linux variant but I haven't tinkered with it. It has an IP of 192.168.1.2
. I'm running the test on a Win7 Professional desktop using VC Express 2013.
#include <iostream>
#include <string>
#include <Windows.h>
void Test(const std::wstring &sName)
{
std::wcout << sName << L" ==> ";
const size_t nBuffsize = 1024;
wchar_t szBuff[nBuffsize] = { 0 };
if (::GetLongPathNameW(sName.c_str(), szBuff, nBuffsize))
std::wcout << szBuff << std::endl;
else
std::wcout << L"Error: " << ::GetLastError() << std::endl;
}
int main()
{
Test(L"\\\\CALCITE\\public\\x.txt");
Test(L"\\\\?\\UNC\\CALCITE\\public\\x.txt");
Test(L"\\\\?\\UNC\\192.168.1.2\\public\\x.txt");
Test(L"\\\\CALCITE\\public\\bad name.txt");
Test(L"\\\\CALCITE\\Bad path\\x.txt");
return 0;
}
The results:
\\CALCITE\public\x.txt ==> \\CALCITE\public\x.txt
\\?\UNC\CALCITE\public\x.txt ==> \\?\UNC\CALCITE\public\x.txt
\\?\UNC\192.168.1.2\public\x.txt ==> \\?\UNC\192.168.1.2\public\x.txt
\\CALCITE\public\bad name.txt ==> Error: 2
\\CALCITE\Bad path\x.txt ==> Error: 67
Error 2 is ERROR_FILE_NOT_FOUND
Error 67 is ERROR_BAD_NET_NAME