Windows coding conventions

Source: Internet
Author: User
ArticleDirectory
    • Type Definition
Windows Encoding conventions

Translated from English on msdn

If you are writingWindowsProgramWhen you first seeWindowsProgram source code, it will be disturbing. TheseCodeFull of strange definitions, suchDWORD, lprectAnd variable names, such as"Hwnd","Pwsz"(Hungarian Notation ). It's worth some timeWindowsCoding conventions for programming.

 

The vast majorityWindows APIIs composed of functions or component object models (Com) Interface. FewWindows APIYesC ++Class (a notable exception isGDI +,2dGraphicsAPI).

 

Type Definition

WindowsThe header file contains a large number of Type Definitions, mostly inWindef. h. Here are some of the frequently encountered Type Definitions.

 

Integer(Integer) Type

Data Type

Data size

Whether there are positive or negative symbols

Byte

8Bit binary

Unsigned

DWORD

32Bit binary

Unsigned

Int32

32Bit binary

SignedSigned

Int64

64Bit binary

SignedSigned

Long

32Bit binary

SignedSigned

Longlong

64Bit binary

SignedSigned

Uint32

32Bit binary

Unsigned

Uint64

64Bit binary

Unsigned

Ulong

32Bit binary

Unsigned

Ulonglong

64Bit binary

Unsigned

Word

16Bit binary

Unsigned

 

As you can see, the definition of these data types has certain redundancy, and the overlap is partly due to the consideration of the old versionWindows API. The types listed here have fixed sizes, and32Bitwise AND64In applications. For example:DWORDType, in32Bit or64Is always32Bit Width.

 

BooleanType (Boolean)

Bool is an integer used to define the type of a Boolean context. It is a header file.Windef. hTwo values andUse bool together:

# Define false 0

# Define true 1

Although this definition isTrueHowever, mostThe bool value function returns any non-zero value to indicate the Boolean true value. Therefore, Boolean applications should be written as follows:

 

//Correct writing

Bool result = somefunctionthatreturnsboolean ();

If (result)

{

...

}

And it should not be like this:

 

//Incorrect syntax

If (result = true)

{

...

}

Note that bool is an integerC ++OfThe bool type is not interchangeable.

 

PointerType (pointer)

WindowsDefines many forms suchPointer-to-xData type.POrLpAs the name prefix, for example:Lprect is a pointer to rect, and rect is a structure that describes the rectangle. The following Variable declaration is equivalent:

Rect * rect ;//PointRectStructure pointer

Lprect rect ;//Same as above

Prect rect ;//Same as above

 

In terms of history,PPointer,LpIndicates a long pointer; a long pointer (also called a remote pointer) is16BitWindowsWhen the memory address range of the current segment is not required. ThisLpThe prefix is retained to make it easier16Bit code is connected32BitWindows. Now there is no difference. A pointer is just a pointer.

 

Pointer precisionType (pointer precision)

The following data types are pointer sizes -- specifically, in 32 Bit application is 32 Bit Width, 64 Bit application is 64 Bit Width. The size is determined during compilation. When 32 The program is running in 64 Bit Windows The data type is still 4 Byte width (one 64 Bit programs cannot 32 Bit Windows ).

L dword_ptr

L int_ptr

L long_ptr

L ulong_ptr

L uint_ptr

 

When these types are used, an integer may be converted to a pointer. They are also used to define pointers.AlgorithmAnd define the cyclic counter to traverse all the bytes in the memory buffer. More commonly, where they appear64BitWindowsThe existing32Bit value extended64Bit.

 

Hungarian notation

The Hungarian notation adds the prefix to the variable name and adds information to the variable (the inventor of this notation, Charles-Simon is a Hungary, so this name is used ).

In its initial form, Hungary notation assigns a variable semantic information that tells you the intended use. For example,IThe semantics is index,CBThe semantics is the size (Byte Count) in bytes, andRWAndColThe meaning is the number of rows and columns. These prefixes are designed to avoid unexpected use of a variable in the error context. For example, you can see an expressionRwpointer + cbtableYou will know that a row number is added to a size, which is almost certainly in the codeBug.

 

A more common Hungarian notation is to use a prefix to give type information. For example,DWIndicatesDWORD,WIndicatesWord.

 

If you search for Hungarian notation on the internet, you will find many comments about whether the Hungarian notation is good or bad. Some programmers are highly disgusted with the Hungarian notation and others find it useful. In any case, many peopleMsdnThe sample code on is in Hungary notation. But you do not need to remember the prefix to understand the code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.