要在C#代碼中調用C++函數,大致的思路是這樣的:首先將C++函數寫成DLL形式的庫,而後在C#中導入DLL中的函數進行調用。具體的代碼相似這樣:
C++代碼:數組
int StaticElementNumber = 10; extern "C" AFX_API_EXPORT int GetArrayElementNumber() { return StaticElementNumber; }
C#代碼:
(導入函數部分,寫在調用函數所在類中)函數
[DllImport("MFCDll.dll")] public static extern int GetArrayElementNumber(); int ElementNumber = GetArrayElementNumber();
s其中的細節,好比int和char等數據類型在C++和C#中佔用的空間不一樣等等CLR會自動處理。(主要是經過Marshal類自動處理)spa
這樣的調用還支持調試。打開C#工程的Properties,在Debug選項卡中勾選Enable unmanaged code debugging便可啓用C++代碼調試。這樣在調試模式下,調用這個函數時能夠繼續按F11跟進函數內部進行調試。debug
一些複雜的Windows對象能夠經過句柄來傳送。好比下面的代碼就將一個GDI+ Bitmap對象轉換成GDI句柄進行傳送。
C++代碼(GDI+的聲明,引用等等省略):調試
extern "C" AFX_API_EXPORT HBITMAP GetABitmap(WCHAR *strFileName) { Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); Bitmap *bitmap = Bitmap::FromFile(strFileName); HBITMAP HBitmapToReturn; bitmap->GetHBITMAP(NULL, &HBitmapToReturn); GdiplusShutdown(gdiplusToken); return HBitmapToReturn; }
C#代碼(用戶界面採用WPF,略去相關聲明和引用):code
[DllImport("MFCDll.dll")] public static extern IntPtr GetABitmap([MarshalAs(UnmanagedType.LPWStr)] string strFileName); private void MenuItemFileOpenOnClicked(object sender, RoutedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Title = "Load an image..."; dialog.Multiselect = false; if (dialog.ShowDialog() == true) { mainGrid.Children.Clear(); IntPtr hBitmap = GetABitmap(dialog.FileName); Bitmap bitmap = Bitmap.FromHbitmap(hBitmap); System.Windows.Controls.Image image = new Windows.Controls.Image(); image.Source = Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, ro, Int32Rect.Empty, Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); image.Stretch = System.Windows.Media.Stretch.Fill; DeleteObject(hBitmap); mainGrid.Children.Add(image); } }
傳遞數組對象
傳遞定長數組很簡單,此處不述。下面的代碼實現變長數組的傳遞:
C++代碼:ip
int StaticElementNumber = 10; extern "C" AFX_API_EXPORT bool GetArray(int ElementNumber, double *BaseAddress) { if (ElementNumber < StaticElementNumber) { return false; } for (int i = 0; i < StaticElementNumber; ++i) { BaseAddress[i] = 1 / ((double)i + 1); } return true; } extern "C" AFX_API_EXPORT int GetArrayElementNumber() { return StaticElementNumber; }
C#代碼:string
[DllImport("MFCDll.dll")] public static extern bool GetArray(int ElementNumber, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] BaseAddress); private void MenuItemFileGetArrayOnClicked(object sender, RoutedEventArgs e) { //Get array data. int ElementNumber = GetArrayElementNumber(); double[] doubleArray = new double[ElementNumber]; GetArray(ElementNumber, doubleArray); //Show the data. mainGrid.Children.Clear(); ListBox listBox = new ListBox(); foreach (double number in doubleArray) { listBox.Items.Add(number); } mainGrid.Children.Add(listBox); }