C#调用C++导出(dllexport)方法

开发环境:ui

visual studio 2013+win10code

 

一:创建C++项目blog

在vs中创建C++project(Win32 Project),须要注意的是,要勾选:开发

  Application type:Dllit

  Additional options:Export symbolsio

在项目头文件中放入代码:编译

#define WAOCVDLL_API __declspec(dllexport)
// 自定义方法
EXTERN_C WAOCVDLL_API int _stdcall Fit(int width, int height, wchar_t*image, float*firstPoint, int pointsLength);

而后再cpp文件中实现该方法:class

WAOCVDLL_API int _stdcall Fit(int imageWidth, int imageHeight, wchar_t*image, float*firstPoint, int pointsLength)
{
// 实现代码
}

 ps:WAOCVDLL_API 因各自项目不一样而不一样dva

在编译以前须要设置项目属性:float

C/C++ --> Advanced --> Compile As:Compile as C++ code

ps:C++项目的平台结构必定要和调用项目的一致(如果x64就都是x64,反之亦然)。

OK,build项目,获得dll

二:创建调用项目(C#控制台)

调用时须要注意的是:

[DllImport(@"你的dll存放路径", EntryPoint = "C++中定义的方法名字")]
extern static unsafe int Fit(int width, int height, char* image, float* firstPoint, int pointsLength);

以后就是在C#中调用非托管代码了,须要注意的是:

  1.设置项目属性:Allow unsafe code

  2.代码中添加unsafe代码块

OK,运行控制台项目,项目正常运行!

相关文章
相关标签/搜索