If we work with Visual C ++, there is a need to declare variables, work with C libraries, WinAPI, etc. The question arises as to how better and more correctly to declare the types of variables, for example, you can unsigned short, and you can WORD, or unsigned long, and you can DWORD, etc. Do not want this confusion, how to develop the right approach !? And also, for example:

DWORD WINAPI DdeQueryString( __in DWORD idInst, __in HSZ hsz, __out_opt LPTSTR psz, __in DWORD cchMax, __in int iCodePage ); 

How to find out the abbreviation decoding - LPTSTR (and the like), STR-string, PT-pointer, is understandable here, and L-? Where can I get information in accessible language?

    4 answers 4

    Use better WinAPI style code. That is, instead of unsigned shor write WORD and so on. Source - google search on msdn.com. There you have both C # + .NET and C ++ + WinAPI.

    If you are writing an application and in the future you want to transfer it to Linux, then try not to use any platform-specific functions at all in the main code, but only wrapping around them. In this case, then you just write another wrapper with the same interface under Linux and voila.

    If such portability is not planned, you can safely use WinAPI and its aliases.

    • Now holivar will begin. - avp
    • Opinions are divided!? :) - rejie
    • Come on you. If a person asks what style of code to use, I do not think that he has a giant application in his plans that works even in a vacuum cleaner. - Jakeroid
    • The application is exclusively for Windows, just as it does not beautifully work in one place unsigned short in another WORD, I try to decide once and for all! :) - rejie pm
    • I think that this is how it will be determined and no longer use another style will not work. Write in one style, and here what - choose depending on the task. - Jakeroid 2:46 pm

    LPSTR and so on. This is one of the versions of the so-called. Hungarian Naming Convention , which according to one of the legends was invented by the developers of Windows. Hungarian it was named because it is difficult to pronounce (Hungarian is known to be one of the most difficult languages ​​in the world) and in honor of one of the developers of Hungarian nationality.

    In this context, LPSTR means Long Pointer to String - a long pointer to a string. In those ancient times when the memory of computers was segmented were so-called. long and short pointers. Short pointers within a segment, and long pointers within the entire memory. It's archaism now, but LPSTR has survived. Capital letters indicate what type it is.

      Similar aliases are introduced by WinAPI authors for better portability, since in different versions some APIs work with different data types. For portability and pseudonyms are applied.

      And the most important source is MSDN.

      • Well, in my code to use pseudonyms or a standard ad? Style, then we must immediately develop!? - rejie
      • If you want portability between * nix and Windows, do not use DWORD, LPSTR ... and others from this series, write where you can using posix functions (native WinAPI only as a last resort). - avp

      I declare my types. For example:

       typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; 

      I myself use only them. There have never been any WinAPI compatibility issues.

      LPTSTR is defined as char* when Unicode is disabled, and wchar_t* when enabled. LPCTSTR is the same, but with const. LPSTR and LPCSTR are the same, but always char* . LPWSTR and LPCWSTR - wchar_t* .

      I think that WinAPI types should not be used. Everything that is written in capital letters is usually associated with macros or constants. In addition, these types (INT, LONG) are not highlighted by the editor. Own ushort and uint, though not also highlighted, but if you decide to use a single style, then they fit the style to short, int and long.

      • In general, correct, but do not reinvent the wheel. In * nix (see sys / types.h, but usually with an “automaton” it is connected via stdio.h, etc.) typedefs of u_char, u_short, u_long , etc. are taken . For all sizes, for signed and unsigned. IMHO this is for posix . - avp
      • But is it Unix, and in other OS? Need something in common. - gammaker