Dll 导出类 [示例代码]

简介: 1、Dll相关代码 MyDll.h[cpp] view plaincopyprint?#ifdef DLL1_API  #else  #define DLL1_API extern "C" __declspec(dllimport)  #endif  ...

1、Dll相关代码

MyDll.h

  1. #ifdef DLL1_API 
  2. #else 
  3. #define DLL1_API extern "C" __declspec(dllimport) 
  4. #endif 
  5. DLL1_API int Add(int a,int b); 
  6. DLL1_API int Sub(int a,int b); 
  7. class __declspec(dllexport) Person 
  8. public
  9.     Person(char *name); 
  10.     char*   m_Name; 
  11.     int     m_Age; 
  12. }; 
#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

  1. #define DLL1_API extern "C" __declspec(dllexport) 
  2. #include "MyDll.h" 
  3. #include <Windows.h> 
  4. #include <stdio.h> 
  5. #pragma comment(linker,"/DLL") 
  6. #pragma comment(linker,"/ENTRY:DllMain") 
  7. int Add(int a,int b) 
  8.     return a+b; 
  9. int Sub(int a,int b) 
  10.     return a-b; 
  11. Person::Person(char *name) 
  12.     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; }

 

编译链接,如下图:

2、调用dll中类

Main.cpp

  1. #include <iostream.h> 
  2. #include <stdio.h> 
  3. #include <windows.h> 
  4. #include "MyDll.h" 
  5. #pragma comment(lib,"MyDll.lib") 
  6. void main() 
  7.     int x=3; 
  8.     int y=9; 
  9.     int z=Add(x,y); 
  10.     printf("%d+%d=%d /r/n", x,y,z); 
  11.      
  12.     Person pt("123"); 
  13.     cout<<pt.m_Name<<endl; 
#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; }

 

编译链接,如下图:

 

 

from:

http://blog.csdn.net/wangningyu/article/details/5467550
目录
相关文章
|
6月前
|
C++
C/C++ DLL 简单实现
C/C++ DLL 简单实现
44 0
|
1月前
|
编解码
qt中使用dll库的方法
qt中使用dll库的方法
18 2
vs2013 生成dll /lib,和调用dll库方法
vs2013 生成dll /lib,和调用dll库方法
221 0
vs2013 生成dll /lib,和调用dll库方法
|
C#
C#调用dll代码范例
C#调用dll代码范例
95 0
|
C# 图形学 C++
Unity与 DLL文件 ☀️| 怎样使用 C# 类库 生成一个DLL文件 并 调用!
📢前言 🎬生成DLL文件 🎥使用 C#类库 将Unity中的脚本打包成 DLL文件 并调用 🏳️‍🌈第一步:打开Visual Studio之后,新建一个项目 🏳️‍🌈第二步:选择类库(.NET Framework),改个名字,选择一个位置路径 🏳️‍🌈第三步:然后在创建的脚本中简单写一点代码,如下所示 🏳️‍🌈第四步:然后在解决方案资源管理器右键这个脚本 -> 添加 -> 引用 🏳️‍🌈第五步:然后点击浏览,找到Unity安装路径 -> Editor -> Data -> Managed 下的这两个DLL 文件,点击添加!
Unity与 DLL文件 ☀️| 怎样使用 C# 类库 生成一个DLL文件 并 调用!
|
C# 图形学 C++
Unity中调用DLL库
DLL —— Dynamic Link Library(动态链接库文件),这里以Window平台为例。   Unity支持的两种语言生成的DLL库(C++、C#),这里以C#为例,C++网上可以搜索很详细的资料。
1521 0
|
.NET C# 计算机视觉
Csharp调用基于Opencv编写的类库文件
下载地址:https://files.cnblogs.com/files/jsxyhelu/GOCW20171217.zip 现在将Csharp调用基于Opencv编写的类库文件(Dll)的方法定下来,我取名叫做GreenOpenCsharpWarper,简称GOCW。
1408 0