MFC Message Map Problem

This is a discussion on MFC Message Map Problem within the C++ Programming forums, part of the General Programming Boards category; For some reason, the compiler is giving me an error when I try to use a custom message. Here's the ...

  1. #1
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215

    MFC Message Map Problem

    For some reason, the compiler is giving me an error when I try to use a custom message. Here's the problem:

    Code:
    error C2440: 'static_cast' : cannot convert from 'void (__thiscall CStageZeroNetworkAnalysisView::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
    And how I implemented it:
    Code:
    //////// View.h
    afx_msg void OnNewPacket(WPARAM wParam, LPARAM lParam);
    DECLARE_MESSAGE_MAP()
    
    
    //////// View.cpp
    BEGIN_MESSAGE_MAP(CStageZeroNetworkAnalysisView, CView)
    
    	ON_MESSAGE(MSG_NEW_PACKET, OnNewPacket)
    END_MESSAGE_MAP()
    
    
    ///////// The function
    void CStageZeroNetworkAnalysisView::OnNewPacket(WPARAM wParam, LPARAM lParam)
    {
    	ZeroPacket* ZP = new ZeroPacket();
    	ZP->SetData((pcap_pkthdr*)wParam, (u_char*)lParam);
    	ZP->Serialize(ZData);
    }
    Any help will be much appreciated. Thanks.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Wrong Forum, please delete.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    yes, I get the logic in the error. My question is, why is it coming up? The message map is in the class that generates the error, its NOT in CWnd. CWnd is a base class that you cannot get access to except for overriding, so adding the function to it is impossible.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The BEGIN MESSAGE MAP macro needs a function that returns an LRESULT not void?

  5. #5
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I tried it. All of the other messages supplied by windows uses the same exact syntax that I'm using. I'm using .NET 2003 and god do I miss the class wizard.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    and god do I miss the class wizard.
    lol. Which line is the error on?

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    hehe, its on this line:

    Code:
    ON_MESSAGE(MSG_NEW_PACKET, OnNewPacket)
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The error message says you need an LRESULT as a return value. I looked at some declarations of the event functions in a simple Visual C++ project I'm working on, and not all of them return void, and most don't have any parameters. So, if you are sending a function pointer to Windows, maybe you need to return an LRESULT, which I can't find much info on.

    Time to go ask the experts in the windows programming forum.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Changing the return type and making sure that afx_msg is used should at least change the error message Are you declaring everything exactly as it is in this site http://www.flounder.com/messages.htm#WM_USER ?

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    MSDN says you need an LRESULT return type as well:

    http://msdn.microsoft.com/library/de...d_handlers.asp

    and LRESULT is a typedef for LONG_PTR:

    typedef LONG_PTR LRESULT;

    and LONG_PTR is a typedef for a long:

    #if defined(_WIN64)
    typedef __int64 LONG_PTR;
    #else
    typedef long LONG_PTR;
    #endif

    http://msdn.microsoft.com/library/de...data_types.asp

    Some of the examples I saw that don't want to use the return value just cast 0 to an LRESULT type, and return it as the last line of their function, e.g.

    return (LRESULT)0;

    You might want to use the C++ static_cast instead to make a C++ style cast. Check out this short article on user defined messages:

    http://www.codeguru.com/Cpp/misc/misc/article.php/c301/
    Last edited by 7stud; 10-31-2005 at 01:59 PM.

  11. #11
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Thanks guys. It was my fault, I tried using LRESULT once before and it gave me a couple errors and without even looking at em I assumed it was the return type. I just got it working tho. Thanks again!
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with static STL i.e. map
    By vijay_choudhari in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2007, 06:56 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. Problems with view's message map in MFC
    By ubermensch in forum Windows Programming
    Replies: 1
    Last Post: 05-27-2006, 07:21 PM
  4. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 06:05 AM
  5. ListView ImageList problem (MFC)
    By The Dog in forum Windows Programming
    Replies: 1
    Last Post: 09-12-2002, 01:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21