For historical reasonsCodeIt is not entirely written using. net. At this time, mixed programming with the previous C ++ code is very important. I recently encountered such a problem and briefly described the method as follows.
Call simple C ++ Functions
The general idea of calling C ++ functions in C # code is as follows: First, write the C ++ functions as libraries in the DLL format, then, import the functions in the DLL in C # For calling. The specific code is similar to this:
C ++ code:
1 Int Staticelementnumber = 10 ;
2 Extern " C " Afx_api_export Int Getarrayelementnumber ()
3 {
4 Return Staticelementnumber;
5 }
C # code:
(Import the function, which is written in the class where the function is called)
1 [Dllimport ( " Mfcdll. dll " )]
2 Public Static Extern Int Getarrayelementnumber ();
3 Int Elementnumber = Getarrayelementnumber ();
The details in S, such as int and char data types occupying different spaces in C ++ and C #, are automatically processed by CLR. (Mainly through automatic processing of Marshal class)
Such a call also supports debugging. Open the properties of the C # project, and select enable unmanaged code debugging on the debug tab to enable C ++ code debugging. In this way, when calling this function, you can continue to follow up the internal debugging of the function by F11.
Passing a GDI object
Some Complex windows objects can be transferred through handles. For example, the following code converts a GDI + bitmap object into a GDI handle for transmission.
C ++ Code (the Declaration and reference of GDI + are omitted ):
1 Extern " C " Afx_api_export hbitmap getabitmap (wchar * Strfilename)
2 {
3 Gdiplus: gdiplusstartupinput;
4 Ulong_ptr gdiplustoken;
5 Gdiplusstartup ( & Gdiplustoken, & Gdiplusstartupinput, null );
6 Bitmap * Bitmap = Bitmap: fromfile (strfilename );
7 Hbitmap hbitmaptoreturn;
8 Bitmap -> Gethbitmap (null, & Hbitmaptoreturn );
9 Gdiplusshutdown (gdiplustoken );
10
11 Return Hbitmaptoreturn;
12 }
C # code (the user interface uses WPF, with the relevant declarations and references omitted ):
1 [Dllimport ( " Mfcdll. dll " )]
2 Public Static Extern Intptr getabitmap ([financialas (unmanagedtype. lpwstr)] String Strfilename );
3
4 Private Void Menuitemfileopenonclicked ( Object Sender, routedeventargs E)
5 {
6 Openfiledialog Dialog = New Openfiledialog ();
7 Dialog. Title = " Load an image... " ;
8 Dialog. multiselect = False ;
9 If (Dialog. showdialog () = True )
10 {
11 Maingrid. Children. Clear ();
12
13 Intptr hbitmap = Getabitmap (dialog. filename );
14 Bitmap bitmap = Bitmap. fromhbitmap (hbitmap );
15 System. Windows. Controls. Image = New Windows. Controls. Image ();
16 Image. Source = Windows. InterOP. imaging. createbitmapsourcefromhbitmap (hbitmap, Ro, int32rect. empty,
17 Windows. Media. imaging. bitmapsizeoptions. fromemptyoptions ());
18 Image. Stretch = System. Windows. Media. Stretch. Fill;
19 Deleteobject (hbitmap );
20 Maingrid. Children. Add (image );
21 }
22 }
Passing Arrays
It is easy to pass a fixed-length array, which is not described here. The following code transfers a variable-length array:
C ++ code:
1 Int Staticelementnumber = 10 ;
2 Extern " C " Afx_api_export Bool Getarray ( Int Elementnumber, Double * Baseaddress)
3 {
4 If (Elementnumber < Staticelementnumber)
5 {
6 Return False ;
7 }
8
9 For ( Int I = 0 ; I < Staticelementnumber; ++ I)
10 {
11 Baseaddress [I] = 1 / (( Double ) I + 1 );
12 }
13
14 Return True ;
15 }
16
17 Extern " C " Afx_api_export Int Getarrayelementnumber ()
18 {
19 Return Staticelementnumber;
20 }
C # code:
1 [Dllimport ( " Mfcdll. dll " )]
2 Public Static Extern Bool Getarray ( Int Elementnumber, [financialas (unmanagedtype. lparray, sizeparamindex = 0 )] Double [] Baseaddress );
3
4 Private Void Menuitemfilegetarrayonclicked ( Object Sender, routedeventargs E)
5 {
6 // Get array data.
7 Int Elementnumber = Getarrayelementnumber ();
8 Double [] Doublearray = New Double [Elementnumber];
9 Getarray (elementnumber, doublearray );
10
11 // Show the data.
12 Maingrid. Children. Clear ();
13 ListBox = New ListBox ();
14 Foreach ( Double Number In Doublearray)
15 {
16 ListBox. Items. Add (number );
17 }
18 Maingrid. Children. Add (ListBox );
19 }
20
With these three functions, it is relatively simple to reuse C ++ code on the C # platform.