Prevent C preprocessor to do a specific macro subsitution
Asked Answered
A

5

6

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.

Ator answered 14/11, 2008 at 22:8 Comment(3)
Just doing #undef GetMessage is not good, as the code that uses the API also uses the Windows GetMessage.Ator
Ah, another example of Windows SDK's indiscriminate use of macros clobbering any and all namespaces (and not just in the namespace feature of C++).Merchant
@vincent: see don.newfeld's comment in ShoeLace's #undef GetMessage answerMerchant
F
6

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.

Fold answered 14/11, 2008 at 22:12 Comment(4)
But then I can't use the Windows GetMessage anymoreAtor
Yes you can, just call GetMessageA or GetMessageW directly, whichever is appropriate.Etna
And, yes, push_macro and pop_macro are MS specific. Pragmas are implementation-defined by definition, and I haven't noticed these outside MS code. Of course, this may not be a problem for somebody coding to the Windows API.Bowshot
#pragma push_macro and #pragma pop_macro are supported from gcc 4.3.Mutt
C
6

(GetMessage)(...)

GetMessage on MSDN

Crimea answered 14/11, 2008 at 22:54 Comment(2)
While this would often be a good way to avoid unwittingly getting clobbered by a macro, I don't think it'll work for GetMessage, as it's not defined as a 'function-like' macro.Merchant
Oh, right... this works only if the function is declared before the macro, not after (which I don't think is guaranteed anyway). MSNCrimea
C
4

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.

Cyperaceous answered 14/11, 2008 at 23:10 Comment(2)
MS specific shouldn't be a problem as the problem is in a macro defined by windows.h :-PChangeup
It is a problem because there are non-MS compiler suites targeting Windows (for instance, mingw).Citizen
E
1

Is there code that calls both your GetMessage and Window's GetMessage?

Such code won't be able differentiate between the two of them. You won't be able to call both in the same file.

If you use one function in one file and the other in another file, just do the suggested #undef in one file.

Exhaust answered 14/11, 2008 at 22:27 Comment(0)
F
0

Given your constraints as outlined in your comment, the only way you can do this is to do a:

#undef GetMessage

right before the call to your API's GetMessage. (And this assumes noone after this point in the source file is calling the Win32 GetMessage.)

Fervency answered 14/11, 2008 at 22:36 Comment(1)
That's not the only way, but it would require you to NOT include windows.h. :)Coley

© 2022 - 2024 — McMap. All rights reserved.