views:

18

answers:

2

Hi,

Below code throws unhandled exception at wsprintf.

 #define FRONT_IMAGE_NAME "Image01front.bmp"
 void GetName(LPTSTR FileName)
 {

     wsprintf(FileName, "%s", FRONT_IMAGE_NAME);
 }

 int main()
 {

    GetName(FRONT_IMAGE_NAME);
    return 0;
 }

Please let me know why exception is generated at wsprintf.

Thanks.

A: 

You seem to write from FRONT_IMAGE_NAME to FRONT_IMAGE_NAME. You can't write to something that is not a buffer.

Dialecticus
A: 

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!

Lakshmi