Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent C preprocessor to do a specific macro subsitution

How can I tell the preprocessor not to replace a specific macro?

The specific problem is the following: Windows header files define the GetMessage macro.

My C++ header files with my API have a GetMessage method. I do not want to rename my method. But when using the API on Windows, including windows.h replaces my GetMessage method call with GetMessageA.

like image 222
Vincent Oberle Avatar asked Nov 14 '08 22:11

Vincent Oberle


1 Answers

have you tried just doing an

#undef GetMessage

or even

#ifdef GetMessage
#undef GetMessage
#endif

and then calling the windows GetMessageA or GetMessageW directly, whichever is appropriate.

you should know if you are using char* for wchar_t8..

(thanks don.neufeld)

Brian also says that Jus some useful info, you can also use the #pragma push_macro/pop_macro to push and pop macro definitions. This is great if you want to override a macro definition in a block of code:

#pragma push_macro("GetMessage")
#undef GetMessage

// Your GetMessage usage/definition here

#pragma pop_macro("GetMessage")

I suspect this is an MS specific feature though, so keep that in mind.

like image 59
ShoeLace Avatar answered Sep 29 '22 10:09

ShoeLace