Help! Windows.h Conflict

Q

quortex

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@@QAEPAVIMessage@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
 
P

Phlip

quortex said:
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
 
Q

quortex

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
 
D

deane_gavin

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@@QAEPAVIMessage@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
 
D

deane_gavin

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
 
Z

Zara

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@@QAEPAVIMessage@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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top