在C#调用C++的DLL简析(一)——生成非托管dll
来源:互联网 发布:电子科技大学知乎 编辑:程序博客网 时间:2024/06/05 08:31
1
2
3
4
5
6
7
//Define.h
///////////////////////////////////////////
//////////////////////////////////////////
#ifndef _DEFINE_H_
#define _DEFINE_H_
#define _EXTERN_C_ extern "C" _declspec(dllexport)
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//CFunction.h
////////////////////////////////////////////
///////////////////////////////////////////
#ifndef _C_FUNCTION_H_
#define _C_FUNCTION_H_
#include "Define.h"
#include <string>
#include <istream>
struct
SystemTime
{
int
year;
int
month;
int
day;
int
hour;
int
minute;
int
second;
int
millsecond;
SystemTime & operator= (SystemTime st)
{
this
->year = st.year;
this
->month = st.month;
this
->day = st.day;
this
->hour = st.hour;
this
->minute = st.minute;
this
->second = st.second;
this
->millsecond = st.millsecond;
return
*
this
;
}
};
_EXTERN_C_
int
add(
int
x,
int
y);
_EXTERN_C_
int
sub(
int
x,
int
y);
_EXTERN_C_
int
testChar(
char
* src,
char
* res,
int
nCount);
_EXTERN_C_
int
testStruct(SystemTime & stSrc, SystemTime & stRes);
#endif //_C_FUNCTION_H_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//CFunction.cpp
////////////////////////////////////////////
////////////////////////////////////////////
#include "stdafx.h"
#include "CFunction.h"
#include <stdio.h>
int
add(
int
x,
int
y)
{
return
x + y;
}
int
sub(
int
x,
int
y)
{
return
x - y;
}
int
testChar(
char
* src,
char
* res,
int
nCount)
{
memcpy
(res, src,
sizeof
(
char
) * nCount);
return
1;
}
int
testStruct(SystemTime & stSrc, SystemTime & stRes)
{
stRes = stSrc;
return
1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Runtime.InteropServices;
namespace
DllTest
{
[StructLayout(LayoutKind.Sequential)]
public
struct
SystemTime
{
public
int
year;
public
int
month;
public
int
day;
public
int
hour;
public
int
minute;
public
int
second;
public
int
millsecond;
public
SystemTime(DateTime dt)
{
this
.year = dt.Year;
this
.month = dt.Month;
this
.day = dt.Day;
this
.hour = dt.Hour;
this
.minute = dt.Minute;
this
.second = dt.Second;
this
.millsecond = dt.Millisecond;
}
public
override
string
ToString()
{
return
this
.year.ToString() +
"-"
+
this
.month.ToString() +
"-"
+
this
.day.ToString() +
" "
+
this
.hour.ToString() +
":"
+
this
.minute.ToString() +
"-"
+
this
.second.ToString() +
"-"
+
this
.millsecond.ToString();
}
};
public
class
CFunction
{
[DllImport(
"MyNativeDll.dll"
, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public
extern
static
int
add(
int
x,
int
y);
[DllImport(
"MyNativeDll.dll"
, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public
extern
static
int
sub(
int
x,
int
y);
[DllImport(
"MyNativeDll.dll"
, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public
extern
static
int
testChar(
ref
byte
src,
ref
byte
res,
int
nCount);
[DllImport(
"MyNativeDll.dll"
, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public
extern
static
int
testStruct(
ref
SystemTime stSrc,
ref
SystemTime stRes);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Diagnostics;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
namespace
DllTest
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
int
a = CFunction.add(100, 50);
int
b = CFunction.sub(100, 50);
Debug.WriteLine(
"add = "
+ a.ToString() +
" b = "
+ b.ToString());
Debug.WriteLine(
"\r\n"
);
string
src =
"123456"
;
byte
[] srcBytes = System.Text.Encoding.ASCII.GetBytes(src);
byte
[] resBytes =
new
byte
[100];
a = CFunction.testChar(
ref
srcBytes[0],
ref
resBytes[0], src.Length);
string
res = (System.Text.Encoding.ASCII.GetString(resBytes, 0, resBytes.Length)).TrimEnd();
Debug.WriteLine(res.ToString());
Debug.WriteLine(
"\r\n"
);
SystemTime stSrc =
new
SystemTime(DateTime.Now);
SystemTime stRes =
new
SystemTime();
a = CFunction.testStruct(
ref
stSrc,
ref
stRes);
Debug.WriteLine(stRes.ToString());
Debug.WriteLine(
"\r\n"
);
}
}
}
0 0
- 在C#调用C++的DLL简析(一)——生成非托管dll
- 在C#调用C++的DLL简析(一)——生成非托管dll
- C#调用C++ DLL简析(一)——生成非托管dll
- 在C#调用C++的DLL简析(二)—— 生成托管dll
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- 在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
- C#动态调用非托管DLL(一)
- c#调用非托管C++生成的dll
- Sql获取第一天、最后一天(转载)
- android-命令
- Python编程习惯与特点
- 二分查找非递归实现--【编程珠玑】
- 关于springmvc 返回json乱码
- 在C#调用C++的DLL简析(一)——生成非托管dll
- PHP文件的锁定机制
- Android多点触控技术实战,自由地对图片进行缩放和移动
- Android属性动画深入分析:让你成为动画牛人
- 黑马程序员——学习笔记09.Java_IO流
- Objective-C KVC
- graphviz dot uml类图绘制笔记
- Fedora20搭建伪Hadoop集群,运行Wordcount程序
- 这个网站收集了计算机领域主流会议的best paper