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. ThinkPHP v6.0.x 反序列化漏洞利用

    前言: 上次做了成信大的安询杯第二届CTF比赛,遇到一个tp6的题,给了源码,目的是让通过pop链审计出反序列化漏洞. 这里总结一下tp6的反序列化漏洞的利用. 0x01环境搭建 现在tp新版本的官网 ...

  2. PTA 1005 Spell It Right

    题目描述: Given a non-negative integer N, your task is to compute the sum of all the digits of N, and ou ...

  3. C#排序算法的实现---快速排序

    快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的 ...

  4. tensorflow数据集加载

    本篇涉及的内容主要有小型常用的经典数据集的加载步骤,tensorflow提供了如下接口:keras.datasets.tf.data.Dataset.from_tensor_slices(shuffl ...

  5. 剑指offer-面试题24-反转链表-链表

    /* 题目: 定义一个函数,输入链表的头结点,反转链表输出反转后链表的头节点. */ /* 思路: 记录当前节点的next和pre. 断开当前节点指向next的指针,指向pre. */ #includ ...

  6. win10c盘被下满文件解决办法

    今天更新系统,发现一个巨坑,好不容易软件配置的环境,开始以为是病毒,重装后在弄发现还是这个问题.经过两天的亲测解决办法: win7 svchost.exe占用内存和CPU很高,电脑很卡的解决方法:ht ...

  7. CF1227F2 Wrong Answer on test 233 (Hard Version)

    题意 \(n\)道题,每道题有\(k\)种选项,其中第\(i\)道题正确答案是\(a_i\),但是填答案的时候填错啦,第一道题的选择填到了第二道题...第\(n\)道题的选择填到了第一道题,求在\(k ...

  8. 暂停后保存sql server profiler的跟踪结果

  9. Sql Server2008忘记sa登陆密码

    Sql的sa登陆密码忘记解决方法: 语句执行的前提: 1.系统可以登陆进去(当不记得sa密码的时候,可以使用windows用户验证的方式进行登陆) 2.平时用sa登陆,点了记住密码但是不记得密码是多少 ...

  10. stylelint

    "number-leading-zero": "never", // 去掉小数点前面的0 "prettier.stylelintIntegration ...