There are a lot of arguments about the advantages and disadvantages of C # And C ++, and I don't like such arguments. It seems that I have to let the other party die and then go fast. Our great Chairman Mao said: The Hundred Flowers are fighting and the Hundred Flowers are playing together. Deng Xiaoping, chief designer of reform and opening up, also said: black cats and white cats are good cats. (Haha, it's a little too far away. The angry youth should never hit bricks .) What I want to say is: in fact, both languages have their own strengths. Learning from each other is the final principle. C # excellent performance in XML reading and writing, database manipulation, interface construction, and many other aspects; high efficiency of C ++ is an essential weapon for underlying development. Of course in. the functions of C ++/CLI on the. NET platform become more and more powerful, but after all, most people are not familiar with managed C ++, it is not very convenient to use (after all, there are too many blended items). It is rare to use C ++/CLI for development. However, in actual development, we can use a small amount of managed C ++ code to bond and Package C # And C ++, C # And C ++ do their part of the real work and give full play to their respective expertise, so that a small amount of code can open the second pulse of Ren du.
This article describes how to call the content in the pure C ++ module in the C # code.
In this example, the C # interface calls the addition function of C ++ to add two strings. Of course, managed C ++ is used as a bridge. The implementation steps are as follows:
First, create a pure C ++ static library ---- purecppslib, which implements a class for adding strings. (Step omitted)
Class purecclass
{
Public:
Purecclass ();
~ Purecclass (void );
Public:
Lpctstr getfirstname ();
Void setfirstname (lpctstr fistname );
Lpctstr getlastname ();
Void setlastname (lpctstr lastname );
Lpctstr joinname ();
PRIVATE:
Cstring m_firstname;
Cstring m_lastname;
Cstring m_fullname;
};
Purecclass: purecclass ()
{
}
Purecclass ::~ Purecclass (void)
{
}
Lpctstr purecclass: getfirstname ()
{
Return (lpctstr) m_firstname;
}
Void purecclass: setfirstname (lpctstr fistname)
{
M_firstname = fistname;
}
Lpctstr purecclass: getlastname ()
{
Return (lpctstr) m_lastname;
}
Void purecclass: setlastname (lpctstr lastname)
{
M_lastname = lastname;
}
Lpctstr purecclass: joinname ()
{
M_fullname = m_firstname + _ T ("") + m_lastname;
Return (lpctstr) (m_fullname );
}
Step 2: Create a managed dynamic link library project named mgdlib.
Import the Lib file of purecppslib in the hosting project, include the header file of the purecclass class, and construct a Managed class-mgclass to implement managed packaging of the class purecclass. This Managed class is placed in namespace mgdlib.
Namespace mgdlib {
Public ref class mgclass
{
Public:
Mgclass ();
Protected:
! Mgclass ();
Public:
~ Mgclass ();
Property string ^ firstname
{
String ^ get ();
Void set (string ^ Str );
}
Property string ^ lastname
{
String ^ get ();
Void set (string ^ Str );
}
String ^ joinname ();
PRIVATE:
Purecclass * m_pimpobj;
};
}
Namespace mgdlib {
Mgclass: mgclass ()
{
M_pimpobj = new purecclass ();
}
Mgclass ::! Mgclass ()
{
Delete m_pimpobj;
}
Mgclass ::~ Mgclass ()
{
This->! Mgclass ();
}
String ^ mgclass: firstname: Get ()
{
Return gcnew string (m_pimpobj-> getfirstname ());
}
Void mgclass: firstname: Set (string ^ Str)
{
Pin_ptr <const wchar> wch = ptrtostringchars (STR );
M_pimpobj-> setfirstname (STD: wstring) wch). c_str ());
}
String ^ mgclass: lastname: Get ()
{
Return gcnew string (m_pimpobj-> getlastname ());
}
Void mgclass: lastname: Set (string ^ Str)
{
Pin_ptr <const wchar> wch = ptrtostringchars (STR );
M_pimpobj-> setlastname (STD: wstring) wch). c_str ());
}
String ^ mgclass: joinname ()
{
Return gcnew string (m_pimpobj-> joinname ());
}
}
Then, create a C # interface project. C # The project reference references the mgdlib. dll generated by the hosting project and using the namespace mgdlib to call the mgclass of the hosting packaging class. (Adjust the dependency between the three projects for correct compilation)
"Connect" button click function to add code:
Private void btnconnect_click (Object sender, eventargs E)
{
Mgclass ACC = new mgclass ();
ACC. firstname = txtfirstname. text;
ACC. lastname = txtlastname. text;
String STR = maid. joinname ();
Txtfullname. Text = STR;
}
Final running result:
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lyrebing/archive/2007/10/10/1817951.aspx