MSDN > Home page del forum > Visual C++ Language > C6.0->VS2005 -user messsages- ON_MESSAGE error C2440: 'static_cast'
Formula una domandaFormula una domanda
 

Con rispostaC6.0->VS2005 -user messsages- ON_MESSAGE error C2440: 'static_cast'

  • martedì 25 aprile 2006 13.47Les06291979 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    Help Needed:

    I'm moving a program from C6.0 to Visual Studio 2005 C++.  It's an MFC program.  The problem I am having is with ON_MESSAGE handling user messages.  The compiler is not happy with the function prototype for the message handler.  I've made the needed changes to the code to satisfy the stronger type checking of this compliler, but don't understand the 'static_cast' comment.  I've tried making the function 'static', but it makes no difference to the compiler.

    [from acquiredoc.h]

    public:

    afx_msg LRESULT OnSetCameraParms(WPARAM w, LPARAM l);

    [from acquiredoc.cpp, lines 135-140]

    BEGIN_MESSAGE_MAP(CAcquireDoc, CDocument)

    //{{AFX_MSG_MAP(CAcquireDoc)

    //}}AFX_MSG_MAP

    // ON_COMMAND_RANGE(IDM_GET_IMAGE_A, IDM_GET_IMAGE_C, OnGetImage)

    ON_MESSAGE(WM_USER_MSG_IDM_PARMS, OnSetCameraParms) -- the problem line --

    END_MESSAGE_MAP()

    The compiler generates the following error:

    acquiredoc.cpp(139) : error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CAcquireDoc::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'

    None of the functions with this name in scope match the target type

    I've seen many comments about this problem in other postings.  I've implemented the recommended changes, but still have not managed to overcome this issue.  What am I missing?

     

     

Risposte

  • mercoledì 26 aprile 2006 8.30Martin RichterMVP, ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta

    CDocumets can handle only ON_COMMAND messages. Windows messges are not possible and they doesn't make sense, because a CDocuent has no WndProc!

    This might be a relict from old VC6 times were message maps and their handlers were not checked properly. Also I suppose that this code was never called and used.

  • mercoledì 26 aprile 2006 12.32Les06291979 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta

    Truth is stranger than fiction.

    I spent a great deal of time yesterday looking for other solutions.  The result I posted in my original posting is the CORRECT solution.  However, as luck would have it, the first errors were showing up in a CDocument Class.

    As you noted, CDocument does not process ON_MESSAGE, but C6.0 never complained about this.  I checked the two handlers I had in the CDocument class and it is true.  They were never used.

    I have removed the unused handlers, made the correct prototype changes to all the remaining functions that are acccessed via ON_MESSAGE and the program compiles and runs.  Now to see if it still runs correctly.

    Thanks,

    Les

    P.S. Just for future reference for any other who may read this:

    ON_MESSAGE connot be handled by CDocument classes.

    The prototype in the header file should look like this:

    afx_msg LRESULT functionname(WPARAM, LPARAM);

    Example

    (from AcquireFrame.h)

    public:

    afx_msg LRESULT OnShowAcqWindow(WPARAM, LPARAM);

    (from AcquireFrame.cpp)

    BEGIN_MESSAGE_MAP(CAcquireFrame, CMDIChildWnd)

    ON_MESSAGE(WM_SHOW_ACQUIRE_WINDOW, OnShowAcqWindow)

    END_MESSAGE_MAP()

     

    LRESULT CAcquireFrame::OnShowAcqWindow(WPARAM w, LPARAM l)

    {

    ActivateFrame();

    return 0;

    }

     

Tutte le risposte

  • mercoledì 26 aprile 2006 0.54Jonathan Caves - MSFTModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    I suspect that this line:

    ON_MESSAGE(WM_USER_MSG_IDM_PARMS, OnSetCameraParms) 

    should be something like:

    ON_MESSAGE(WM_USER_MSG_IDM_PARMS, &XXX::OnSetCameraParms) 

    where XXX is the class in which OnSetCameraParams is defined.

  • mercoledì 26 aprile 2006 8.30Martin RichterMVP, ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta

    CDocumets can handle only ON_COMMAND messages. Windows messges are not possible and they doesn't make sense, because a CDocuent has no WndProc!

    This might be a relict from old VC6 times were message maps and their handlers were not checked properly. Also I suppose that this code was never called and used.

  • mercoledì 26 aprile 2006 12.11Les06291979 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    I tried several flavors of that idea yesterday to no avail.  But I have to confess that I didn't try the &.

    The problem is solved. If you could take a look at the next reply, I'll go into details.

    Thanks for the assist.

    Les

  • mercoledì 26 aprile 2006 12.32Les06291979 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta

    Truth is stranger than fiction.

    I spent a great deal of time yesterday looking for other solutions.  The result I posted in my original posting is the CORRECT solution.  However, as luck would have it, the first errors were showing up in a CDocument Class.

    As you noted, CDocument does not process ON_MESSAGE, but C6.0 never complained about this.  I checked the two handlers I had in the CDocument class and it is true.  They were never used.

    I have removed the unused handlers, made the correct prototype changes to all the remaining functions that are acccessed via ON_MESSAGE and the program compiles and runs.  Now to see if it still runs correctly.

    Thanks,

    Les

    P.S. Just for future reference for any other who may read this:

    ON_MESSAGE connot be handled by CDocument classes.

    The prototype in the header file should look like this:

    afx_msg LRESULT functionname(WPARAM, LPARAM);

    Example

    (from AcquireFrame.h)

    public:

    afx_msg LRESULT OnShowAcqWindow(WPARAM, LPARAM);

    (from AcquireFrame.cpp)

    BEGIN_MESSAGE_MAP(CAcquireFrame, CMDIChildWnd)

    ON_MESSAGE(WM_SHOW_ACQUIRE_WINDOW, OnShowAcqWindow)

    END_MESSAGE_MAP()

     

    LRESULT CAcquireFrame::OnShowAcqWindow(WPARAM w, LPARAM l)

    {

    ActivateFrame();

    return 0;

    }

     

  • giovedì 15 giugno 2006 17.13Mandana Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    Thanks alot, It really helped me and you explained it in wonderful format.

    I appriciate it. you Saved my time and I feel great.

  • venerdì 16 giugno 2006 10.48Les06291979 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Thanks.  But without the help I was given, it wouldn't have been possible.   I'm glad  I took the time to summarize what others told me.

    Les
  • sabato 3 febbraio 2007 14.19Bin Zhou Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    Thanks a lot.

    I tried several post sites until find your post!

    wishes,

    Bin