Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On_Message & On_Command issues when porting c++/MFC code to c++.NET

Programming

On_Message & On_Command issues when porting c++/MFC code to c++.NET

by  fiaery  Posted    (Edited  )
I'm assuming the reader has done some user defined messages in MFC.

In previous versions of Visual c++ (6.0), message handlers for On_Message and On_Command messages could be used interchangeably because of the macro that defined On_message in afxmsg_.h. The old macro code is as follows:

// from afxmsg_.h VC++ 6
#define ON_MESSAGE(message, mbrFn) {message, 0, 0, 0, AfxSig_lwl, (AFX_PMSG)(AFX_PMSGW)(LRESULT (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))&mbrFn },

whereby the return value of the message handler is cast to the appropriate type for On_message, which requires a LRESULT returned from the handler, or a AFX_PMSG for the On_command.

However in c++.net the macro now uses a static cast and this may cause problems when you try to port a MFC program to c++.net; the new macro is shown below

// for Windows messages, from afxmsg_.h
#define ON_MESSAGE(message, memberFxn) { message, 0, 0, 0, AfxSig_lwl, (AFX_PMSG)(AFX_PMSGW) (static_cast< LRESULT (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM) > (memberFxn)) },

If you have used message handlers interchangeably for both On_command and On_message in VC++ 6, compiling your code in .net will generate a C2440 error: 'static_cast' : cannot convert from 'void* (__thiscall CStethoSoftView::* )(void)' to LRESULT (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM)'.

The declaration for my message handler is:

afx_msg void OnPlay();


Two options are now available, one is that you revert the afxmsg_.h to the old macro (which I do not recommend) or explicitly cast the handler to the LRESULT type needed in On_message.

Previously my message map was:

ON_MESSAGE(WM_USER_PLAY, OnPlay)

This is what I did for my problem:

ON_MESSAGE(WM_USER_PLAY,(LRESULT(AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))OnPlay)

This seems to have fixed the problem. More Information at http://www.microsoft.com/msj/0399/c/c0399.aspx
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top