Hi folks,

I have a problem with AfxMessageBox.

Consider the following snippet:


--------------SNIP------------------------
int i = 3;
char * Indice = new char[4];

_snprintf(Indice, 2, "%d", i);

AfxMessageBox(LPCTSTR(Indice));
--------------END SNIP--------------------

The MessageBox pops with '3' followed by some garbage.

_snprintf should put \0 at the end of converted string. Just to trust
nobody I've tried to add it by myself, too, but the garbage is still
present.

What I did wrong?

TIA

Jake

Re: Question about AfxMessageBox input type by Michael

Michael
Fri Jul 09 08:01:18 CDT 2004

Use wchar_t. Sure you got compilation errors, which is why you put the
LPCTSTR cast in. Can you really cast a char * to a wchar_t *? I think not.
Certainly you can to fool the compiler, but it doesn't convert anything for
you.

How about supplying the data type required by the API?

wchar_t *Indice = new wchar_t[4];
_snwprintf(Indice, 2, L"%d", i);

AfxMessageBox(Indice);
--

Michael Salamone [eMVP]
Entrek Software, Inc.
www.entrek.com



"Jake" <ciccio@ciccio.it> wrote in message
news:2l7g4dF9m8raU1@uni-berlin.de...
> Hi folks,
>
> I have a problem with AfxMessageBox.
>
> Consider the following snippet:
>
>
> --------------SNIP------------------------
> int i = 3;
> char * Indice = new char[4];
>
> _snprintf(Indice, 2, "%d", i);
>
> AfxMessageBox(LPCTSTR(Indice));
> --------------END SNIP--------------------
>
> The MessageBox pops with '3' followed by some garbage.
>
> _snprintf should put \0 at the end of converted string. Just to trust
> nobody I've tried to add it by myself, too, but the garbage is still
> present.
>
> What I did wrong?
>
> TIA
>
> Jake



Re: Question about AfxMessageBox input type by Jake

Jake
Fri Jul 09 08:16:21 CDT 2004

Michael J. Salamone [eMVP] wrote:

> Use wchar_t. Sure you got compilation errors, which is why you put the
> LPCTSTR cast in. Can you really cast a char * to a wchar_t *? I think not.

Surely not. But I haven't been able to find out what LPCTSTR type was.

> How about supplying the data type required by the API?

Sure. Unfortunately typing F1 didn't give any clue, and my crystal ball
was out of order :-)

Anyway, do you have some pointers about default types, so I can search
for them next time I find a strange type I don't understand?

Thanks for your answer

Jake

Re: Question about AfxMessageBox input type by Michael

Michael
Fri Jul 09 08:29:13 CDT 2004

You can right-click it in the eVC IDE and click "Go to Definition of
LPCTSTR".

You also have the header files to scan through (which is where you go when
you click "Go to Definition...".

Do you have MSDN Library? Enter LPCTSTR in the index.

There are samples all over the place, too.
--

Michael Salamone [eMVP]
Entrek Software, Inc.
www.entrek.com



"Jake" <ciccio@ciccio.it> wrote in message
news:2l7k58F9mgorU1@uni-berlin.de...
> Michael J. Salamone [eMVP] wrote:
>
> > Use wchar_t. Sure you got compilation errors, which is why you put the
> > LPCTSTR cast in. Can you really cast a char * to a wchar_t *? I think
not.
>
> Surely not. But I haven't been able to find out what LPCTSTR type was.
>
> > How about supplying the data type required by the API?
>
> Sure. Unfortunately typing F1 didn't give any clue, and my crystal ball
> was out of order :-)
>
> Anyway, do you have some pointers about default types, so I can search
> for them next time I find a strange type I don't understand?
>
> Thanks for your answer
>
> Jake