CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Feb 2002
    Posts
    3,788

    Error when converting VC 6 project

    I've been out of the VC world for years now. I'm trying to build an old project and I'm getting this error:

    Code:
    Error	2	error C2440: 'static_cast' : cannot convert from 'BOOL (__thiscall CMainFrame::* )(void)' to 'AFX_PMSG'	d:\user\alin\vc++\xcp_diag v 1.0.0 beta4\mainfrm.cpp	99	1	XCP
    Mainframe.cpp:
    Code:
    BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
    	//{{AFX_MSG_MAP(CMainFrame)
    	ON_WM_CREATE()
    	ON_COMMAND(ID_STOP_LOG, OnStopLog)
    	ON_COMMAND(ID_START_LOG, OnStartLog)
    	ON_UPDATE_COMMAND_UI(ID_START_LOG, OnUpdateStartLog)
    	ON_UPDATE_COMMAND_UI(ID_STOP_LOG, OnUpdateStopLog)
    	ON_COMMAND(ID_APP_SET_ADDR_INTEL, OnAppSetAddrIntel)
    	ON_COMMAND(ID_APP_SET_ADDR_MOT, OnAppSetAddrMotorola)
    	ON_UPDATE_COMMAND_UI(ID_APP_SET_ADDR_INTEL, OnUpdateAppSetAddrIntel)
    	ON_UPDATE_COMMAND_UI(ID_APP_SET_ADDR_MOT, OnUpdateAppSetAddrMotorola)
    	ON_WM_TIMER()
    	ON_WM_CLOSE()
    	ON_COMMAND(ID_FILE_SAVE_LOG, OnSaveLogFile) --> This is where the compiler reports the error.
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    Mainframe.h:

    Code:
    afx_msg BOOL OnSaveLogFile();
    Thanks,
    Alin

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Error when converting VC 6 project

    I mean to remember that I had the same error some years ago when porting a VC6 project to VC8 (VS2005).

    The ON_COMMAND macro tries to "cast" a function pointer of a member function of the current class to the signature the handler function of a WM_COMMAND message does require. If this fails in a later version of VC it could be either because the signature has changed in the meanwhile, i. e. the OnCommand handler needs now a different interface, or because the compiler wouldn't accept that kind of cast at all. The latter is less likely if you made sure that the ON_COMMAND macro comes from a header of the new compiler version and not from VC6, i. e. if you don't have a mixed environment.

    If you could post the definition of the AFX_PMSG macro (press F12 on that macro) I probably would remember what I did to solve the issue.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Error when converting VC 6 project

    AFAIK, the message handler signature for ON_COMMAND macro is
    Code:
    afx_msg [B]void/B] OnSomething(void)[
    *not* afx_msg BOOL ...
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Error when converting VC 6 project

    Oh, CodePlug did remember (google) faster than me

  6. #6
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Error when converting VC 6 project

    thanks for the replies.

    i still remember the ON_COMMAND signature is void ), but at the same time my function returns BOOL and i need it to be that way. is there a cast i could try to make it accept BOOL like afx_msg BOOL OnSaveLogFile()?

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Error when converting VC 6 project

    Hi Alin,

    Quote Originally Posted by Alin View Post
    is there a cast i could try to make it accept BOOL like afx_msg BOOL OnSaveLogFile()?
    Nope. Not good. No way.
    MSDN
    WM_COMMAND Message
    Return Value
    If an application processes this message, it should return zero.
    And that MFC does.
    However, if really makes sense for you to return other value, use ON_MESSAGE macro instead.
    Although I'm wondering if something like that has any sense or effect.
    Last edited by ovidiucucu; January 13th, 2011 at 05:34 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #8
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Error when converting VC 6 project

    Code:
    afx_msg void MMOnSaveLogFile() {OnSaveLogFile();}
    Use that in your message map.

    gg

  9. #9
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Error when converting VC 6 project

    Quote Originally Posted by Codeplug View Post
    Code:
    afx_msg void MMOnSaveLogFile() {OnSaveLogFile();}
    Use that in your message map.

    gg
    That did the trick, thanks very much.

  10. #10
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Error when converting VC 6 project

    my function returns BOOL and i need it to be that way
    A BOOL return in a message map function is senseful if there is a default handling which you can invoke by returning FALSE what means "not handled". As MS did change the signature for OnCommand handlers you can be sure there is no default handling. Hence returning a BOOL or not makes no difference. So I would change the signature of my handler function in order to have a correct interface and remove the return statements accordingly.

    If you need a BOOL return cause you need to call the function from another function, you better rename the old function to 'SaveLogFile' and call the void function OnSaveLogFile.

  11. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Error when converting VC 6 project

    Quote Originally Posted by Alin View Post
    That did the trick, thanks very much.
    Still wondering. Has it any effect, or it's only to compile?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  12. #12
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Error when converting VC 6 project

    Quote Originally Posted by ovidiucucu View Post
    Still wondering. Has it any effect, or it's only to compile?
    In VC8 help they described ON_COMMAND_EX with

    An extended form of command message handlers is available for advanced uses. The ON_COMMAND_EX macro is used for such message handlers, and it provides a superset of the ON_COMMAND functionality. Extended command-handler member functions take a single parameter, a UINT containing the command ID, and return a BOOL
    So it looks as ON_COMMAND_EX has now the functionality of former ON_COMMAND and that newer ON_COMMAND only will accept a void handler.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Error when converting VC 6 project

    Quote Originally Posted by itsmeandnobodyelse View Post
    In VC8 help they described ON_COMMAND_EX with
    ...
    So it looks as ON_COMMAND_EX has now the functionality of former ON_COMMAND and that newer ON_COMMAND only will accept a void handler.
    This ON_COMMAND_EX looks like the old ON_CONTROL_REFLECT macro. Doesn't it?
    Victor Nijegorodov

  14. #14
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Error when converting VC 6 project

    Don't know exactly but I mean to remember that the _REFLECT macros were to redirect messages normally sent to the parent dialog to the (derived) control itself.

    See here for details: http://msdn.microsoft.com/de-de/libr...(v=vs.80).aspx

  15. #15
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Error when converting VC 6 project

    >> ... ON_COMMAND_EX has now the functionality of former ON_COMMAND and that newer ON_COMMAND only will accept a void handler.
    http://msdn.microsoft.com/en-us/libr...8VS.60%29.aspx
    They've both been the same since 6.0.
    The original code compiled under 6.0 either because the conversion was forced or the compiler silently accepted it.

    gg

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured