Problems with GetShortPathName

Hey guys!

I am trying to get the 8.3 (short) filename of a long (standard) filename, because an old program that we are still using uses those short filenames. For some reason that I can not figure out, the following program is not working :

#include <tchar.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <string>

int main()
{
WCHAR nameBuffer[256];
WCHAR buffera[256];
FILE *pFile;
float error;
DWORD dw = GetLastError();

memset(nameBuffer, 0, sizeof(nameBuffer));
wcscpy_s(nameBuffer, _countof(nameBuffer), (const wchar_t *)"C:\\Documents and Settings\\STUDENT\\My Documents\\Dominique\\CLS\\July 2011\\FBPMe-b\\Analyse\\fbpme-b_1xx\\fbpme-b_104.txt");

error = GetShortPathName(nameBuffer, buffera, _countof(nameBuffer));

pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fprintf (pFile, "%s\n\n", nameBuffer);
fprintf (pFile, "%s\n\n", buffera);
fprintf (pFile, "%f\n", error);
fprintf (pFile, "%d\n", dw);
fclose (pFile);
}
return 0;
}

Each time, I get the following output in my write file :

C:\Documents and Settings\STUDENT\My Documents\Dominique\CLS\July 2011\FBPMe-b\Analyse\fbpme-b_1xx\fbpme-b_104.txt

ÿÿÿ

0.000000
0

Which indicates that the GetShortPathName did not work, since it returned 0 and weird characters instead of the short filename. The filepath is OK, I verified it a few times.

Any hints on how to solve this would be greatly appreciated!!
Thanks!
(const wchar_t *)"C:\\Documents..."

Don't cast around compiler errors!

"This is not a wchar_t string. The compiler error should tell you as much. If you try to cast to it, it will fail.";

If you want to make it a wide string, use the L prefix:

L"This is a wide string, and will work without casting."
Thanks for the quick reply!

You were right, I did but the L prefix in front of my string, and the compiler did not complain!
Now, it seems that I'm still stuck with the same problem. The output is now :

C

ÿÿÿ

0.000000
0


So it would seem that by adding the L prefix, it does not recognize my path name, and I still end up with GetShortPathName returning a 0, indicating that it does not work. I'm not sure that I made any progress...

Any more help would be really nice!
Thanks!
There are problems with your fprintf() calls or even with GetShortPathName() itself as you use wchar_t explicitly. If UNICODE is defined, then first fprintf() call will fail as the third argument is wchar_t.
Again, thanks for the replies!

I think that my UNICODE is defined, but just to make sure, how do I verify it? In case it makes any difference, I am using VS2008.

If the initialization as w_char is causing the issue, what should be the type of variable that I should use in order for it to work? I am still fairly new to C++, and those string/char types are really messing me up :(

Thanks!
Topic archived. No new replies allowed.