0

Whenever i use MessageBox function, i am getting first chance exception. My messagebox is like this.

MessageBox(NULL, (LPCWSTR)L"testing", (LPCWSTR)L"SOFTSAFETY", MB_OKCANCEL | MB_ICONWARNING);

If i debug, i am getting this

First-chance exception at 0x76267A24 (user32.dll) in Thread Message BOX.exe: 0xC0000005: Access violation reading location 0x001629D0.

First-chance exception at 0x76267A24 (user32.dll) in Thread Message BOX.exe: 0xC0000005: Access violation reading location 0x001629D0.

First-chance exception at 0x76267A24 (user32.dll) in Thread Message BOX.exe: 0xC0000005: Access violation reading location 0x001629D0.

First-chance exception at 0x76267A24 (user32.dll) in Thread Message BOX.exe: 0xC0000005: Access violation reading location 0x001629D0.

How can i remove those exceptions? My program is not suspended because of this exceptions, its just displaying in the output window. So can i neglect these. Please guide me.

Kumar
  • 616
  • 1
  • 18
  • 39

1 Answers1

1

Perhaps taking a look on the MSDN would help you? The MessageBox function has the following prototype:

int WINAPI MessageBox(
  _In_opt_  HWND hWnd,
  _In_opt_  LPCTSTR lpText,
  _In_opt_  LPCTSTR lpCaption,
  _In_      UINT uType
);

LPCTSTR is a pointer to TCHAR, and that is not necessarily a wide character. In wtypes.h, you will find:

const TCHAR *LPCTSTR

and TCHAR can be wchar_t or a char, depending on your project's settings. Your problem is almost certainly that you forced (via a cast) wide chars where regular ones were expected.

You can try using the _T() macro, to generate regular or wide string literals according to your project's configuration.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user2511124
  • 158
  • 5
  • 1
    All good advice, but don't neglect to point out that narrow ('regular') characters went obsolete for Windows programming over a decade ago. Anymore, *all* projects should have `UNICODE` and `_UNICODE` symbols defined to force everything to be wide characters. And lots of people go one step further, replacing all of the macro types with explicitly wide characters: `wchar_t`, `wchar_t*`, `L"..."` etc. – Cody Gray - on strike May 24 '14 at 03:26