C#代码(用户界面采用WPF,略去相关声明和引用):
[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++代码:
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;
}
责任编辑:cyth