473,698 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8405
> 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(LPMS G msg, HWND hwnd,
UINT m, UINT n){
return ::GetMessageA(m sg, hwnd, m, n);}

#endif

#if (GetMessage == GetMessageA)
#undef GetMessage

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

#endif

#if (GetMessage == GetMessageW)
#undef GetMessage
inline BOOL GetMessage(LPMS G msg, HWND hwnd,
UINT m, UINT n){
return ::GetMessageW(m sg, 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
2361
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 line -----> Python 2.3 (#46, Jul 29 2003, 18:54:32) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from Tkinter import * >>> root = Tk()
8
5224
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 library's curses module. Is there any (reliable) way to access a module of the standard library if the names conflict as in this case?
2
4746
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: previous declaration of `int sysinfo(int, char *, long int)' .... szh.cpp:899: no match for call to `(sysinfo_t) (int, char, long
9
2327
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 double declaration conflict. I tried to wrap the smaller library into a namespace but its a nightmare. Is there anyway I could solve this problem without giving up on one of the libraries? Thanks, --j
37
3035
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 said that in his code he couldnt use the above words as function names or variable names and got compilation errors. He advised me to use them as enums instead of macros. is he right ??? (in my code the macros are used as return values from fns...
16
3591
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 and therefore have to include math.h. Thus my compiler reports conflicting declarations of the same name. Does anyone know any reasonably elegant way to resolve this conflict? I understand that I can give up and choose another name, but somehow not...
5
2926
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 with Windows.h. I receive the following error: Error 1 error LNK2001: unresolved external symbol "public: class Messaging::Native::IMessage * __thiscall
2
3150
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 on the server and then restored it onto the PC. Both running MS SQL Server 2000 SP3; though the server has Standard edition
9
1741
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" name="action" value="yxz">yxz </form>
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.