Dll 导出类 [示例代码]

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; };

 

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; }

 

编译链接,如下图:

Dll 导出类 [示例代码]_第1张图片

 

2、调用dll中类

 

Main.cpp

#include <iostream.h> #include <stdio.h> #include <windows.h> #include "MyDll.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; }

 

编译链接,如下图:

Dll 导出类 [示例代码]_第2张图片

你可能感兴趣的:(c,api,Class,dll,linker)