1 #ifndef MYDLL_H
2 #define MYDLL_H
3
4 #ifdef DLL_IMPLEMENT_
5 #define DLL_APL __declspec(dllexport)
6 #else
7 #define DLL_API __declspec(dllimport)
8 #endif
9
10 //导出函数接口
11 extern "C"
12 {
13 int DLL_API add(const int &a, const int &b);
14 int DLL_API sub(const int &a, const int &b);
15 int DLL_API mul(const int &a, const int &b);
16 int DLL_API div(const int &a, const int &b);
17 }
18
19
20 //导出类
21 class __declspec(dllexport) CPreson
22 {
23 private:
24 char szName[128];
25 int age;
26 public:
27 CPreson();
28 ~CPreson();
29 CPreson(char* lpName,int nAge);
30 public:
31 char *GetName();
32 int GetAge();
33 };
34
35 #endif
1 // mydll.cpp : 定义 DLL 应用程序的导出函数。
2 //
3 #define DLL_IMPLEMENT_
4 #include "stdafx.h"
5 #include "mydll.h"
6
7 int add(const int &a, const int &b)
8 {
9 return a + b;
10 }
11
12 int sub(const int &a, const int &b)
13 {
14 return a - b;
15 }
16
17 int mul(const int &a, const int &b)
18 {
19 return a * b;
20 }
21
22 int div(const int &a, const int &b)
23 {
24 return a / b;
25 }
26
27
28 CPreson::CPreson()
29 {
30
31 }
32
33 CPreson::CPreson(char* lpName,int nAge)
34 {
35 this->age=nAge;
36 if (lpName)
37 {
38 int len = strlen(lpName);
39 if (len>127)
40 len=127;
41 memcpy(this->szName,lpName,len);
42 this->szName[len] = 0;
43 }
44 }
45
46 char * CPreson::GetName()
47 {
48 return szName;
49 }
50
51 int CPreson::GetAge()
52 {
53 return age;
54 }
55
56 CPreson::~CPreson()
57 {
58
59 }
1
//测试dll
// TestMyDll.cpp : 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include "mydll.h"
6 #include <iostream>
7 using namespace std;
8
9 int _tmain(int argc, _TCHAR* argv[])
10 {
11 /*int a, b;
12 char c;*/
13 //while( cin >> a >> c >> b )
14 //{
15 // switch( c )
16 // {
17 // case '+':
18 // cout << add(a, b) << endl;
19 // break;
20 // case '-':
21 // cout << sub(a, b) << endl;
22 // break;
23 // case '*':
24 // cout << mul(a, b) << endl;
25 // break;
26 // default:
27 // cout << '\"' << a << c << b << '\"' << "isn't a valid expression." << endl;
28 // }
29 //}
30
31 CPreson person("zhang",23);
32 cout<<person.GetName()<<endl;
33 system("pause");
34 return 0;
35 }