GetShortPathNameW problem

Hi all,
im strucked with GetShortPathNameW ,it returns 0

here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wstring tempws = L"\\\\?\\";
	wstring filep(pchar);
	if (filep.find(L"\\\\")==0)
	{
		tempws.append(L"UNC\\");
		tempws.append(filep.substr(2,filep.size()-1));
	}
	else
	{
		tempws.append(pchar);
	}
	wchar_t *str= new wchar_t[tempws.size()+2];
	int newlen=GetShortPathNameW(tempws.c_str(),str,tempws.size()+2);
	char* shortstr=new char[newlen + 1];
	shortstr[newlen]='\0';
	wcstombs(shortstr, str, newlen);


i dont know where i have went wrong please advise me to fix the problem

waiting for your reply.............
if (filep.find(L"\\\\")==0)
should probably be:
if (filep.find(L"\\\\")==std::wstring::npos)

Having said that, I don't really know what you're trying to do so I can't comment on the rest of the code.
thanx for your reply,

i need to get short path of the given path hence im using GetShortPathNameW
Last edited on
You're right about the check for a UNC name.

I'm not on a Windows box right now, so I can't test it. But try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std:wstring GetShortName(const std::wstring &MAX_PATH)
{
    size_t mxlen = 32767;
    wchar_t* shortpath = new wchar_t[mxlen];
    *shortpath = 0;

    std::wstring local_longpath;
    if (longpath.find(L"\\\\") = 0)
        local_longpath = longpath;
    else
        local_longpath =  = L"\\\\?\\" + longpath;

    DWORD dwLen = ::GetShortPathW(local_longpath.c_str(), shortpath, mxlen);

    std::wstring out(shortpath);
    delete [] shortpath;
    return out;
}
Last edited on
closed account (z05DSL3A)
im strucked with GetShortPathNameW ,it returns 0
If it returns zero (0) then call GetLastError to find out what the error was.
Topic archived. No new replies allowed.