Printing variables

Hi there, I'm trying to learn WinAPI but I don't understand how I can print variables with TextOut(), I get the following error:
error C2664: 'TextOutW' : cannot convert parameter 4 from 'int' to 'LPCWSTR'

How do I print variables?
I hate WinAPI but...

BOOL TextOut(
__in HDC hdc,
__in int nXStart,
__in int nYStart,
__in LPCTSTR lpString,
__in int cbString
);

http://msdn.microsoft.com/en-us/library/dd145133(VS.85).aspx

It looks like from your error your printing out an integer? Cast it to an LPCTSTR

 
TextOut(getDC(hWnd), xpos, ypos, _itoa_s(Integer), strlen(_itoa_s(Integer)));
Last edited on
Use _itoa_s/_itow_s/_itot_s.
itoa isn't standard, _itoa_s is even less standard if that is possible
You can use sprintf which is standard to convert a number to a C string:
http://www.cplusplus.com/articles/numb_to_text/#stdio
I don't understand...

If I have a int called number that is 5.

1
2
3
int number = 5;

TextOut(hdc, 10, 10, WHAT?!?, 1);


What do I have to write in the WHAT?!? section? :)

What do I have to write in the WHAT?!? section?


Answer: A variable of the type specified in the function prototype.

Is...

int number;

a 'LPCTSTR'?

Answer: No!

Bazzy just told you what to do with sprintf. I could tell you more,
but it would be best for you to give this some thought yourself.
note:

sprintf works with char arrays, not TCHAR or WCHAR arrays.

If you are trying to be unicode friendly (you should), and if you must use sprintf, then specifically use the ANSI version of TextOut (ie: TextOutA)

1
2
3
4
5
6
7
8
int number = 5;

char mybuffer[10]; // make sure it's big enough -- buffer overflow = bad

sprintf(mybuffer,"%d",number);

 // note again:  TextOutA, not TextOut
TextOutA(hdc,10,10,mybuffer, strlen(mybuffer) );


For more info on being Unicode friendly in WinAPI, see this:

http://cplusplus.com/forum/articles/16820/
Topic archived. No new replies allowed.