zenno2
zenno2

Reputation: 453

How to deal with WinAPI macros overriding some function names?

<windows.h> defines macroses for Ansi and Unicode versions of WinAPI. I have a function named SendMessage in my class library. It works fine until <windows.h> is included before including my library. In this case SendMessage is overrided by the macros and the function name becomes SendMessageA or SendMessageW.

Is it possible to somehow deal with it without changing the name of the function in order to save compatibility with older versions of the library?

Upvotes: 5

Views: 566

Answers (1)

Daniel Kleinstein
Daniel Kleinstein

Reputation: 5502

The real problem is that WinAPI's function definitions are at the C-preprocessor level, and so you have to write some ugly code to try to coexist with them.

If at all possible, you should rename your codebase's functions so that there is no collision with WinAPI.


Otherwise, you can write code like #undef SendMessage to undefine WinAPI's definition of this function before defining your own function.

If you need to switch between defining your own functions and using WinAPI's function macros, you can also use the #pragma push_macro and #pragma pop_macro functionality to preserve the macro before the undef and restore it afterwards.

Upvotes: 3

Related Questions