Development Environment:
visual studio 2013+win10
A: Create C ++ project
Build C ++ project (Win32 Project) in vs, it is necessary to note that, to check:
Application type:Dll
Additional options:Export symbols
In the project header file into the code:
1
2
3
|
#define WAOCVDLL_API __declspec(dllexport)
// 自定义方法
EXTERN_C WAOCVDLL_API
int
_stdcall Fit(
int
width,
int
height, wchar_t*image,
float
*firstPoint,
int
pointsLength);
|
Cpp file and then implement this method:
1
2
3
4
|
WAOCVDLL_API
int
_stdcall Fit(
int
imageWidth,
int
imageHeight, wchar_t*image,
float
*firstPoint,
int
pointsLength)
{
// 实现代码
}
|
ps: WAOCVDLL_API because each project varies
Before compiling need to set the project properties:
C/C++ --> Advanced --> Compile As:Compile as C++ code
ps: platform C ++ project structure must be consistent and call the project (if it is x64 x64, and vice versa).
OK, build the project, get dll
Two: establish call project (C # console)
Note that when you call:
1
2
|
[DllImport(
@"你的dll存放路径"
, EntryPoint =
"C++中定义的方法名字"
)]
extern
static
unsafe
int
Fit(
int
width,
int
height,
char
* image,
float
* firstPoint,
int
pointsLength);
|
After that call unmanaged code in C #, should be noted that:
1. Set the project properties: Allow unsafe code
2. Add the code block unsafe
OK, run console project, the project running!