c++本地动态连接库代码
c++本地动态连接库代码 |

- 1 #pragma once
- 2 #include "stdafx.h"
- 3
- 4 #ifdef PERSON_EXPORTS
- 5 #define PERSON_API __declspec(dllexport)
- 6 #else
- 7 #define PERSON_API __declspec(dllimport)
- 8 #endif
- 9
- 10 class PERSON_API CPerson
- 11 {
- 12 public:
- 13 CPerson(LPCTSTR pszName, SYSTEMTIME birth);
- 14 unsigned int getAge(void);
- 15
- 16 private:
- 17 TCHAR _name[256];
- 18 SYSTEMTIME _birth;
- 19 };
- 20
- 21 #ifdef __cplusplus
- 22 extern "C"{
- 23 #endif
- 24 extern PERSON_API int nVal;
- 25 PERSON_API int nFunc(void);
- 26 #ifdef __cplusplus
- 27 }
- 28 #endif

Source |


- 1 // CPerson.cpp: 定义 DLL 应用程序的导出函数。
- 2 //
- 3
- 4 #include "stdafx.h"
- 5 #include "CPerson.h"
- 6
- 7 CPerson::CPerson(LPCTSTR pszName, SYSTEMTIME birth)
- 8 {
- 9 _birth = birth;
- 10 if (pszName != nullptr)
- 11 lstrcpy(_name, pszName);
- 12 }
- 13
- 14 unsigned int CPerson::getAge(void)
- 15 {
- 16 SYSTEMTIME st;
- 17 GetSystemTime(&st);
- 18 UINT age = st.wYear - _birth.wYear;
- 19 if (_birth.wMonth > st.wMonth ||
- 20 (_birth.wMonth == st.wMonth && _birth.wDay > st.wDay))
- 21 {
- 22 --age;
- 23 }
- 24
- 25 return age;
- 26 }
- 27
- 28 PERSON_API int nVal = 20180403;
- 29 PERSON_API int nFunc(void)
- 30 {
- 31 return 1816;
- 32 }

- 2 //
- 3
- 4 #include "stdafx.h"
- 5 #include "CPerson.h"
- 6
- 7 CPerson::CPerson(LPCTSTR pszName, SYSTEMTIME birth)
- 8 {
- 9 _birth = birth;
- 10 if (pszName != nullptr)
- 11 lstrcpy(_name, pszName);
- 12 }
- 13
- 14 unsigned int CPerson::getAge(void)
- 15 {
- 16 SYSTEMTIME st;
- 17 GetSystemTime(&st);
- 18 UINT age = st.wYear - _birth.wYear;
- 19 if (_birth.wMonth > st.wMonth ||
- 20 (_birth.wMonth == st.wMonth && _birth.wDay > st.wDay))
- 21 {
- 22 --age;
- 23 }
- 24
- 25 return age;
- 26 }
- 27
- 28 PERSON_API int nVal = 20180403;
- 29 PERSON_API int nFunc(void)
- 30 {
- 31 return 1816;
- 32 }
c++/cli包装代码 |

- 1 #pragma once
- 2 #include "CPerson.h"
- 3
- 4 using namespace System;
- 5
- 6 namespace Adapter
- 7 {
- 8 public ref class Person
- 9 {
- 10 public:
- 11 Person(String ^name, DateTime birth);
- 12 virtual ~Person();
- 13
- 14 property unsigned int Age
- 15 {
- 16 unsigned int get();
- 17 }
- 18
- 19 static int CallnVal();
- 20 static int CallnFunc();
- 21
- 22 private:
- 23 Person(): _person(nullptr) { }
- 24 CPerson *_person;
- 25 };
- 26 }

Source |


- 1 #include "stdafx.h"
- 2 #include "Person.h"
- 3 #include <vcclr.h>
- 4
- 5 Adapter::Person::Person(String ^ name, DateTime birth)
- 6 {
- 7 SYSTEMTIME st = { 0 };
- 8 st.wYear = birth.Year;
- 9 st.wMonth = birth.Month;
- 10 st.wDay = birth.Day;
- 11
- 12 pin_ptr<const TCHAR> psz = PtrToStringChars(name);
- 13
- 14 _person = new CPerson(psz, st);
- 15 }
- 16
- 17 Adapter::Person::~Person()
- 18 {
- 19 //System::Diagnostics::Debugger::Log(0, "Debug", "Person destructor.");
- 20 if (_person != nullptr)
- 21 {
- 22 CPerson *ptmp = _person;
- 23 _person = nullptr;
- 24 delete ptmp;
- 25 }
- 26 }
- 27
- 28 int Adapter::Person::CallnVal()
- 29 {
- 30 return nVal;
- 31 }
- 32
- 33 int Adapter::Person::CallnFunc()
- 34 {
- 35 return nFunc();
- 36 }
- 37
- 38 unsigned int Adapter::Person::Age::get()
- 39 {
- 40 return _person->getAge();
- 41 }

- 2 #include "Person.h"
- 3 #include <vcclr.h>
- 4
- 5 Adapter::Person::Person(String ^ name, DateTime birth)
- 6 {
- 7 SYSTEMTIME st = { 0 };
- 8 st.wYear = birth.Year;
- 9 st.wMonth = birth.Month;
- 10 st.wDay = birth.Day;
- 11
- 12 pin_ptr<const TCHAR> psz = PtrToStringChars(name);
- 13
- 14 _person = new CPerson(psz, st);
- 15 }
- 16
- 17 Adapter::Person::~Person()
- 18 {
- 19 //System::Diagnostics::Debugger::Log(0, "Debug", "Person destructor.");
- 20 if (_person != nullptr)
- 21 {
- 22 CPerson *ptmp = _person;
- 23 _person = nullptr;
- 24 delete ptmp;
- 25 }
- 26 }
- 27
- 28 int Adapter::Person::CallnVal()
- 29 {
- 30 return nVal;
- 31 }
- 32
- 33 int Adapter::Person::CallnFunc()
- 34 {
- 35 return nFunc();
- 36 }
- 37
- 38 unsigned int Adapter::Person::Age::get()
- 39 {
- 40 return _person->getAge();
- 41 }
经过验证使用Visual Studio 2017包含头文件<vcclr.h>会有报错,可以设置修改平台工具集来编译
通过CSharp调用 |

- 1 using System;
- 2 using Adapter;
- 3
- 4 namespace ConsoleApp
- 5 {
- 6 class Program
- 7 {
- 8 static void Main(string[] args)
- 9 {
- 10 DateTime dt = DateTime.Now;
- 11 dt = dt.AddYears(-50);
- 12 Person p1 = new Person("c++", dt);
- 13 Console.WriteLine("调用本地c++ dll中的类: " + p1.Age);
- 14 Console.WriteLine("调用本地c++ dll中的变量: " + Person.CallnVal());
- 15 Console.WriteLine("调用本地c++ dll中的函数: " + Person.CallnFunc());
- 16
- 17 Console.ReadLine();
- 18 }
- 19 }
- 20 }

参考资料详情:
https://www.codeproject.com/Articles/35041/Mixing-NET-and-native-code
https://blog.csdn.net/ganzheyu/article/details/50154705
// CPerson.cpp: 定义 DLL 应用程序的导出函数。//
#include "stdafx.h"#include "CPerson.h"
CPerson::CPerson(LPCTSTR pszName, SYSTEMTIME birth){ _birth = birth; if (pszName != nullptr) lstrcpy(_name, pszName);}
unsigned int CPerson::getAge(void){ SYSTEMTIME st; GetSystemTime(&st); UINT age = st.wYear - _birth.wYear; if (_birth.wMonth > st.wMonth || (_birth.wMonth == st.wMonth && _birth.wDay > st.wDay)) { --age; }
return age;}
PERSON_API int nVal = 20180403;PERSON_API int nFunc(void){ return 1816;}
c++本地动态连接库代码的更多相关文章
- Linux下的动态连接库及其实现机制
Linux与Windows的动态连接库概念相似,但是实现机制不同.它引入了GOT表和PLT表的概念,综合使用了多种重定位项,实现了"浮动代码",达到了更好的共享性能.本文对这些技术 ...
- Qt动态连接库/静态连接库创建与使用,QLibrary动态加载库
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt动态连接库/静态连接库创建与使用,QLibrary动态加载库 本文地址:https ...
- Linux 静态链接库和动态连接库
(0)文件夹 VMware 下安装Ubuntu的吐血经历 零基础学习Shell编程 Linux下的makefile的妙用 Linux调试神器 -- gdb 十分钟学会Python的基本类型 Linux ...
- 目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”)。静态库是一个或者多个obj文件的打包
前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”) ...
- c++调用matlab生成的Dll动态连接库
点击打开链接http://download.csdn.net/detail/nuptboyzhb/4228429 c++调用matlab生成的Dll动态连接库 实验平台: matlab 7.0(R ...
- VS2013 生成sqlite3动态连接库及sqlite3.dll的调用
一,生成sqlite3动态连接库1,去sqlite官网上下载最近的sqlite源码包,解压后得到四个文件:shell.c,sqlite3.c,sqlite3.h,sqlite3ext.h此处还需要sq ...
- C编译: 动态连接库 (.so文件)(转摘)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在“纸上谈兵: 算法与数据结构”中,我在每一篇都会有一个C程序,用于实现算法和数据 ...
- C编译: 动态连接库 (.so文件)
转自:http://www.cnblogs.com/vamei/archive/2013/04/04/2998850.html 在“纸上谈兵: 算法与数据结构”中,我在每一篇都会有一个C程序,用于实现 ...
- 在Android工程中导入外部动态连接库(so文件)
假设要导入的so文件命为libtest.so,导入的方法如下: 给工程加入Native Support,将libtest.so复制到在jni文件夹下,在Android.mk文件中,加入以下代码: in ...
随机推荐
- 2018年一名合格的web前端程序员应该会哪些技术
有朋友让小编说一说web前端在未来几年的发展趋向,对于这个问题,恕小编无能为力,web前端技术日新月异,更新非常快,谁也不能预料未来会发生些什么 小编也只能说在2018年,react native和j ...
- Long类型框架自动序列化成String失效问题排查
目录 问题描述 猜想 1. 写错了 2. 重新使用 验证猜想 1.验证猜想 2.继续猜想 3.再次猜想 4.再次验证 5.疑惑 6.找到原因,解决疑惑 7.解决 问题描述 微服务架构下进行业务模块开发 ...
- python + pytest基本使用方法(运行测试&测试报告)
import pytest# 1.运行名称中包含某字符串的测试用例#名称中含add 的测试用例# 执行: pytest -k add test_assert.py# 2.减少测试的运行冗长# 执行: ...
- CentOS7.9安装Oracle 12C数据库实战
准备工作(先安装好以下软件): 1.服务器操作系统 CentOS7.9 2.Shell工具:Xshell 7免费版 3.Xmanager 7软件 =========================== ...
- 福昕foxit phantom pdf高级编辑器企业版10.1 pro安装破解教程
本文提供福昕foxit phantom pdf高级编辑器企业版10.1的安装教程.pj教程,可以使用全部功能,注意的是此方法对个人版无效. 没有必要再尝试别的文章,仅看这一篇即可!别的文章亲测是通过修 ...
- Python - 赋值运算符
前置知识 先了解下变量: https://www.cnblogs.com/poloyy/p/15042257.html 再了解下算术运算符: https://www.cnblogs.com/poloy ...
- 深入刨析tomcat 之---第4篇 tomcat4.0连接池 实现原理
writedby 张艳涛
- frame window 和open 的关系
建立一个如下的关系框架 windowA.html <!DOCTYPE html> <html lang="en"> <head> <met ...
- FreeRTOS-03-其它任务相关函数
说明: 本文仅作为学习FreeRTOS的记录文档,作为初学者肯定很多理解不对甚至错误的地方,望网友指正. FreeRTOS是一个RTOS(实时操作系统)系统,支持抢占式.合作式和时间片调度.适用于微处 ...
- JMX远程连接JVM
-Dcom.sun.management.jmxremote :允许使用JMX远程管理 -Dcom.sun.management.jmxremote.port=9999 :JMX远程连接端口 -Dco ...