托管代码中调用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的要点汇集如下,希 ...
随机推荐
- git签名设置
作用:只区分不同开发人员的身份 一.项目级别/仓库级别:仅在当前本地库范围内有效 签名设置用户名(UserName)和邮箱(User@email),邮箱可以是任意邮箱(无效邮箱也可以) git con ...
- 【读书笔记】自然语言处理综述 -- 第四章 -- N元语法
第四章 N元语法 本章开篇的两句话很有意思,代表了当时两个学派的思想和矛盾. 一句是"有史以来最伟大的语言学家"乔姆斯基说的:"句子的概率,在任何已知的对于这个术语的解释 ...
- Pikachu-URL重定向
不安全的url跳转 不安全的url跳转问题可能发生在一切执行了url地址跳转的地方.如果后端采用了前端传进来的(可能是用户传参,或者之前预埋在前端页面的url地址)参数作为了跳转的目的地,而又没有做判 ...
- 松软科技课堂:jQuery 效果 - 滑动
jQuery 滑动方法 通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以下滑动方法: slideDown() slideUp() slideToggle() jQuery sli ...
- 2019年3月最新可用KMS激活服务器地址
更新日期:2019/3/6 kms.03k.org kms.chinancce.com kms.lotro.cc cy2617.jios.org kms.shuax.com kms.luody.inf ...
- python ide 使用
pycharm jupyter 官方文档 使用 部署到服务器 参考 配置域名(反向代理) *.conf文件 server { listen ; server_name ju.iii.top; inde ...
- 将字符串日期格式化为yyyy-mm-dd
(CONVERT(varchar(100), CONVERT(datetime,a.con_ret_time), 23))
- Normalizing flows
probability VS likelihood: https://zhuanlan.zhihu.com/p/25768606 http://sdsy888.me/%E9%9A%8F%E7%AC%9 ...
- mysql 视图、触发器、事务、存储过程、函数
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- CentOS进行yum操作时不能访问国外镜像的解决方案
1. 例如执行yum update,经常报错“Cound not resolve host: xxxxx”,一般都是yum源是使用的国外镜像,国内访问很不好.这时可以将源手动替换为国内的清华大学源,或 ...