[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

static_cast error with Visual Studio .NET but not with VC6

Posted on 2003-10-31
3
Medium Priority
?
618 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
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 …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 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