Naming Conventions in Win32 and MFC

Dr. Patrick Young, Stanford University CS193W Summer 2003

Hungarian Notation

Win32 follows a notation convention called Hungarian Notation. In Hungarian Notation the first or first several characters of a variable or parameter name identifies that variable or parameters type. Here are some of the most common Hungarian Notation prefixes: b or f—used as a prefix for booleans (f stands for flag). c—used as a prefix to chars.1 n—used as a prefix to shorts. h—used as a prefix for a handle. Handles in Win32 are numbers used to identify resources or windows. They are not pointers or pointers to pointers. Think of them as ID numbers. l—used as a prefix to a long. w—used as a prefix to a word. dw—used as a prefix to a double word. s—used as a prefix to a string. sz—used as a prefix to a null-terminated string.2 p—used as a prefix to a pointer. lp—used as a prefix to a long pointer (this is the same as a pointer in Win32 and is a relic from Win16). fn—used as a prefix to function parameters.

1 The combination cb is also used for parameters which represent the number of bytes of something. In this case cb is short for count in bytes. 2 sz can also be used for size. I do recommend double checking the documentation to make sure that the string is indeed null terminated, as an error here can be very difficult to track down.

Win32 Data Types

Win32 includes its own set of data types. These data types are usually written with all capital letters and are typically #defined or typedef’ed somewhere in the Windows header files. They often follow similar conventions to Hungarian Notation. An H before a name, for example represents a handle while an LP represents a long pointer.

Most of them are pretty obvious WORD, for example is obviously a word (which is a 16-bit unsigned integer in Win32). Here are a few of the more common types you should be familiar with:

LPSTR—long pointer to a string (equivalent to a pointer to a string for Win32).

LPCSTR—long pointer to a constant string.

BOOL—a Windows’ Boolean. Note that this is not quite the same as a C++ bool, use the constants TRUE and FALSE with Windows’ BOOLs and true and false with C++ bools.

UINT—unsigned integer.

Win32 Constants

An identifier with all uppercase characters may also represent a constant integer value. In this case, the first two characters are followed by an underscore and the first two characters identify the type of constant. For example WM_COMMAND constant integer which identifies a particular windows message. ES_RIGHT is a constant integer which can be used to request a particular style of edit window (one in which the text is right aligned). LVS_REPORT is a constant integer which is used to request a list control with list view style including multi-column report display format.

MFC Naming

MFC has its own set of naming conventions:

m_—all member variables are preceded with an m_.

On—all message handlers function should be preceded with On (e.g., OnSize). Note however, that not all functions that begin with On are true Win32 message handlers. For example OnInitialUpdate is not a Win32 message handler, and there is no corresponding WM_INITIALUPDATE Windows message.

2