这个网上很多。但是没有能运行起来的。自己经过一周多的摸索进行稍微总结下。首先是
C++下的声明:
头文件:
extern
"C"
_declspec(
dllexport
)
int
Add(
int
sm);
Cpp文件:
int
Add(
int
sm)
{
int
insx=20;
sm=insx+sm;
return
sm;
}
头文件:
extern
"C"
_declspec(
dllexport
)
int
Add(
int
sm);
Cpp文件:
int
Add(
int
sm)
{
int
insx=20;
sm=insx+sm;
return
sm;
}
|
生成dll把所生成的拷到C#的bin/debug下
在C#下调用:
[DllImport(
"mediastream.dll"
, EntryPoint =
"Add"
, CharSet =CharSet.Auto, CallingConvention =CallingConvention.Cdecl)]
public
static
extern
int
Add(
int
sm);
|
[DllImport(
"mediastream.dll"
, EntryPoint =
"Add"
, CharSet =CharSet.Auto, CallingConvention =CallingConvention.Cdecl)]
public
static
extern
int
Add(
int
sm);
|
[DllImport(
"mediastream.dll"
, EntryPoint =
"Add"
, CharSet =CharSet.Auto, CallingConvention =CallingConvention.Cdecl)]
public
static
extern
int
Add(
int
sm);
[DllImport(
"mediastream.dll"
, EntryPoint =
"Add"
, CharSet =CharSet.Auto, CallingConvention =CallingConvention.Cdecl)]
public
static
extern
int
Add(
int
sm);
|
好了就可以调用了Add了。
虽然过程很简单但是摸索了好久才弄出来的。
转自:http://blog.sina.com.cn/s/blog_5d0990c701011wjl.html