Solved

static_cast error with Visual Studio .NET but not with VC6

Posted on 2003-10-31
3
587 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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