Unhandled exception using wsprintf
Below code throws unhandled exception at wsprintf.
#define FRONT_IMAGE_NAME "Image01front.bmp"
void GetName(LPTSTR FileName)
{
wsprintf(FileName, "%s", FRONT_IMAGE_NAME);
}
开发者_Go百科int main()
{
GetName(FRONT_IMAGE_NAME);
return 0;
}
Please let me know why exception is generated at wsprintf.
Thanks.
You seem to write from FRONT_IMAGE_NAME to FRONT_IMAGE_NAME. You can't write to something that is not a buffer.
LPTSTR is a typedef. An LPTSTR actually is a TCHAR*, which depending on whether UNICODE is defined maps to either char* or wchar_t*.
You need to initialize your LPTSTR to sufficient size for the string you want to return. You do this in two ways, on the stack or on the heap (with new): On the stack: TCHAR FileName[50]; wsprintf(FileName, "%s", FRONT_IMAGE_NAME);
On the heap: LPTSTR FileName= new TCHAR[50]; wsprintf(FileName, "%s", FRONT_IMAGE_NAME); delete[] FileName; // DO NOT FORGET THIS!
精彩评论