ON_MESSAGE help. 
Author Message
 ON_MESSAGE help.

Quote:

> I have been able to create a ON_MESSAGE related function that works
> fine, but only when called within my CShipDlg class.  When i try to
> send this message to CShipDlg from my property sheet(m_pBuildSheet), i
> get an assertion error.  I would set it up like this(which i think is
> incorrect).  This is all within one function in my property sheet
> class.

> CShipDlg* x;
> x->PostMessage(WM_GOODBYE);

> here are snippets of code:

>         #define WM_GOODBYE WM_USER + 12

> This is under my CShipDlg class, right before the
> DECLARE_MESSAGE_MAP() and after the AFX brackets.

>         afx_msg void OnGoodbye();

> This is in my dialog cpp file, after BEGIN_MESSAGE_MAP, and outside of
> the AFX_MSG_MAP brackets

>         ON_MESSAGE(WM_GOODBYE, OnGoodbye)

> This code was added to my CShipDlg, which is the dialog class created
> by classwizard, which produces a dialog when the code is run.
>         void CShipDlg::OnGoodbye()
>         {
>                 m_pBuildSheet->DestroyWindow();
>                 delete m_pBuildSheet;
>                 m_pBuildSheet = NULL;
>                 MessageBox("Testing 1 2 3...");

>         }

> NOTE: I can get everything to run fine when i post the message within
> the CShipDlg class(messagebox works, etc), so i think my problem is
> when i try to create a pointer to my CShipDlg...

> I appreciate all/any help that you can give me.  Thanx.

> ----------------------------------------------
> Chris Ahlers

> ICQ: 1822282

Hi Chris.  This is a problem:

 CShipDlg* x;
 x->PostMessage(WM_GOODBYE);

The pointer x is uninitialized so it is not pointing at anything usable.  You should first change it into a member variable of the property sheet.  When creating the property sheet object, pass it the address of the dialog either as a constructor parameter or with something like:

 m_pBuildSheet->m_pShipDlg = this;



Tue, 04 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
I have been able to create a ON_MESSAGE related function that works
fine, but only when called within my CShipDlg class.  When i try to
send this message to CShipDlg from my property sheet(m_pBuildSheet), i
get an assertion error.  I would set it up like this(which i think is
incorrect).  This is all within one function in my property sheet
class.

CShipDlg* x;
x->PostMessage(WM_GOODBYE);

here are snippets of code:

        #define WM_GOODBYE WM_USER + 12

This is under my CShipDlg class, right before the
DECLARE_MESSAGE_MAP() and after the AFX brackets.

        afx_msg void OnGoodbye();

This is in my dialog cpp file, after BEGIN_MESSAGE_MAP, and outside of
the AFX_MSG_MAP brackets

        ON_MESSAGE(WM_GOODBYE, OnGoodbye)

This code was added to my CShipDlg, which is the dialog class created
by classwizard, which produces a dialog when the code is run.
        void CShipDlg::OnGoodbye()
        {
                m_pBuildSheet->DestroyWindow();
                delete m_pBuildSheet;
                m_pBuildSheet = NULL;
                MessageBox("Testing 1 2 3...");

        }

NOTE: I can get everything to run fine when i post the message within
the CShipDlg class(messagebox works, etc), so i think my problem is
when i try to create a pointer to my CShipDlg...

I appreciate all/any help that you can give me.  Thanx.

----------------------------------------------
Chris Ahlers

ICQ: 1822282



Wed, 05 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
On Fri, 18 Jun 1999 23:03:29 -0400, Scott McPhillips

Quote:

>Hi Chris.  This is a problem:

> CShipDlg* x;
> x->PostMessage(WM_GOODBYE);

>The pointer x is uninitialized so it is not pointing at anything usable.  You should first change it into a member variable of the property sheet.  When creating the property sheet object, pass it the address of the dialog either as a constructor parameter or with something like:

> m_pBuildSheet->m_pShipDlg = this;

When i try to add a CShipDlg member to the property sheet, i get the
following errors:

projects\Ship\BuildSheet.h(29) : error C2501: 'CShipDlg' : missing
decl-specifiers
c:\projects\Ship\BuildSheet.h(29) : error C2143: syntax error :
missing ';' before '*'
c:\projects\Ship\BuildSheet.h(29) : error C2501: 'm_pShipDlg' :
missing decl-specifiers
Page.cpp
c:\projects\Ship\Buildsheet.h(29) : error C2501: 'CShipDlg' : missing
decl-specifiers
c:\projects\Ship\Buildsheet.h(29) : error C2143: syntax error :
missing ';' before '*'
c:\projects\Ship\Buildsheet.h(29) : error C2501: 'm_pShipDlg' :
missing decl-specifiers
Ship.cpp
c:\projects\Ship\Buildsheet.h(29) : error C2501: 'CShipDlg' : missing
decl-specifiers
c:\projects\Ship\Buildsheet.h(29) : error C2143: syntax error :
missing ';' before '*'
c:\projects\Ship\Buildsheet.h(29) : error C2501: 'm_pShipDlg' :
missing decl-specifiers
ShipDlg.cpp
c:\projects\Ship\Buildsheet.h(29) : error C2501: 'CShipDlg' : missing
decl-specifiers
c:\projects\Ship\Buildsheet.h(29) : error C2143: syntax error :
missing ';' before '*'
c:\projects\Ship\Buildsheet.h(29) : error C2501: 'm_pShipDlg' :
missing decl-specifiers
Generating Code...
Error executing cl.exe.

I don't know how to get around this, although it is probably very easy
to do.

----------------------------------------------
Chris Ahlers

ICQ: 1822282



Wed, 05 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
Chris:

Your Buildsheet.h file does not know about the CShipDlg class. Since you are only using a pointer to this class you can fix this using advance declaration. In Buildsheet.h put

class CShipDlg;    //advance declaration

class CBuildSheet
{
public:
    CShipDlg* m_pShipDlg;

//etc

Quote:
};

Actually, though, if you are only using this pointer to send a message to the dialog (using CWnd::SendMessage), you could declare this pointer to be a CDialog* or even a CWnd*. Then you would not need the advance declaration.

HTH,

David Wilkinson

=============

Quote:

> On Fri, 18 Jun 1999 23:03:29 -0400, Scott McPhillips

> >Hi Chris.  This is a problem:

> > CShipDlg* x;
> > x->PostMessage(WM_GOODBYE);

> >The pointer x is uninitialized so it is not pointing at anything usable.  You should first change it into a member variable of the property sheet.  When creating the property sheet object, pass it the address of the dialog either as a constructor parameter or with something like:

> > m_pBuildSheet->m_pShipDlg = this;

> When i try to add a CShipDlg member to the property sheet, i get the
> following errors:

> projects\Ship\BuildSheet.h(29) : error C2501: 'CShipDlg' : missing
> decl-specifiers
> c:\projects\Ship\BuildSheet.h(29) : error C2143: syntax error :
> missing ';' before '*'
> c:\projects\Ship\BuildSheet.h(29) : error C2501: 'm_pShipDlg' :
> missing decl-specifiers
> Page.cpp
> c:\projects\Ship\Buildsheet.h(29) : error C2501: 'CShipDlg' : missing
> decl-specifiers
> c:\projects\Ship\Buildsheet.h(29) : error C2143: syntax error :
> missing ';' before '*'
> c:\projects\Ship\Buildsheet.h(29) : error C2501: 'm_pShipDlg' :
> missing decl-specifiers
> Ship.cpp
> c:\projects\Ship\Buildsheet.h(29) : error C2501: 'CShipDlg' : missing
> decl-specifiers
> c:\projects\Ship\Buildsheet.h(29) : error C2143: syntax error :
> missing ';' before '*'
> c:\projects\Ship\Buildsheet.h(29) : error C2501: 'm_pShipDlg' :
> missing decl-specifiers
> ShipDlg.cpp
> c:\projects\Ship\Buildsheet.h(29) : error C2501: 'CShipDlg' : missing
> decl-specifiers
> c:\projects\Ship\Buildsheet.h(29) : error C2143: syntax error :
> missing ';' before '*'
> c:\projects\Ship\Buildsheet.h(29) : error C2501: 'm_pShipDlg' :
> missing decl-specifiers
> Generating Code...
> Error executing cl.exe.

> I don't know how to get around this, although it is probably very easy
> to do.

> ----------------------------------------------
> Chris Ahlers

> ICQ: 1822282



Wed, 05 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
On Sat, 19 Jun 1999 13:43:42 -0400, David Wilkinson

Quote:

>Chris:

>Your Buildsheet.h file does not know about the CShipDlg class. Since you are only using a pointer to this class you can fix this using advance declaration. In Buildsheet.h put

>class CShipDlg;    //advance declaration

>class CBuildSheet
>{
>public:
>    CShipDlg* m_pShipDlg;

>//etc
>};

>Actually, though, if you are only using this pointer to send a message to the dialog (using CWnd::SendMessage), you could declare this pointer to be a CDialog* or even a CWnd*. Then you would not need the advance declaration.

>HTH,

>David Wilkinson

Thanx for the help.  I set it up to use CDialog instead and it works
fine.
----------------------------------------------
Chris Ahlers

ICQ: 1822282


Wed, 05 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
One of the worst bug I done to find, yeah I do have experience about it, it
took me few days to find :

problem is here :
 void CShipDlg::OnGoodbye()
 {
 }

It have to be :
 LPARAM CShipDlg::OnGoodbye( WPARAM, LPARAM)
 {
 }

you will damage call stack if you will define it like you did

{r=1}r++{r=0}

Quote:

>I have been able to create a ON_MESSAGE related function that works
>fine, but only when called within my CShipDlg class.  When i try to
>send this message to CShipDlg from my property sheet(m_pBuildSheet), i
>get an assertion error.  I would set it up like this(which i think is
>incorrect).  This is all within one function in my property sheet
>class.

>CShipDlg* x;
>x->PostMessage(WM_GOODBYE);

>here are snippets of code:

> #define WM_GOODBYE WM_USER + 12

>This is under my CShipDlg class, right before the
>DECLARE_MESSAGE_MAP() and after the AFX brackets.

> afx_msg void OnGoodbye();

>This is in my dialog cpp file, after BEGIN_MESSAGE_MAP, and outside of
>the AFX_MSG_MAP brackets

> ON_MESSAGE(WM_GOODBYE, OnGoodbye)

>This code was added to my CShipDlg, which is the dialog class created
>by classwizard, which produces a dialog when the code is run.
> void CShipDlg::OnGoodbye()
> {
> m_pBuildSheet->DestroyWindow();
> delete m_pBuildSheet;
> m_pBuildSheet = NULL;
> MessageBox("Testing 1 2 3...");

> }

>NOTE: I can get everything to run fine when i post the message within
>the CShipDlg class(messagebox works, etc), so i think my problem is
>when i try to create a pointer to my CShipDlg...

>I appreciate all/any help that you can give me.  Thanx.

>----------------------------------------------
>Chris Ahlers

>ICQ: 1822282



Fri, 07 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
On Mon, 21 Jun 1999 10:56:24 +0200, "Roman Rckschloss (Rori)"

Quote:

>One of the worst bug I done to find, yeah I do have experience about it, it
>took me few days to find :

>problem is here :
> void CShipDlg::OnGoodbye()
> {
> }

>It have to be :
> LPARAM CShipDlg::OnGoodbye( WPARAM, LPARAM)
> {
> }

>you will damage call stack if you will define it like you did

>{r=1}r++{r=0}

Damage the call stack?  What does this mean?  I got my program up and
running, and my OnGoodbye function still has no parameters.  Could you
explain what will happen if I 'damage the call stack'?  I have no idea
what this is, but if I should change it to prevent bugs/create a more
stable program let me know...

----------------------------------------------
Chris Ahlers

ICQ: 1822282



Fri, 07 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
This is what MFC will do when it wil call your message handler ( when used
ON_MESSAGE) :

(( message_func)OnGoodBye)( wParam, lParam)

and message_func is declared as function with two params WPARAM & LPARAM
and returns LPARAM as standard windows message functions is

I wrote 'One of the worst bug I done to find' because you can run your code,
and it will looks fine and suddenly will throw memory exception on part of
code where it should not ( logicaly), but it will. I have been looking for
this bug for few days, because I have no idea where and why it happens -
because throwing memory exception was undetrministic.

why damaging call stack - because function will be called and to stack
pointer is added with size of two params LPARAM & WPARAM - then on return
function should decrease this stack to same value as it was before calling -
but function 'thinks' it was called without params and will not decrease
this stack pointer.

Probably your program is runnig OK, because this function is called on the
end, when is app closing. It is little bit more to talk about why it can
stay withou memory exception, why program can run while before it will
crash.

{r=1}r++{r=0}

Quote:

>On Mon, 21 Jun 1999 10:56:24 +0200, "Roman Rckschloss (Rori)"

>>One of the worst bug I done to find, yeah I do have experience about it,
it
>>took me few days to find :

>>problem is here :
>> void CShipDlg::OnGoodbye()
>> {
>> }

>>It have to be :
>> LPARAM CShipDlg::OnGoodbye( WPARAM, LPARAM)
>> {
>> }

>>you will damage call stack if you will define it like you did

>>{r=1}r++{r=0}

>Damage the call stack?  What does this mean?  I got my program up and
>running, and my OnGoodbye function still has no parameters.  Could you
>explain what will happen if I 'damage the call stack'?  I have no idea
>what this is, but if I should change it to prevent bugs/create a more
>stable program let me know...

>----------------------------------------------
>Chris Ahlers

>ICQ: 1822282



Fri, 07 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
On Mon, 21 Jun 1999 17:14:44 +0200, "Roman Rckschloss (Rori)"

Quote:

>>On Mon, 21 Jun 1999 10:56:24 +0200, "Roman Rckschloss (Rori)"

>>>One of the worst bug I done to find, yeah I do have experience about it,
>it
>>>took me few days to find :

>>>problem is here :
>>> void CShipDlg::OnGoodbye()
>>> {
>>> }

>>>It have to be :
>>> LPARAM CShipDlg::OnGoodbye( WPARAM, LPARAM)
>>> {
>>> }

>>>you will damage call stack if you will define it like you did

>>>{r=1}r++{r=0}

Ok, what should I use for those parameters then. So should I just fill
them in with 'junk' variables, which have no usage in my program?

Thanx for your help.  I never before had any idea that this type of
error could occur.

----------------------------------------------
Chris Ahlers

ICQ: 1822282



Fri, 07 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.

Quote:

>I have been able to create a ON_MESSAGE related function that works
>fine, but only when called within my CShipDlg class.  When i try to
>send this message to CShipDlg from my property sheet(m_pBuildSheet), i
>get an assertion error.  I would set it up like this(which i think is
>incorrect).  This is all within one function in my property sheet
>class.

>CShipDlg* x;
>x->PostMessage(WM_GOODBYE);

x is uninitialized and doesn't point to a CShipDlg. Anything can happen, but
you'll most likely crash.

Quote:
>here are snippets of code:

>    #define WM_GOODBYE WM_USER + 12

User-defined messages should start at WM_APP, especially if intended for a
window derived from a control, because many controls base their messages on
WM_USER.

Quote:
>This is under my CShipDlg class, right before the
>DECLARE_MESSAGE_MAP() and after the AFX brackets.

>    afx_msg void OnGoodbye();

>This is in my dialog cpp file, after BEGIN_MESSAGE_MAP, and outside of
>the AFX_MSG_MAP brackets

>    ON_MESSAGE(WM_GOODBYE, OnGoodbye)

Your message handler is declared incorrectly. If you click the "User-defined
Handlers" link in the ON_MESSAGE documentation, you'll find the message
handler needs to be declared as:

 afx_msg LRESULT Handler(WPARAM,LPARAM);

--
Doug Harrison

Visual C++ MVP



Fri, 07 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.
When you PostMessage(WM_GOODBYE), this is the same as doing
PostMessage(WM_GOODBYE, 0, 0), so you are implicitly passing two
parameters all the time anyway. CHeck out how default parameters work
in C++. In any case, if you declare your handler as

LRESULT OnGoodbye(WPARAM, LPARAM)
    {
     ....
     return 0;
    }

by not giving names to the two parameters you are telling the compiler
that you don't care about them. Note that PostMessage and SendMessage
always send both WPARAM and LPARAM, and your handler is always called
with them, it is just part of the spec of how this works.

Several hints about using these messages:

        1. Don't call them WM_ messages. Assume this prefix is
            sacred to Microsoft.  Use some other prefix. I use UWM_
            (User Window Message), but at least make it different
        2. Whenever you define a value in a #define which has a
            numeric computation, ALWAYS put it in parentheses:
                #define UWM_GOODBYE (WM_APP + 12)
        3. Don't use WM_APP+ anything, as controls you've never
            heard of sometimes use these messages and you will get
            royally nuked (it has happened to me all too often).
            Instead, use Registered Window Messages, which are almost
            identical.

static const UINT UWM_GOODBYE = ::RegisterWindowMessage(
      _T("UWM_GOODBYE-{09A03CA0-284F-11d3-8868-00AA00080A1D}")

then use ON_REGISTERED_MESSAGE instead. That suffix, {...}, comes from
the GUIDGEN program which is part of the SDK, and absolutely
positively guarantees that nobody, ever, anywhere, under any
conditions, can conflict with your message. Eliminates thousands of
potential problems. I ended up developing this technique because I got
really, really tired of being done it. I usually do a #define in a
header file:

#define UWM_GOODBYE_MESSAGE \
 _T("UWM_GOODBYE-{09A03CA0-284F-11d3-8868-00AA00080A1D}"

then do

static const UINT UWM_GOODBYE =
::RegisterWindowMessage(UWM_GOODBYE_MESSAGE);

Years of being done in by WM_USER + n messages forced me down this
path. A bit more complex, but utterly reliable.

The #define problem: consider

#define TERM1  2 + 3
#define TERM2  4 + 5

what is the value of A in
        int A = TERM1 * TERM2;  // ?

If you get 19, you understand the problem. If you get 45, study the
problem carefully.


Quote:


>>I have been able to create a ON_MESSAGE related function that works
>>fine, but only when called within my CShipDlg class.  When i try to
>>send this message to CShipDlg from my property sheet(m_pBuildSheet), i
>>get an assertion error.  I would set it up like this(which i think is
>>incorrect).  This is all within one function in my property sheet
>>class.

>>CShipDlg* x;
>>x->PostMessage(WM_GOODBYE);

>x is uninitialized and doesn't point to a CShipDlg. Anything can happen, but
>you'll most likely crash.

>>here are snippets of code:

>>        #define WM_GOODBYE WM_USER + 12

>User-defined messages should start at WM_APP, especially if intended for a
>window derived from a control, because many controls base their messages on
>WM_USER.

>>This is under my CShipDlg class, right before the
>>DECLARE_MESSAGE_MAP() and after the AFX brackets.

>>        afx_msg void OnGoodbye();

>>This is in my dialog cpp file, after BEGIN_MESSAGE_MAP, and outside of
>>the AFX_MSG_MAP brackets

>>        ON_MESSAGE(WM_GOODBYE, OnGoodbye)

>Your message handler is declared incorrectly. If you click the "User-defined
>Handlers" link in the ON_MESSAGE documentation, you'll find the message
>handler needs to be declared as:

> afx_msg LRESULT Handler(WPARAM,LPARAM);

Joseph M. Newcomer

http://www3.pgh.net/~newcomer


Sat, 08 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.



Quote:

> 3. Don't use WM_APP+ anything, as controls you've never
>     heard of sometimes use these messages and you will get
>     royally nuked (it has happened to me all too often).
>     Instead, use Registered Window Messages, which are almost
>     identical.

> static const UINT UWM_GOODBYE = ::RegisterWindowMessage(
>       _T("UWM_GOODBYE-{09A03CA0-284F-11d3-8868-00AA00080A1D}")

So where would you place the above line of code in a typical MFC app? Also,
I thought using WM_APP was supposed to be safe.

Jeff



Sat, 08 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.

Quote:

>    1. Don't call them WM_ messages. Assume this prefix is
>        sacred to Microsoft.  Use some other prefix. I use UWM_
>        (User Window Message), but at least make it different

Good point. Now if I could only convince people not to begin their class
names with C[A-Z] :)

Quote:
>    2. Whenever you define a value in a #define which has a
>        numeric computation, ALWAYS put it in parentheses:
>            #define UWM_GOODBYE (WM_APP + 12)

Another good point. Better still, if you're programming in C++, use:

 const UINT MY_MSG = WM_APP+1;

instead of the macro.

Quote:
>    3. Don't use WM_APP+ anything, as controls you've never
>        heard of sometimes use these messages and you will get
>        royally nuked (it has happened to me all too often).
>        Instead, use Registered Window Messages, which are almost
>        identical.

Which controls? I've used WM_APP a couple of dozen times or so, and I've
never had any problem with it. I definitely don't broadcast it, though. :) I
only found one use of WM_APP in the Windows, ATL, and MFC headers and MFC
source, in <scrnsave.h>, but I didn't get any hits on the message in MSDN:

 #define SCRM_VERIFYPW   WM_APP

The WM_APP docs do say that WM_APP-based messages aren't used by the system,
and that they're available for applications to use as private messages. That
said, if you're subclassing a window, you should definitely review its use
of WM_APP, and if you can't be sure how it uses WM_APP, use a registered
message, as you suggest.

--
Doug Harrison

Visual C++ MVP



Sat, 08 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.

Quote:

>Also,
>I thought using WM_APP was supposed to be safe.

Unfortunately it doesn't spare you from the tender mercies of some
moron who can't be bothered to find the other window he wants to send
to, and just broadcasts a WM_APP+n message. It happens.

Bob Moore [MVP]
http://www.mooremvp.freeserve.co.uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Due to an unreasonable amount of queries, I no
longer answer unsolicited email questions. Sorry,
no exceptions.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Sat, 08 Dec 2001 03:00:00 GMT  
 ON_MESSAGE help.

Quote:

>Unfortunately it doesn't spare you from the tender mercies of some
>moron who can't be bothered to find the other window he wants to send
>to, and just broadcasts a WM_APP+n message. It happens.

Any conspicuous culprits? That's truly a bad thing to do. Given the
well-defined role of WM_APP, I think it would have been a good thing for
SendMessage and friends to reject attempted broadcasts of WM_APP-based
messages. Too late to start, I wonder?

--
Doug Harrison

Visual C++ MVP



Sun, 09 Dec 2001 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. problems with postmessage() and on_message handlers, need Help!!!

2. ON_MESSAGE in main app

3. ON_MESSAGE problem ...

4. error ON_MESSAGE

5. ON_MESSAGE

6. error C2440: 'static_cast' for ON_MESSAGE

7. Compiling ON_MESSAGE

8. SendMessage/ON_MESSAGE crashes MFC42.DLL in Release version

9. ON_MESSAGE error

10. Q: using ON_MESSAGE

11. ON_MESSAGE in threads: Debug vs Release different?

12. Intercept WM_EXITSIZEMOVE with ON_MESSAGE

 

 
Powered by phpBB® Forum Software