1、Dll相關代碼
MyDll.h
#ifdef DLL1_API
#else
#define DLL1_API extern "C" __declspec(dllimport)
#endif
DLL1_API int Add(int a,int b);
DLL1_API int Sub(int a,int b);
class __declspec(dllexport) Person
{
public:
Person(char *name);
char* m_Name;
int m_Age;
};
#ifdef DLL1_API
#else
#define DLL1_API extern "C" __declspec(dllimport)
#endif
DLL1_API int Add(int a,int b);
DLL1_API int Sub(int a,int b);
class __declspec(dllexport) Person
{
public:
Person(char *name);
char* m_Name;
int m_Age;
};
MyDll.cpp
#define DLL1_API extern "C" __declspec(dllexport)
#include "MyDll.h"
#include <Windows.h>
#include <stdio.h>
#pragma comment(linker,"/DLL")
#pragma comment(linker,"/ENTRY:DllMain")
int Add(int a,int b)
return a+b;
}
int Sub(int a,int b)
return a-b;
Person::Person(char *name)
m_Name = name;
#define DLL1_API extern "C" __declspec(dllexport)
#include "MyDll.h"
#include <Windows.h>
#include <stdio.h>
#pragma comment(linker,"/DLL")
#pragma comment(linker,"/ENTRY:DllMain")
int Add(int a,int b)
return a+b;
}
int Sub(int a,int b)
return a-b;
Person::Person(char *name)
m_Name = name;
編譯連結,如下圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZugzYkJTOzYTYlVmZhhDMxQjNiBTN4UjY0UGN5gDOxEWNfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
2、調用dll中類
Main.cpp
#include <iostream.h>
#include <windows.h>
#pragma comment(lib,"MyDll.lib")
void main()
int x=3;
int y=9;
int z=Add(x,y);
printf("%d+%d=%d /r/n", x,y,z);
Person pt("123");
cout<<pt.m_Name<<endl;
#include <iostream.h>
#include <windows.h>
#pragma comment(lib,"MyDll.lib")
void main()
int x=3;
int y=9;
int z=Add(x,y);
printf("%d+%d=%d /r/n", x,y,z);
Person pt("123");
cout<<pt.m_Name<<endl;
from:
<a href="http://blog.csdn.net/wangningyu/article/details/5467550">http://blog.csdn.net/wangningyu/article/details/5467550</a>