ollo
ollo

Reputation: 25340

Why does MinGW define Win API functions as macro?

I'm wondering why so many Win API functions are only defines to their real implemenation in MinGW.

Example:

MessageBox() function as described in the MSDN documenation:

int WINAPI MessageBox(
  _In_opt_  HWND hWnd,
  _In_opt_  LPCTSTR lpText,
  _In_opt_  LPCTSTR lpCaption,
  _In_      UINT uType
);

And here's the implementation of MinGW (winuser.h):

#define MessageBox MessageBoxA
/* ... */
WINUSERAPI int WINAPI MessageBoxA(HWND,LPCSTR,LPCSTR,UINT);

So MessageBox is not a function, it's only a define for the real function.

Another one (taken from winbase.h:

#define GetVersionEx GetVersionExA
/* ... */
WINBASEAPI BOOL WINAPI GetVersionExA(LPOSVERSIONINFOA);

As you can see in most cases the functions are implemented as macros to their real implementation.

Is there any reason for doing this? And why are they not implemented as "real" functions (using their real name)?

Upvotes: 4

Views: 884

Answers (2)

md5
md5

Reputation: 23699

From GetVersionEx documentation:

Unicode and ANSI names: GetVersionExW (Unicode) and GetVersionExA (ANSI)

From Conventions for Function Prototypes documentation:

The preprocessor expands the macro into either the Windows code page or Unicode function name. The letter "A" (ANSI) or "W" (Unicode) is added at the end of the generic function name, as appropriate. The header file then provides two specific prototypes, one for Windows code pages and one for Unicode, as shown in the following examples.

In your case, GetVersionEx will expand to GetVersionExA because it looks like you are using Windows code page.

Upvotes: 6

Inisheer
Inisheer

Reputation: 20794

Using the DEFINEs enable you to use the same code with difference compiler options. For example: MessageBox will use MessageBoxW if using unicode, but MessageBoxA if not. It would be a pain to have to go through an entire code base just to change the method names.

Edit: Also see Christoph's reference, which this answer is referring to.

Upvotes: 2

Related Questions