inheriting the VC++ template classes into C#.net

A

Amu

Hi i am stuck up in a part of project where i have to inherit the VC++
template classes into C#.net. Please provide me the solution for this .
 
A

Amu

Mathieu said:
Amu a écrit :
I may be wrong, but I really believe that composition
(http://en.wikipedia.org/wiki/Object_composition) is the only way to
achieve what you are trying to.

Mathieu

i read the solution but can u give me some clear idea regarding
it.actually i have a dll ( template class) ready which is written in
VC++6. But presently i need to inherit its classes into my new C#./net
project.so if there is some better solution with u then please give me
the solution.
:Amu
 
A

Andre Kaufmann

Amu said:
Hi i am stuck up in a part of project where i have to inherit the VC++
template classes into C#.net. Please provide me the solution for this .

Hi,

best solution IMHO would be to use VC++ 2005 and write some wrapper
classes in C++/CLI. Which then can be directly used from C#.
You can use generics and map them to the associated template class.
Though generics are more restricted than templates, so you possibly
can't directly wrap the template classes by using generics.

Another solution would be wrapping the template classes in COM
interfaces or by using flat functions. Surely with this method you can't
use template / generic parameters.

VC++ 2005 Express Edition, can be downloaded from Microsoft.

Hope that helps,
Andre
 
A

Amu

Andre said:
Hi,

best solution IMHO would be to use VC++ 2005 and write some wrapper
classes in C++/CLI. Which then can be directly used from C#.
You can use generics and map them to the associated template class.
Though generics are more restricted than templates, so you possibly
can't directly wrap the template classes by using generics.

Another solution would be wrapping the template classes in COM
interfaces or by using flat functions. Surely with this method you can't
use template / generic parameters.

VC++ 2005 Express Edition, can be downloaded from Microsoft.

Hope that helps,
Andre



The C++ classes which I mentioned are classes which are derived from
MFC standard classes like CPropertyPage and CPropertySheet.

These template classes are simple MFC classes which are packaged as a
DLL. Since the Implementation part of this DLL is huge, I can't think
of rewriting the whole code in C#.



I have attached the the class template code snippet for your reference.
I need to inherit from this class, DTMPOPropertyPage in C#.

How can I achieve this?



class AFX_EXT_CLASS DTMPOPropertyPage : public CPropertyPage

{



// Construction

public:



//DECLARE_DYNAMIC(DTMPOPropertyPage)



DTMPOPropertyPage(FDTXmtrDoc *doc, UINT nIDTemplate = 0,
UINT nCaption = 0);

~DTMPOPropertyPage();



//{{AFX_DATA(CXmtrPropertyPage)

// NOTE - ClassWizard will add data members
here.

// DO NOT EDIT what you see in these blocks
of generated code !

//}}AFX_DATA





// Attributes

public:

// Access to determine if performing CANCEL processing.

// Pages use this to avoid validation

//BOOL InCancelProcessing ();

// Overrides

// ClassWizard generate virtual function overrides

//{{AFX_VIRTUAL(CXmtrPropertyPage)



// Define a generic OnKillFocus handler for use by all
pages

//afx_msg void OnKillFocus();



virtual void DoDataExchange(CDataExchange* pDX); //
DDX/DDV support

//}}AFX_VIRTUAL



virtual BOOL OnInitDialog() { return
CPropertyPage::OnInitDialog(); }

virtual BOOL OnSetActive() { UpdateData(FALSE); return
CPropertyPage::OnSetActive(); }

//virtual BOOL OnKillActive();



// access to BO parameters

virtual void SetValues() = 0;

virtual void UpdateDocument() = 0;



// access to AppInfo

FDTAppInfo *GetAppInfo();



// Implementation

protected:

// Generated message map functions

//{{AFX_MSG(CXmtrPropertyPage)

// NOTE: the ClassWizard will add member
functions here

//}}AFX_MSG

//afx_msg LRESULT OnCommandHelp(WPARAM, LPARAM lParam);

//afx_msg LRESULT OnShiftF1Help(WPARAM, LPARAM lParam);

//afx_msg void OnHelp( );



afx_msg BOOL OnHelpInfo( HELPINFO* lpHelpInfo );





//_DLLPROC DECLARE_MESSAGE_MAP()

DTMPOPropertySheet *GetSheet();



// pointer to document

FDTXmtrDoc *mDoc;



private:

void SetSheet( DTMPOPropertySheet *ps );

friend class DTMPOPropertySheet;



mDTMPOPropertyPage *m;

};
 
A

Amu

Andre said:
Hi,

best solution IMHO would be to use VC++ 2005 and write some wrapper
classes in C++/CLI. Which then can be directly used from C#.
You can use generics and map them to the associated template class.
Though generics are more restricted than templates, so you possibly
can't directly wrap the template classes by using generics.

Another solution would be wrapping the template classes in COM
interfaces or by using flat functions. Surely with this method you can't
use template / generic parameters.

VC++ 2005 Express Edition, can be downloaded from Microsoft.

Hope that helps,
Andre

The C++ classes which I mentioned are classes which are derived from
MFC standard classes like CPropertyPage and CPropertySheet.

These template classes are simple MFC classes which are packaged as a
DLL. Since the Implementation part of this DLL is huge, I can't think
of rewriting the whole code in C#.



I have attached the the class template code snippet for your reference.
I need to inherit from this class, DTMPOPropertyPage in C#.

How can I achieve this?



class AFX_EXT_CLASS DTMPOPropertyPage : public CPropertyPage

{



// Construction

public:



//DECLARE_DYNAMIC(DTMPOPropertyPage)



DTMPOPropertyPage(FDTXmtrDoc *doc, UINT nIDTemplate = 0,
UINT nCaption = 0);

~DTMPOPropertyPage();



//{{AFX_DATA(CXmtrPropertyPage)

// NOTE - ClassWizard will add data members
here.

// DO NOT EDIT what you see in these blocks
of generated code !

//}}AFX_DATA





// Attributes

public:

// Access to determine if performing CANCEL processing.

// Pages use this to avoid validation

//BOOL InCancelProcessing ();

// Overrides

// ClassWizard generate virtual function overrides

//{{AFX_VIRTUAL(CXmtrPropertyPage)



// Define a generic OnKillFocus handler for use by all
pages

//afx_msg void OnKillFocus();



virtual void DoDataExchange(CDataExchange* pDX); //
DDX/DDV support

//}}AFX_VIRTUAL



virtual BOOL OnInitDialog() { return
CPropertyPage::OnInitDialog(); }

virtual BOOL OnSetActive() { UpdateData(FALSE); return
CPropertyPage::OnSetActive(); }

//virtual BOOL OnKillActive();



// access to BO parameters

virtual void SetValues() = 0;

virtual void UpdateDocument() = 0;



// access to AppInfo

FDTAppInfo *GetAppInfo();



// Implementation

protected:

// Generated message map functions

//{{AFX_MSG(CXmtrPropertyPage)

// NOTE: the ClassWizard will add member
functions here

//}}AFX_MSG

//afx_msg LRESULT OnCommandHelp(WPARAM, LPARAM lParam);

//afx_msg LRESULT OnShiftF1Help(WPARAM, LPARAM lParam);

//afx_msg void OnHelp( );



afx_msg BOOL OnHelpInfo( HELPINFO* lpHelpInfo );





//_DLLPROC DECLARE_MESSAGE_MAP()

DTMPOPropertySheet *GetSheet();



// pointer to document

FDTXmtrDoc *mDoc;



private:

void SetSheet( DTMPOPropertySheet *ps );

friend class DTMPOPropertySheet;



mDTMPOPropertyPage *m;

};
 
M

Mathieu Cartoixa

Amu a écrit :
The C++ classes which I mentioned are classes which are derived from
MFC standard classes like CPropertyPage and CPropertySheet.

These template classes are simple MFC classes which are packaged as a
DLL. Since the Implementation part of this DLL is huge, I can't think
of rewriting the whole code in C#.



I have attached the the class template code snippet for your reference.
I need to inherit from this class, DTMPOPropertyPage in C#.

How can I achieve this?



class AFX_EXT_CLASS DTMPOPropertyPage : public CPropertyPage

{
// Construction
public:
//DECLARE_DYNAMIC(DTMPOPropertyPage)
DTMPOPropertyPage(FDTXmtrDoc *doc, UINT nIDTemplate = 0,
UINT nCaption = 0);
~DTMPOPropertyPage();
//{{AFX_DATA(CXmtrPropertyPage)
// NOTE - ClassWizard will add data members
here.
// DO NOT EDIT what you see in these blocks
of generated code !
//}}AFX_DATA

// Attributes
public:
// Access to determine if performing CANCEL processing.
// Pages use this to avoid validation
//BOOL InCancelProcessing ();
// Overrides
// ClassWizard generate virtual function overrides
//{{AFX_VIRTUAL(CXmtrPropertyPage)
// Define a generic OnKillFocus handler for use by all
pages
//afx_msg void OnKillFocus();
virtual void DoDataExchange(CDataExchange* pDX); //
DDX/DDV support
//}}AFX_VIRTUAL
virtual BOOL OnInitDialog() { return
CPropertyPage::OnInitDialog(); }
virtual BOOL OnSetActive() { UpdateData(FALSE); return
CPropertyPage::OnSetActive(); }
//virtual BOOL OnKillActive();
// access to BO parameters
virtual void SetValues() = 0;
virtual void UpdateDocument() = 0;
// access to AppInfo
FDTAppInfo *GetAppInfo();
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CXmtrPropertyPage)
// NOTE: the ClassWizard will add member
functions here
//}}AFX_MSG
//afx_msg LRESULT OnCommandHelp(WPARAM, LPARAM lParam);
//afx_msg LRESULT OnShiftF1Help(WPARAM, LPARAM lParam);
//afx_msg void OnHelp( );
afx_msg BOOL OnHelpInfo( HELPINFO* lpHelpInfo );
//_DLLPROC DECLARE_MESSAGE_MAP()
DTMPOPropertySheet *GetSheet();
// pointer to document
FDTXmtrDoc *mDoc;
private:
void SetSheet( DTMPOPropertySheet *ps );
friend class DTMPOPropertySheet;
mDTMPOPropertyPage *m;
};

I am sorry to write that you will have to forget about inheriting from
this class.
I'm far from being fluent in Managed C++, so I redirect you to these
resources about implementing a wrapper (i.e. a class that uses
composition) :
http://www.ondotnet.com/pub/a/dotnet/2004/03/29/mcpp_part3.html
http://www.codeguru.com/Cpp/Cpp/cpp_managed/interop/article.php/c6867/

Mathieu
 
A

Andre Kaufmann

Amu said:
Andre said:
Amu wrote:
[...]
I have attached the the class template code snippet for your reference.
I need to inherit from this class, DTMPOPropertyPage in C#.

How can I achieve this?

As some posters already have mentioned, you can't derive directly from a
native C++ class in C#. You can wrap the class in a managed one and
embed the native class in the managed as a pointer. Directly embedding,
meaning mixing managed and native classes is not (yet) possible.

A short example gives you the idea:

(Create for example a class library in VC++ 2005,
or a CLR console application for testing):


using namespace System;

// Native C++ class - that's for example your DTMPOPropertyPage class
class Native
{
public: void Method() {}
};

// You can create and inherit from this class in C#
// It's a managed class and should be visible in a C#
// project if you add a reference to the DLL.
public ref class Managed
{
public:

Managed() { native = new Native(); }
~Managed() { delete native; }

// Function callable from C#
void External() { native->Method(); }

private:
Native* native;
};

Anyways mixing MFC code and C++/CLI (managed) code is not that simple,
especially if GUI components / classes are involved. There are some
examples for this type of application in the MSDN library.

You have no chance to do the above in VC6. If you must use VC6 your only
chance (IMHO) is to wrap your classes by using handles and flat
functions (just the way Win32 is doing it) and use DLLImport to call the
functions - though IMHO to much work. Or wrap your native classes in COM
objects - but IMHO quite too much work.

So you should give VC++ 2005 a try.

Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top