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
#include
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
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"
);
}
}
}
|