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++本地代码的更多相关文章

  1. [转]C# 互操作性入门系列(四):在C# 中调用COM组件

    传送门 C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列(二):使用平台调用调用Win32 函数 C# 互操作性入门系列(三):平台调用中的数据封送处理 ...

  2. [转]在C#中调用C语言函数(静态调用Native DLL,Windows & Microsoft.Net平台)

    原文:https://blog.csdn.net/yapingxin/article/details/7288325 对于不太了解.Net的人,如果想要了解.Net,我必须给他介绍P/Invoke.P ...

  3. 在.net中调用Delphi dll的Pchar转换

    Pchar是非托管代码,要在.net中调用Delphi dll中的功能,请使用MarshalAs属性告知.net调用PInvoke去转换.net中标准的string类型.如果Delphi dll是De ...

  4. C#中调用Windows API的要点 .

    介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...

  5. java中调用dll文件的两种方法

    一中是用JNA方法,另外是用JNative方法,两种都是转载来的, JNA地址:http://blog.csdn.net/shendl/article/details/3589676   JNativ ...

  6. 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码

    原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...

  7. C#中调用Dll动态链接库

    C#中调用Dll动态链接库 起始 受限于语言的不同,我们有的时候可能会用别人提供的函数及方法 或者其他的什么原因.反正就是要调!!! 恰巧别人所使用的的语言跟自己又不是一样的 这个时候想要调用别人的函 ...

  8. [转][android][利用JNI技术在Android中调用、调试C++代码]

    在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...

  9. [windows菜鸟]C#中调用Windows API的技术要点说明

    在.Net Framework SDK文档中,关于调用Windows API的指示比较零散,并且其中稍全面一点的是针对Visual Basic .net讲述的.本文将C#中调用API的要点汇集如下,希 ...

随机推荐

  1. final 和 static之间的区别和联系

    关键字final和关键字static两者的含义并不相似,但是笔者常常使用一段时间后就会忘记它们之间的区别,因为它俩总是相伴着出现.当只出现其中一个时,就对其代表的含义不甚清晰了.故而特地将相关知识点记 ...

  2. STL-set&&multiset 集合

    #include <iostream> #include <cstdio> #include <set> // 仿函数的原型 //struct greaters / ...

  3. 在windows系统下,配置vue项目一键启动文件

    我的项目由客户端.后台管理.数据库和服务器三部分组件,每次启动项目都要一个一个启动,挺麻烦的,现在写一个.bat文件来批处理命令. 这个是我的启动文件内容. 第一行运行的我wampServer服务器, ...

  4. ng-做一个简单的通讯录--学习使用路由和HTTP

    app.module import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@ang ...

  5. 剑指offer-面试题12-矩阵中的路径-回溯法

    /* 题目: 设计一个函数,判断一个矩阵中是否存在一条包含该字符串所有字符的路径. 路径可从字符串的任意一格开始,每一步可向上.下.左.右移动一格. 如果一条路径经过了矩阵中的某一格,那么该路径不能再 ...

  6. 安全 - 内容安全策略(CSP)(未完)

    威胁 跨站脚本攻击(Cross-site scripting) 跨站脚本攻击Cross-site scripting (XSS)是一种安全漏洞,攻击者可以利用这种漏洞在网站上注入恶意的客户端代码. 攻 ...

  7. Scala之Option: Some None

    Option类型本身没有实现,而是依赖两个子类型提供具体实习那:Some和None.Some是iyge类型参数化的单元素集合,None是一个空集合. ----<scala学习手册>P119

  8. jvm gc 调优 实战

    非常不错的文章们 转自: 中文:http://blog.csdn.net/dragonassassin/article/details/51010947 http://josh-persistence ...

  9. 关于华为高斯数据库 GaussDB 版本及认证体系介绍

    目录 你需要知道的 技术有国界 从它的名称说起 你听到过的版本 你听到过的流言蜚语 各个版本的区别 版本未来名称 华为 GaussDB 认证体系介绍 GaussDB 其他资料相关链接 你需要知道的 任 ...

  10. 关于Euler-Poisson积分的几种解法

    来源:https://www.cnblogs.com/Renascence-5/p/5432211.html 方法1:因为积分值只与被积函数和积分域有关,与积分变量无关,所以\[I^{2}=\left ...