473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Name conflict with windows.h define

Hi!

I have a name conflict between my class and windows.h header. The
problem is because of windows.h contains GetMessage macro and my class
a method with the same name. Thus the macro replaces my method
declaration in the main module which contains these lines:

#include <windows.h>
#include "myclass.h"

But in myclass.cpp file windows.h is not included. So I have GetMessage
method compiled and GetMessageA referenced in the main module. Of
course, it causes an 'unresolved external symbol' linker error.

Is there any "good" solution to avoid these uncomfortable error.
Anything instead of renaming (this seems to be the best now for me) my
method, including windows.h into myclass.cpp or so. Can I in some way
disable macro and enable it again?

I guess, smth like that will work, probably:

// myclass.h
#pragma once

#ifdef GetMessage
# undef GetMessage
# define GET_MESSAGE_WAS_DEFINED
#endif

class MyClass
{
...
};

#ifdef GET_MESSAGE_WAS_DEFINED
# undef GET_MESSAGE_WAS_DEFINED
# ifdef _UNICODE
# define GetMessage GetMessageA
# else
# define GetMessage GetMessageW
# endif
#endif

But still, I don't like this solution. It is still is not flexible and
portable (especially if real GetMessage macro definition will change)

And why the hell this global preprocessor definition conflicts with my
class method!!!
I don't like it at all.

Thanks, for any advice about solving the problem.

Apr 2 '06 #1
2 8354
> And why the hell this global preprocessor definition conflicts with my
class method!!!
I don't like it at all.
I guess no one likes it. Here are some possibilities in addition to what
you have posted:

1. Change your naming style. Don't use GetMessage, instead, use
getMessage, or get_message or (my personal favorite) get_msg.

2. Don't include <windows.h> or other notorious headers. This may be
overkilling but I used to put the header only in a source file called
PAL.cpp (Platform Abstraction Layer) and put necessary declarations to
wrapper functions in PAL.hpp. This way you not only filters the nasty
macros but also improves portability.

3. Include the header only after your GetMessage. It might seem
unnatural but it should do the trick. But if your GetMessage uses
anything from <windows.h> then you may have to manually forward declare
all those.

4. Provide your wrapper header that does all the macro tricks. E.g.

// windows_wrapped.h

#include <windows.h>

#if (GetMessage == GetMessageA)
#undef GetMessage

BOOL GetMessage(LPMSG msg, HWND hwnd,
UINT m, UINT n){
return ::GetMessageA(msg, hwnd, m, n);}

#endif

#if (GetMessage == GetMessageA)
#undef GetMessage

inline BOOL GetMessage(LPMSG msg, HWND hwnd,
UINT m, UINT n){
return ::GetMessageA(msg, hwnd, m, n);}

#endif

#if (GetMessage == GetMessageW)
#undef GetMessage
inline BOOL GetMessage(LPMSG msg, HWND hwnd,
UINT m, UINT n){
return ::GetMessageW(msg, hwnd, m, n);}

#endif

This way the symbol you will have to deal with is no longer a
preprocessor macro so namespace rules apply.

Regards,
Ben

Thanks, for any advice about solving the problem.

Apr 2 '06 #2
You can solve the problem by including windows.h in the header, so that
the actual name will be GetMessageA/GetMessageW, also in the
compilation unit. Another way, which is already suggested is to change
your name to something like GetMsg, RetrieveMsg, RetrieveMessage or
something like that.

Apr 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Adam | last post by:
Hi I had Ruby 1.6.8-8 for Windows installed on my WinXP Pro machine prior to installing Python 2.3. After installing Python 2.3, I tried to <----- screen output of python interactive command...
8
by: Felix Wiemann | last post by:
Hi! I want to create a module myproject.ui.curses, which needs to import the curses library. However, if I just write ``import curses``, the module imports *itself* instead of the standard...
2
by: chunmok | last post by:
When compile using g++, following error messages appears. /usr/include/sys/sysinfo.h:101: `struct sysinfo_t sysinfo' redeclared as differe nt kind of symbol /usr/include/sys/systeminfo.h:77:...
9
by: John | last post by:
I am using two large libraries which both have an implementation of a "matrix" class. I have the source code for both. I need to use them in the same project and when I try to compile I get a...
37
by: hasadh | last post by:
Hello, probably this may be a simple qn to u all but I need an answer plz. In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who included this code as a library into his module...
16
by: Vadim Biktashev | last post by:
Hello all I would like to give a certain name to a certain global variable. Unfortunately, this name is already used in math.h for a mathematical function. Worse, I do need to use maths library...
5
by: quortex | last post by:
Hi all, I have a class which contains a method called GetMessage(). When I compile and unit test it all is well. However in the application I want to use it in there appears to be a conflict...
2
by: Daniel Ngu | last post by:
Hi, I'm in the process of moving a database from a server running Windows 2000 SP4 to a PC running XP SP2. Basically, here's what I've managed to do so far: Made a full backup of the database...
9
by: Meendar | last post by:
Hi, Below is my code snippet having only one form, <form> <input type ="radio" name="action" value="xyz" checked>xyz <input type ="radio" name="action" value="zyx">zyx <input type ="radio"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.