I would like to copy the string returned in FormatMessage() in the error array, but i only get garbage.
I dont know if the problem comes from the fact that FormatMessage() takes a tchar as a 5th parameter.
char * reportError()
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
cout << (char*)Buf << "\n";
char error ="";
strcpy(error, (char*) Buf);
LocalFree( Buf );
You're returning a local variable that goes out of scope when you leave the function. You need to dynamically allocate the memory into which you're copying the string.
Yes, you're right, thanks rags_to_riches.
Anyway I just went with the easier solution and declared it globally to solve the problem. :)
Not the right solution, but whatever. Globals are very, VERY rarely the right solution.
Thanks ill consider then to go the other way.
So i think in that case i could just return 'Buf', directly without assigning it, and then use LocalFree( Buf2 ); after i called the function , right?
Buf2 = reportError();
LocalFree( Buf2 );