托管代码中调用c++本地代码
c++本地动态连接库代码
#pragma once
#include "stdafx.h" #ifdef PERSON_EXPORTS
#define PERSON_API __declspec(dllexport)
#else
#define PERSON_API __declspec(dllimport)
#endif class PERSON_API CPerson
{
public:
CPerson(LPCTSTR pszName, SYSTEMTIME birth);
unsigned int getAge(void); private:
TCHAR _name[];
SYSTEMTIME _birth;
}; #ifdef __cplusplus
extern "C"{
#endif
extern PERSON_API int nVal;
PERSON_API int nFunc(void);
#ifdef __cplusplus
}
#endif
Source
// 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 = ;
PERSON_API int nFunc(void)
{
return ;
}
c++/cli包装代码
#pragma once
#include "CPerson.h" using namespace System; namespace Adapter
{
public ref class Person
{
public:
Person(String ^name, DateTime birth);
virtual ~Person(); property unsigned int Age
{
unsigned int get();
} static int CallnVal();
static int CallnFunc(); private:
Person(): _person(nullptr) { }
CPerson *_person;
};
}
Source
#include "stdafx.h"
#include "Person.h"
#include <vcclr.h> Adapter::Person::Person(String ^ name, DateTime birth)
{
SYSTEMTIME st = { };
st.wYear = birth.Year;
st.wMonth = birth.Month;
st.wDay = birth.Day; pin_ptr<const TCHAR> psz = PtrToStringChars(name); _person = new CPerson(psz, st);
} Adapter::Person::~Person()
{
//System::Diagnostics::Debugger::Log(0, "Debug", "Person destructor.");
if (_person != nullptr)
{
CPerson *ptmp = _person;
_person = nullptr;
delete ptmp;
}
} int Adapter::Person::CallnVal()
{
return nVal;
} int Adapter::Person::CallnFunc()
{
return nFunc();
} unsigned int Adapter::Person::Age::get()
{
return _person->getAge();
}
经过验证使用Visual Studio 2017包含头文件<vcclr.h>会有报错,可以设置修改平台工具集来编译
通过CSharp调用
using System;
using Adapter; namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
dt = dt.AddYears(-);
Person p1 = new Person("c++", dt);
Console.WriteLine("调用本地c++ dll中的类: " + p1.Age);
Console.WriteLine("调用本地c++ dll中的变量: " + Person.CallnVal());
Console.WriteLine("调用本地c++ dll中的函数: " + Person.CallnFunc()); Console.ReadLine();
}
}
}
参考资料详情:
https://www.codeproject.com/Articles/35041/Mixing-NET-and-native-code
https://blog.csdn.net/ganzheyu/article/details/50154705
托管代码中调用c++本地代码的更多相关文章
- [转]C# 互操作性入门系列(四):在C# 中调用COM组件
传送门 C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列(二):使用平台调用调用Win32 函数 C# 互操作性入门系列(三):平台调用中的数据封送处理 ...
- [转]在C#中调用C语言函数(静态调用Native DLL,Windows & Microsoft.Net平台)
原文:https://blog.csdn.net/yapingxin/article/details/7288325 对于不太了解.Net的人,如果想要了解.Net,我必须给他介绍P/Invoke.P ...
- 在.net中调用Delphi dll的Pchar转换
Pchar是非托管代码,要在.net中调用Delphi dll中的功能,请使用MarshalAs属性告知.net调用PInvoke去转换.net中标准的string类型.如果Delphi dll是De ...
- C#中调用Windows API的要点 .
介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...
- java中调用dll文件的两种方法
一中是用JNA方法,另外是用JNative方法,两种都是转载来的, JNA地址:http://blog.csdn.net/shendl/article/details/3589676 JNativ ...
- 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码
原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...
- C#中调用Dll动态链接库
C#中调用Dll动态链接库 起始 受限于语言的不同,我们有的时候可能会用别人提供的函数及方法 或者其他的什么原因.反正就是要调!!! 恰巧别人所使用的的语言跟自己又不是一样的 这个时候想要调用别人的函 ...
- [转][android][利用JNI技术在Android中调用、调试C++代码]
在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...
- [windows菜鸟]C#中调用Windows API的技术要点说明
在.Net Framework SDK文档中,关于调用Windows API的指示比较零散,并且其中稍全面一点的是针对Visual Basic .net讲述的.本文将C#中调用API的要点汇集如下,希 ...
随机推荐
- 服务器CentOS7上安装MySql
1.确保服务器系统处于最新状态 [root@localhost ~]# yum -y update如果显示以下内容说明已经更新完成Replaced:grub2.x86_64 1:2.02-0.64.e ...
- Python 一键安装全部依赖包
使用 pip requirements.txt 用来记录项目所有的依赖包和版本号,只需要一个简单的 pip 命令就能完成. pip freeze > requirements.txt 生成的文件 ...
- 嵊州D5T1 鸡翅 chicken
鸡翅 chicken [问题描述] 小 x 非常喜欢小鸡翅. 他得知 NSC 超市为了吸引顾客,举行了如下的活动: 一旦有顾客在其他超市找到更便宜的小鸡翅, NSC 超市将免费送给顾客 1000g ...
- 图像读取函数cv::imread()的几种使用方式
string imgpath = "C:\Users\Y\Pictures\miao.jpg"; OpenCV的imread()函数不支持单右斜线形式的路径,即不支持上述形式的路径 ...
- P3768 简单的数学题 [杜教筛,莫比乌斯反演]
\[\sum_{i=1}^{n}\sum_{j=1}^{n} ij\gcd(i,j)\] \[=\sum_{d=1}^{n} d \sum_{i=1}^{n}\sum_{j=1}^{n} ij[\gc ...
- appium 爬取微信的相册内容(不知什么时候能写完)
# crowl wechat through appium from appium import webdriver from selenium.webdriver.support.ui import ...
- python scraping webs - python取得NIPS oral paper列表
from lxml import html import requests # using xpath # page = requests.get('http://econpy.pythonanywh ...
- 浅识mysql主键
primary key:主键,又叫主键约束. primary key在表中是唯一代表一条记录的.primary key可以是1列,或者多列组合而成的. 如何查看一个表的主键是什么,举个例子: desc ...
- python里的复数complex
复数是一个数学概念,包含了实部和虚部.在python设计语言中,可以直接定义以j为单位,也可以使用complex函数创建复数,这个函数可以传实部和虚部,也可以只传实部. 我们把形如z=a+bj(a,b ...
- SpringMVC简单小结
一.MVC的的大概解释: MVC的核心思想是业务数据抽取和业务数据呈现相分离. MVC:M(Model)+V(View)+C(Controller) M(模型层):业务数据的信息表示,通常是业务实体 ...