Prevent C Preprocessor for Specific Macro Substitution

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

The specific problem is this: 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, it replaces my GetMessage method with the GetMessageA method.

+6
c ++ c-preprocessor
source share
5 answers

Have you tried just to do

  #undef GetMessage 

or even

  #ifdef GetMessage
 #undef GetMessage
 #endif 

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

you should know if you use char * for wchar_t8 ..

(thanks don.neufeld)

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

#pragma push_macro("GetMessage") #undef GetMessage // Your GetMessage usage/definition here #pragma pop_macro("GetMessage") 

I suspect this is a feature specific to MS, so keep that in mind.

+6
source share

(GetMessage)(...)

GetMessage on MSDN

+6
source share

If you have useful information, you can also use #pragma push_macro / pop_macro to define push and pop macros. This is great if you want to override the macro definition in a code block:

 #pragma push_macro("GetMessage") #undef GetMessage // Your GetMessage usage/definition here #pragma pop_macro("GetMessage") 

I suspect this is a feature specific to MS, so keep that in mind.

+4
source share

Is there any code that calls both GetMessage and Window GetMessage?

Such code cannot distinguish between two of them. You cannot call both in the same file.

If you use one function in one file and another in another file, just enter #undef in one file.

+1
source share

Given your limitations outlined in your comment, the only way to do this is to do:

 #undef GetMessage 

right before calling your GetMessage API. (And this does not imply that after this point Win32 GetMessage is called in the source file.)

0
source share

All Articles