Board index » Visual Studio » What the L? in wsprintf

What the L? in wsprintf

Visual Studio223
Hi,



I'm programming in Visual C++ 2005 Express Edition and am having a problem

with wsprintf. I'm trying to display a variable value on screen, here is my

code:



wsprintf(pelement->string, L"%d", (UINT32)val);



Only the most significant character shows up. L"%2d" or L"%3d" doesn't

really help. If I use just "%3d" without the L it just displays %3d on the

screen. Text works fine with the line:



wsprintf(pelement->string, L"%s", "test");



Others I have talked to don't even use the L, so why do I seem to need it.

Why does it not display integers correctly? And what does it mean?



Thomas


-
 

Re:What the L? in wsprintf

"Thomas Magma" <somewhere@overtherainbow.com>wrote in message

Quote
Hi,



I'm programming in Visual C++ 2005 Express Edition and am having a problem

with wsprintf. I'm trying to display a variable value on screen, here is

my code:



wsprintf(pelement->string, L"%d", (UINT32)val);



Only the most significant character shows up. L"%2d" or L"%3d" doesn't

really help. If I use just "%3d" without the L it just displays %3d on the

screen. Text works fine with the line:



wsprintf(pelement->string, L"%s", "test");



Others I have talked to don't even use the L, so why do I seem to need it.

Why does it not display integers correctly? And what does it mean?



L"" defines a wide-character (i.e. Unicode) string literal, while "" defines

an MBCS (i.e. ANSI) string literal.



Whether you need to use a wide-character string in a call to wsprintf

depends on whether you're doing a UNICODE build or an ANSI build. The

easiest way to get the right kind of string literal when working with TCHAR

and related functions (like wsprintf) is to use the _T macro:



wsprintf(pelement->string, _T("%d"), (UINT32)val);

wsprintf(pelement->string,_T("%s"), _T("test"));



By the way, how is pelement->string declared?



Finally, consider not using wsprintf: it's vulnerable to buffer overrun

problems and is non-standard. Consider using _sntprintf or _sntprintf_s

instead.



-cd





-

Re:What the L? in wsprintf





Quote
>

>Others I have talked to don't even use the L, so why do I seem to need

>it. Why does it not display integers correctly? And what does it mean?



L"" defines a wide-character (i.e. Unicode) string literal, while ""

defines an MBCS (i.e. ANSI) string literal.



Whether you need to use a wide-character string in a call to wsprintf

depends on whether you're doing a UNICODE build or an ANSI build. The

easiest way to get the right kind of string literal when working with

TCHAR and related functions (like wsprintf) is to use the _T macro:



wsprintf(pelement->string, _T("%d"), (UINT32)val);

wsprintf(pelement->string,_T("%s"), _T("test"));



By the way, how is pelement->string declared?



Finally, consider not using wsprintf: it's vulnerable to buffer overrun

problems and is non-standard. Consider using _sntprintf or _sntprintf_s

instead.





Here is a little background on what I'm doing. I am programming a aircraft

panel gauge for MS Flight Simulator. The wsprintf is used in a callback

routine set up by a FS macro. The callback looks like this:



FLOAT64 FSAPI update_callback(PELEMENT_STRING pelement ){

FLOAT64 val=pelement->source_var[0].var_value.n;

wsprintf(pelement->string, L"%d",(UINT32)val);

return val;

}



I've presented this problem to the FS programming forums and no one even

uses the L. Their routines look exactly like mine but use "%3d" instead of

L"%d", but that doesn't work for me.



I tried what you suggested and here are my results. When I use:

wsprintf(pelement->string, _T("%d"), (UINT32)val);



I get "warning C4013: '_T' undefined; assuming extern returning int"



When I use:

_sntprintf(pelement->string, _T("%d"), (UINT32)val);



I get:

warning C4013: '_sntprintf' undefined; assuming extern returning int

warning C4013: '_T' undefined; assuming extern returning int



Am I not setting something right in my programming enviroment?



Thomas















-

Re:What the L? in wsprintf



Quote


Here is a little background on what I'm doing. I am programming a aircraft

panel gauge for MS Flight Simulator. The wsprintf is used in a callback

routine set up by a FS macro. The callback looks like this:



FLOAT64 FSAPI update_callback(PELEMENT_STRING pelement ){

FLOAT64 val=pelement->source_var[0].var_value.n;

wsprintf(pelement->string, L"%d",(UINT32)val);

return val;

}



I've presented this problem to the FS programming forums and no one even

uses the L. Their routines look exactly like mine but use "%3d" instead of

L"%d", but that doesn't work for me.



I tried what you suggested and here are my results. When I use:

wsprintf(pelement->string, _T("%d"), (UINT32)val);



I get "warning C4013: '_T' undefined; assuming extern returning int"



When I use:

_sntprintf(pelement->string, _T("%d"), (UINT32)val);



I get:

warning C4013: '_sntprintf' undefined; assuming extern returning int

warning C4013: '_T' undefined; assuming extern returning int



Am I not setting something right in my programming enviroment?



Thomas





Also, I'm getting a warning when compiling which points toward the wsprintf

line:

wsprintf(pelement->string, L"%d", (UINT32)val);

Output:

warning C4133: 'function' : incompatible types - from 'PCHAR' to 'LPWSTR'



Could there a problem with pelement->string?



Magma





-

Re:What the L? in wsprintf

"Thomas Magma" <somewhere@overtherainbow.com>wrote in message

Quote
I've presented this problem to the FS programming forums and no one even

uses the L. Their routines look exactly like mine but use "%3d" instead of

L"%d", but that doesn't work for me.



I tried what you suggested and here are my results. When I use:

wsprintf(pelement->string, _T("%d"), (UINT32)val);



I get "warning C4013: '_T' undefined; assuming extern returning int"



When I use:

_sntprintf(pelement->string, _T("%d"), (UINT32)val);



I get:

warning C4013: '_sntprintf' undefined; assuming extern returning int

warning C4013: '_T' undefined; assuming extern returning int



Am I not setting something right in my programming enviroment?



Interesting. Something is missing, since you're using wsprintf - defined in

terms of TCHAR, but yet the _T macro is undefined. Do you have <tchar.h>

included, directly or indirectly? If not, you might want to include it -

that's where _T is defined.



You should make a habit of looking a function up on MSDN before you try to

use it. If you look up _sntprintf, you'll find the documentation here:



msdn2.microsoft.com/en-us/library/2ts7cx93.aspx">msdn2.microsoft.com/en-us/library/2ts7cx93.aspx



While it's not overtly clear from the documentation, to use any of the "t"

functions (like _snTprintf), you need to #include <tchar.h>and <stdio.h>.



HTH



-cd







-

Re:What the L? in wsprintf

Quote
Here is a little background on what I'm doing. I am programming a aircraft

panel gauge for MS Flight Simulator. The wsprintf is used in a callback

routine set up by a FS macro. The callback looks like this:



FLOAT64 FSAPI update_callback(PELEMENT_STRING pelement ){

FLOAT64 val=pelement->source_var[0].var_value.n;

wsprintf(pelement->string, L"%d",(UINT32)val);

return val;

}



I've presented this problem to the FS programming forums and no one even

uses the L. Their routines look exactly like mine but use "%3d" instead of

L"%d", but that doesn't work for me.



I tried what you suggested and here are my results. When I use:

wsprintf(pelement->string, _T("%d"), (UINT32)val);



I get "warning C4013: '_T' undefined; assuming extern returning int"



When I use:

_sntprintf(pelement->string, _T("%d"), (UINT32)val);



I get:

warning C4013: '_sntprintf' undefined; assuming extern returning int

warning C4013: '_T' undefined; assuming extern returning int



Am I not setting something right in my programming enviroment?



You need to #include the right headers which is normally just <tchar.h>.

Read up on "Generic-text mappings" for details.





-

Re:What the L? in wsprintf

"Thomas Magma" <somewhere@overtherainbow.com>wrote in message

Quote


>

>Here is a little background on what I'm doing. I am programming a

>aircraft panel gauge for MS Flight Simulator. The wsprintf is used in a

>callback routine set up by a FS macro. The callback looks like this:

>

>FLOAT64 FSAPI update_callback(PELEMENT_STRING pelement ){

>FLOAT64 val=pelement->source_var[0].var_value.n;

>wsprintf(pelement->string, L"%d",(UINT32)val);

>return val;

>}

>

>I've presented this problem to the FS programming forums and no one even

>uses the L. Their routines look exactly like mine but use "%3d" instead

>of L"%d", but that doesn't work for me.

>

>I tried what you suggested and here are my results. When I use:

>wsprintf(pelement->string, _T("%d"), (UINT32)val);

>

>I get "warning C4013: '_T' undefined; assuming extern returning int"

>

>When I use:

>_sntprintf(pelement->string, _T("%d"), (UINT32)val);

>

>I get:

>warning C4013: '_sntprintf' undefined; assuming extern returning int

>warning C4013: '_T' undefined; assuming extern returning int

>

>Am I not setting something right in my programming enviroment?

>

>Thomas

>



Also, I'm getting a warning when compiling which points toward the

wsprintf line:

wsprintf(pelement->string, L"%d", (UINT32)val);

Output:

warning C4133: 'function' : incompatible types - from 'PCHAR' to 'LPWSTR'



Could there a problem with pelement->string?



You must be compiling as C, not C++ - in C++ your code shouldn't compile at

all. If I'm reading the C error correctly, you must be doing a UNICODE

build, but pelement->string is an ANSI string. Can you check your project

settings to confirm whether you're doing a UNICODE build or not?



-cd





-

Re:What the L? in wsprintf

Quote


You must be compiling as C, not C++ - in C++ your code shouldn't compile

at all. If I'm reading the C error correctly, you must be doing a UNICODE

build, but pelement->string is an ANSI string. Can you check your project

settings to confirm whether you're doing a UNICODE build or not?



-cd







I just read that the UNICODE code build is default, so I guess I am. I'm

just trying to figure out where to set it back to ANSI in VC++ 2005.



(You have to excuse me, I'm a Java programmer by nature and am finding this

MS stuff to be a little confusing)



I appreciate all the help Carl.



Thomas





-

Re:What the L? in wsprintf



Quote
You must be compiling as C, not C++ - in C++ your code shouldn't compile

at

all. If I'm reading the C error correctly, you must be doing a UNICODE

build, but pelement->string is an ANSI string. Can you check your project

settings to confirm whether you're doing a UNICODE build or not?



-cd





You were right Carl, thanks.



For those that interested and for the history of this string...you have to

set your preprocessor definitions to ANSI. You do this in MS Visual C++ 2005

Express Edition by right clicking on your project then select Properties ->

Configuration Properties ->C/C++ ->Preprocessor ->Preprocessor

Definitions and type in ;ANSI at the end of the string. It's just common

sense! (what a joke)



Thomas





-

Re:What the L? in wsprintf

"Thomas Magma" <somewhere@overtherainbow.com>wrote...

Quote


>You must be compiling as C, not C++ - in C++ your code shouldn't compile

>at all. If I'm reading the C error correctly, you must be doing a

>UNICODE build, but pelement->string is an ANSI string. Can you check

>your project settings to confirm whether you're doing a UNICODE build or

>not?



You were right Carl, thanks.



For those that interested and for the history of this string...you have to

set your preprocessor definitions to ANSI. You do this in MS Visual C++

2005 Express Edition by right clicking on your project then select

Properties ->Configuration Properties ->C/C++ ->Preprocessor ->

Preprocessor Definitions and type in ;ANSI at the end of the string. It's

just common sense! (what a joke)



I'm not sure how that works - wsprintf's declaration changes based on the

UNICODE symbol, not one called ANSI. Maybe that percolates through from

somewhere.



Anyway, you could also try looking on the General property page and setting

"character set" to multi-byte rather than Unicode. That should avoid you

having to mess with preprocessor definitions yourself, since they're then

effectively inherited from the project defaults.



--

Andy





-