創建C++ DLL
注意,32bit和64bit之間不兼容
- 創建普通dll工程
- 設置Properties -> Configuration Properties -> General -> Project Defaults -> Common Language Runtime Support = [Common Language Runtime Support (/clr)]
- 設置turn off "/permissive-": Properties -> Configuration Properties -> C/C++ -> Language -> Conformance mode = No
- 修改debug|release設置后,Properties也要記得修改。
托管類中需要用到Bitmap類時,需要添加引用,右鍵當前工程-> Add(添加)引用 -> Assemblies(程序集) -> Framework(框架) -> 勾選System.Drawing,就可以在托管C++中調用Bitmap類了。
System::Drawing::Bitmap ^clrClass::getImage(double scale) { System::Drawing::Bitmap ^img = gcnew System::Drawing::Bitmap(5,5); img->SetPixel(1,1, System::Drawing::Color::FromArgb(255,0,0)); return img; }
C#中調用
引用DLL: 工程 -> References -> Add References -> Browse -> [Browse] -> 添加C++工程生成的DLL
正常使用即可,典型使用如下。
clrClass cc = new clrClass(); // 后面就可以用C#的方式使用了
注意:如果C++工程的dll引用了其他dll,需要把二級引用的dll手動加到bin文件夾下對應位置。