0

Problems using GetShortPathName

I'm using the UNC prefix for the Path:'\\?\unc\heathers-bill\D$\aFileWithALongName.exe'

GetShortPathName returns 0 & using GetLastError() returns the error 'The filename, directory name, or volume label syntax is incorrect. ' even though Windows Explorer can access the same path

Since I can navigate to this path using Windows Explorer, why does GetShortPathName fail?

I'm using the Win32 API as documented at:
1.http://msdn2.microsoft.com/en-us/library/aa364989.aspx
2.http://msdn2.microsoft.com/en-us/library/aa365247.aspxas follows:

DWORD BuffSize = MAX_PATH;
		ifstream ip;

		ip.open("file.txt");
		if(!ip) { // file that stores Path to convert to Short Path couldn't be opened
			cerr << "Error: file could not be opened" << endl;
			exit(1);
		}

		wchar_t fileName[1000],*pwchar;
		char fileNameChar[700];
		pwchar = fileName;
		ip.getline(fileNameChar,1000);

		size_t result = mbstowcs(pwchar,fileNameChar,700);
		ip.close();
		
		std::wstring strUNCFilePath(fileName);
		std::wstring ws,ShortFileName;
		std::wstring::size_type pos;
		DWORD rc;
                  
                ws = strUNCFilePath;
		for( int i = 0; i < 2; ++i )
		{
			if (ws.length() >= MAX_PATH)
				BuffSize	= strUNCFilePath.length(); 	
			
			auto_ptr<wchar_t> apBuf( new wchar_t[BuffSize]);
			rc = ::GetShortPathName((LPCTSTR)strUNCFilePath.c_str(), (LPTSTR)apBuf.get(), BuffSize );
			if( rc == 0 )
			{
				ofstream err;
					
				DWORD ErrCode = ::GetLastError();
				err.open("error.txt");
				
				err << hex<< ErrCode << "\n";
				err.close();
				break;
			}
			if( rc <= BuffSize )
			{
				ShortFileName = std::wstring( apBuf.get(), rc );
				break;
			}
			BuffSize = rc;
		}

ALSO - Is there a way to use GetShortPathName() for paths longer than MAX_PATH?

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Walter.Worhol
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.