views:

113

answers:

4

Hi,

ive been working on c++ on linux for the past 2 years,and switched to windows c++ programming recently.

can anyone tell me what that L is there in the argument of the function:

SHLoadImageFile(L"\\Program Files\\TrainingApp\\background.png");

and on viewing certain sample code in MSVS C++ i came across hundereds of typedefs like..

LPARAM// typedef LONG_PTR LPARAM... here LONG_PTR is again typedef as __w64 long

WPARAM// typedef UINT_PTR WPARAM...

so there is a lot of chained typedefs.. I never saw this much of typedef chaining on c++ programming on linux using gcc..

what i want to say is that it just creates more confusion in this way for windows application programming.. while ive seen application programming on linux using frameworks like Qt.. there such things are rarely used.. so is there specific purpose in typedefining again and again on MSVSC++?? for eg.. there are typdefs like

typedef int BOOL;

whats the use of this when normal bool is available already..?? there are hundred other cases ive come across where just to decide what data type to use becomes so difficult.. it becomes difficult to understand a pre written code in this fashion too..

Thanks.

A: 

L tells the compiler to use Unicode string.

There is also a _TEXT macro that helps creating programs that target both Unicode and non-Unicode platforms. It is defined as:

#define _TEXT(t) t      // this does noting

or

#define _TEXT(t) L ## t    // Adds L in front of the string

And finally TCHAR which can be stored on 1 or 2 bytes:

TCHAR mystring[] = _TEXT("text");

TCHAR is set up to be equal to a wchar_t when _UNICODE is defined, and char when it isn't.

Pawel Lesnikowski
+2  A: 

The L in the argument is standard way of telling the compiler that the following string literal is unicode not single-byte characters. It is equivalent to the postfix L to indicate that a long integer constant.

The windows typedefs do seem confusing but once you get your head around the naming scheme they start to make sense.

You have to remember that this API originated in the early 90's. Thus the typedefs are/were required to handle pointers on different architectures as well as 32/64 bit types and are also required to compile for non-intel windows machines.

The BOOL->int mapping is required asthe interface is C compatible (not c++) and thus can't use the built in bool type.

Elemental
A: 

Hey,

The L in front of the string literal is to make it a wide string(that is a unicode string).

The other things you are asking about are basically historical. Keep in mind that Windows legacy is stooped in a C only object oriented(kind of) event driven API. This goes all the way back to Win16(basically pre windows 95). Also keep in mind Microsoft always had a knack for very long winded APIs and weird coding conventions. That is why you see things like bool being defined as an int(back in the day C had no bool).

You mention LPARAM and WPARAM... these go all the way back to Win16/23 programming and are the two parameters passed in the Win16/32/64 message pump. The L stands for long(as it was a LONG type) and the W stands for Word as that's what it was. These values held different parameters depending on the message(ie for a windows click down event it might contain a pointer in LPARAM to a struct containing the coordinates of the click down event).

I am sure none of this helps you that much in deciding what type to use... the only thing I could recommend is that you learn a lot more about legacy windows programming if you need to do this. But if I were you and you have used something like Qt on Linux and wish to write windows code... why not jsut get Qt for windows and use that? It will hide most of this(frankly now obsolete) detail from you.

Hope this helps!

Gray Area
haha ya i would have preferred Qt but the project im involved demands VSC++ so no choice.. its basically some porting work of games from this device to that.. @gameloft.. :) thanks by the way for the explanation...
ashishsony
@ashishony: Qt and MSVC++ are not mutually exclusive. I've built a number of serious projects using both MSVC and Qt.
MSalters
+2  A: 

L is standard C, and is valid under Linux also. It denotes that the string literal is wide, for the implementation's definition of wide. See 6.4.5 of C99 spec.

The type of the array elements is wchar_t instead of char when the L prefix is used.

Alex