Solved

static_cast error with Visual Studio .NET but not with VC6

Posted on 2003-10-31
3
582 Views
Last Modified: 2008-02-01
I use a third party code to implement a plug-in architecture in my application (the code: http://www.codeproject.com/library/piarchitecture.asp).

The code works fine with the compiler of VC6 but when I compile with VS .NET I received a lot of errors C2440 like this one:

static_cast' : cannot convert from 'int (__thiscall CViewInPlugIn::* )(LPCREATESTRUCT)' to 'int (__thiscall CWnd::* )(LPCREATESTRUCT)'
 
CViewInPlugIn is derived from CPlugInMap and CPlugInMap is derived from CCmdTarget. Maybe the new compiler can't cast a CCmdTarget class to a CWnd class? I know that VS.NET Compiler is more strict with Message Map but I don't know how to resolve or bypass this problem.

=============================================
Here some parts of my code:

class CViewInPlugIn : public CPlugInMap  
{
................
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CViewInPlugIn)
      afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
      //}}AFX_VIRTUAL
................
};


//Here my implementation
BEGIN_MESSAGE_MAP(CViewInPlugIn, CPlugInMap)
      //{{AFX_MSG_MAP(CViewInPlugIn)
      ON_WM_CREATE()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

int CViewInPlugIn::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      ...............

      return TRUE ;
}

and the declaration of CPlugInMap:
class _PLUGIN_API CPlugInMap : public CCmdTarget
{
      friend class CPlugInApp ;
      DECLARE_DYNCREATE(CPlugInMap)

......................

      DECLARE_MESSAGE_MAP()
};
0
Comment
Question by:Frankbe
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 9660628
I reproduced this error compiling the sample application from CodeProject. PlugIn1 project, MFExamplePI.cpp, lines:

BEGIN_MESSAGE_MAP(CMFPI, CPlugInMap)
    //{{AFX_MSG_MAP(CMFPI)
    ON_WM_CREATE()
    ON_COMMAND(ID_VIEW_MYNEWTOOLBAR, OnViewMyNewToolbar)
    ON_UPDATE_COMMAND_UI(ID_VIEW_MYNEWTOOLBAR, OnUpdateViewMyNewToolbar)
    ON_COMMAND(ID_FILE_SOMENEWOPTION, OnSomeNewOption)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

The same error message in ON_WM_CREATE line. Solution - add the following lines in the beginning of cpp file:

#undef ON_WM_CREATE
#define ON_WM_CREATE() \
    { WM_CREATE, 0, 0, 0, AfxSig_is, \
        (AFX_PMSG) (AFX_PMSGW) \
        (reinterpret_cast< int (AFX_MSG_CALL CWnd::*)(LPCREATESTRUCT) > (OnCreate)) },

This is ON_WM_CREATE macro with static_cast replaced by reinterpret_cast. Not so nice, but should work.
0
 

Author Comment

by:Frankbe
ID: 9667051
When you execute the compiled program, do you receive an unhandled exception : access violation reading location 0x00000000?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9669246
Actually, I didn't execute it at all. Possibly it contains some bugs, I don't know. If this happens also in VC++ 6, this is not result of ON_WM_CREATE compilation problem.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now