Re: Register my own messages



Another alternative is to declare it in the header file as

static const int UWM_WHATEVER = ::RegisterWindowMessage(...);

which is a bit of a cheat but is easy to use, and works correctly. The trick is to not
include the header file except in the modules that actually use it.
joe

On Mon, 21 Jan 2008 10:32:04 -0600, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:

On Mon, 21 Jan 2008 08:10:29 -0800 (PST), clinisbut <clinisbut@xxxxxxxxx>
wrote:

I'm trying to understand how to work with my own defined messages.
I've readed the essays from http://www.flounder.com/mvp_tips.htm which
I'm sure is very complet, is too technical for my level.

I found an example at http://www.codersource.net/mfc_user_event.html
but doesn't seems to work... I'm getting this error:
"int CODERSOURCEMSG" (?CODERSOURCEMSG@@3HA) already defined in
PostMessage01.obj
Debug/PostMessage01.exe : fatal error LNK1169: one or more multiply
defined symbols found

I defined int CODERSOURCEMSG in a header file appart. I don't
understand why vc is saying thats a duplicate declaration...

An object may be declared many times, but it can only be defined once. The
definition is what gives it storage and may contain an initializer, while
the declaration merely declares its type and name so that it can be used. A
definition is always a declaration, but the reverse doesn't necessarily
hold. It sounds like your header file contains a definition, so every file
that #includes the header defines this int, and because it isn't declared
static, the linker complains about multiple definitions. To fix it, use
extern in the header file:

extern const int UWM_MYMSG;

And in one .cpp file, write:

const int UWM_MYMSG = RegisterWindowMessage(...);

This defines UWM_MYMSG in one and only one place, but the declaration in
the header file allows anyone who #includes the header to use it. Since its
value will never change, I also declared it const.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Register my own messages
    ... the declaration merely declares its type and name so that it can be used. ... It sounds like your header file contains a definition, ... that #includes the header defines this int, ... extern const int UWM_MYMSG; ...
    (microsoft.public.vc.mfc)
  • Re: Register my own messages
    ... I defined int CODERSOURCEMSG in a header file appart. ... the declaration merely declares its type and name so that it can be used. ... extern const int UWM_MYMSG; ...
    (microsoft.public.vc.mfc)
  • Re: Whats it with this kind of definition?
    ... > int myfunction; ... > talks about this kinda declaration. ... every caller should include that header file. ... the declaration of myfunction is within main. ...
    (comp.lang.c)
  • Re: Error C2259 while building application on 64-bit
    ... build to the header file you use in the 64-bit build. ... 'HRESULT IPersistFolder::Initialize': is ... declaration of 'IShellFolder::ParseDisplayName' ... while compiling class template member function 'HRESULT ...
    (microsoft.public.vc.mfc)
  • Re: External structs - newbie question
    ... The header file for csd.c contains the typedef for a ... There is only one struct in use ... Declaring it in a header file is the right way to go. ... If you put the keyword extern before the declaration, it will not be a problem declaring it twice. ...
    (comp.lang.c)