By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Help! Windows.h Conflict

P: n/a
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
Messaging::Native::MessageEvent::GetMessageW(void) "
(?GetMessageW@MessageEvent@Native@Messaging@@QAEPA VIMessage@234@XZ) Program.obj

As far as I can tell this is due to the following definition in
Windows.h which is causing the linker to convert my GetMessage() method
call to GetMessageW(). The line which causes this is as such:

_message = notification->GetMessage(); (notification is a
MessageEvent*)

And I think the linker is trying to do ->GetMessageW(); because of:

#define GetMessage GetMessageW

Which is included in one of the includes from Windows.h. I am using
Unicode so I cannot turn unicode off but then I guess it would still
have problems as it would then translate to GetMessageA();

I do not want to have to rename my method because of this. How can I
get remove this conflict? I would of thought that the definition would
only affect the global namespace but alas I must be wrong.

Kind Regards,
Mark

Dec 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
quortex wrote:
I have a class which contains a method called GetMessage(). When I
compile and unit test it all is well.
Props for writing unit tests! (If that's what you mean;)
However ... Windows.h ... GetMessageW ... #define GetMessage GetMessageW I do not want to have to rename my method because of this. How can I
get remove this conflict? I would of thought that the definition would
only affect the global namespace but alas I must be wrong.


Sorry - MS's cute trick always bites us one way or the other. And #define
things never obey C++ scope. That's one big reason to avoid writing your
#define lines. MS can't follow that rule because they must port to C, which
has fewer alternatives.

Try this:

#include <windows.h>
#undef GetMessage

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Dec 22 '05 #2

P: n/a
Hi,

Thanks for the reply. Yes I am writing unit tests, Test Driven
Development in fact ;)

I already tried the #undef GetMessage actually. In fact I was
bewildered by the result:

Error 5 error C2039: 'GetMessage' : is not a member of
'Messaging::Native::MessageEvent'
d:\my data\development\messaging.native.test\program.cpp 28

Surely #undef GetMessage shouldn't undefine my member just ignore the
previous definition.

Windows.h is included by a few header files so I put the #undef only at
the top of the file causing problems although after all the includes so
should be correct IMO.

I am stumped :(

Thanks,
Mark

Dec 22 '05 #3

P: n/a

qu*****@hotmail.com wrote:
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
Messaging::Native::MessageEvent::GetMessageW(void) "
(?GetMessageW@MessageEvent@Native@Messaging@@QAEPA VIMessage@234@XZ) Program.obj

As far as I can tell this is due to the following definition in
Windows.h which is causing the linker to convert my GetMessage() method
call to GetMessageW(). The line which causes this is as such:

_message = notification->GetMessage(); (notification is a
MessageEvent*)

And I think the linker is trying to do ->GetMessageW(); because of:

#define GetMessage GetMessageW

Which is included in one of the includes from Windows.h. I am using
Unicode so I cannot turn unicode off but then I guess it would still
have problems as it would then translate to GetMessageA();

I do not want to have to rename my method because of this. How can I
get remove this conflict? I would of thought that the definition would
only affect the global namespace but alas I must be wrong.


Unfortunately, macros do not respect scope or namespaces or the type
system. They cannot since those concepts are only meaningful to the
compiler and macro substitution is done by the preprocessor, not the
compiler. Your experience is exactly why macros are evil.

Assuming you need to include "windows.h" at all in your program, can
you move your code that uses your GetMessage function into a separate
source file that doesn't #include "windows.h"? That will probably
depend on how widely you use your GetMessage function. If not, I can't
think of anything right now other than renaming your function.

Gavin Deane

Dec 22 '05 #4

P: n/a

qu*****@hotmail.com wrote:
Hi,

Thanks for the reply. Yes I am writing unit tests, Test Driven
Development in fact ;)

I already tried the #undef GetMessage actually. In fact I was
bewildered by the result:

Error 5 error C2039: 'GetMessage' : is not a member of
'Messaging::Native::MessageEvent'
d:\my data\development\messaging.native.test\program.cpp 28

Surely #undef GetMessage shouldn't undefine my member just ignore the
previous definition.

Windows.h is included by a few header files so I put the #undef only at
the top of the file causing problems although after all the includes so
should be correct IMO.

I am stumped :(


Just a guess...

Did you #undef GetMessage before your class definition, the
implementation of the class *and* everywhere you use the class?

If your class definition was compiled with the GetMessage macro visible
then it would actually declare a member function called GetMessageW.
Then your client code (compiled with the GetMessage macro #undef'd)
would try and call a non-existent function called GetMessage, which
would yield the compiler error you see.

Gavin Deane

Dec 22 '05 #5

P: n/a
On 22 Dec 2005 08:44:43 -0800, qu*****@hotmail.com wrote:
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
Messaging::Native::MessageEvent::GetMessageW(void )"
(?GetMessageW@MessageEvent@Native@Messaging@@QAEP AVIMessage@234@XZ) Program.obj

As far as I can tell this is due to the following definition in
Windows.h which is causing the linker to convert my GetMessage() method
call to GetMessageW(). The line which causes this is as such:

_message = notification->GetMessage(); (notification is a
MessageEvent*)

And I think the linker is trying to do ->GetMessageW(); because of:

#define GetMessage GetMessageW

Which is included in one of the includes from Windows.h. I am using
Unicode so I cannot turn unicode off but then I guess it would still
have problems as it would then translate to GetMessageA();

I do not want to have to rename my method because of this. How can I
get remove this conflict? I would of thought that the definition would
only affect the global namespace but alas I must be wrong.

Kind Regards,
Mark


I also program for Windows OS, and I have met this problem so many
times, that finally, I have reached a coding style thta works nice:
All functions will start with a lower case letter (i.e. getMessage
instead of GetMessage).

MS uses exactly the opposite style, so that I have two benefits: no
name clashing with MS *and* easy identificaction os OS functions at
first sight.

Best regards

Zara
Dec 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.