C# 调用C++

12 篇文章 0 订阅

关于C#调用C++ dll, 以前写的博客找不到了,只能重新再写。


1)可以简单的参考:

Calling an unmanaged dll from .NET (C#)
https://blogs.msdn.microsoft.com/jonathanswift/2006/10/02/calling-an-unmanaged-dll-from-net-c/


注意点:

  1. extern "C" 必须加,否则导出的函数名会加入乱七八糟的后缀用于overloaded。
  2. dumpbin 命令可以看到导出的函数名
  3. [StructLayout(LayoutKind.Sequential)] 

 


2)CLI/C++可以当做bridge来连接MFC和.net framework
创建一个MFC project后,利用下面方法可以支持CLR
https://msdn.microsoft.com/en-us/library/ms235211.aspx?f=255&MSPPError=-2147217396

http://www.dorodnic.com/blog/2014/12/10/calling-cpp-by-example/

注: MFC的CString转化成string:  String^ str = marshal_as<String^>(strRungComment);

另外如果在CLI C++头文件定义函数后:

    namespace Cpp
    {
        // This is our native implementation
        // It's marked with __declspec(dllexport) 
        // to be visible from outside the DLL boundaries
        class __declspec(dllexport) Logic
        {
        public:
            int Get() const; // That's where our code goes
        };
    }


CPP文件可用下列方式实现:

#include "Logic.h"

int Cpp::Logic::Get() const
{
    return 42; // Really, what else did you expect?
}


3) Marshal

https://msdn.microsoft.com/en-us/library/bb384865.aspx
https://msdn.microsoft.com/en-us/library/ms235282.aspx
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值