托管代码中调用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的要点汇集如下,希 ...
随机推荐
- 【计算语言学实验】基于 Skip-Gram with Negative Sampling (SGNS) 的汉语词向量学习和评估
一.概述 训练语料来源:维基媒体 https://dumps.wikimedia.org/backup-index.html 汉语数据 用word2vec训练词向量,并用所学得的词向量,计算 pku_ ...
- PP: Extracting statisticla graph features for accurate and efficient time series classification
Problem: TSC, time series classification; Traditional TSC: find global similarities or local pattern ...
- 获取WEB图片
public string GetJpgFile(string strFileServerPath ,string strReportDir) { string strPath = "&qu ...
- 嵊州D5T2 折纸 folding
折纸 folding [问题描述] 在非常紧张的 NOIP 考试中,有人喜欢啃指甲,有人喜欢转铅笔,有人喜欢撕 纸条,……而小 x 喜欢迷折纸. 现有一个 W * H 的矩形纸张,监考老师想知道,小 ...
- 《操作系统真象还原》ELF文件
下面是第五章部分内容的收获. 用C语言编写内核 一直以来我们都是用汇编语言编写程序的,但接下来我们或许很少用汇编语言编写代码了,大多数都是使用C语言.为什么要这样呢?书上的解释我看的不是很懂,只能结合 ...
- python的for循环的神奇之处
python的for循环太神奇了: 你可以编写这样的语句: for i in range(10) : j= i**2 print(j) 你也可以编写这样的语句: with open('/path/to ...
- densenet思路 以及和残差网络区别,pytorch实现
densenet思路 以及和残差网络区别,pytorch实现 待办 densenet思路以及和残差网络区别.以及densenet的pytorch实现 https://zhuanlan.zhihu.co ...
- mybatis插入嵌套对象
今晚做项目遇上了一个需求,需要插入嵌套对象. 对象结构是这样的: public class RegisterMsg{ private Header header; private short pro ...
- 【LInux01】学习Linux课程体系
知识 =>技能 需要大量的练习 相当于复盘 要有成就感 在一个领域深挖,再迁移到其他领域 1.两周以后的知识留存率: 主动学习: 动手实践:40% 讲给别人听:70% 写博客:是写教程,便 ...
- (三)运用Python模块和包
1 引言 为了能够在Python项目中高效地运用Python模块和包,我们需要进一步地来了解它们是如何在Python项目中进行定义.使用和工作的. 2 Python模块和包 Python模块和包的基本 ...