Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MinGW define Win API functions as macro?

Tags:

c++

c

mingw

winapi

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)?

like image 433
ollo Avatar asked Feb 17 '23 07:02

ollo


2 Answers

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.

like image 104
md5 Avatar answered Feb 20 '23 02:02

md5


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.

like image 41
Inisheer Avatar answered Feb 20 '23 04:02

Inisheer