首页 > 编程语言 > 详细

C# 调用C++ dll

时间:2018-12-11 12:17:55      阅读:144      评论:0      收藏:0      [点我收藏+]

C#调用C++封装好的函数.

C++ Dll1.cpp:

1 #define _csharp_string extern "C" __declspec(dllexport) const char*
2 
3 _csharp_string func(){
4   return "Hi,沙奇码"; 
5 }

C#调用:

 1 using System;
 2 using System.Text;
 3 using System.Runtime.InteropServices;
 4 
 5 namespace ConsoleApp1
 6 {
 7    class Program
 8    {
 9       //导入dll
10       [DllImport(@"D:\Dll1.dll"), EntryPoint = "func", 
CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl] 11 //引用外部实现的函数,若C++中函数返回类型为指针,则C#中使用IntPtr作为返回类型,
//若非指针类型,对应返回类型声明即可
12 extern static IntPtr func(); 13 14 static void Main(string[] args) 15 { 16 //使用Marshal封装的函数取指针值 17 Console.WriteLine(Marshal.PtrToStringAnsi(func())); 18 } 19 } 20 }

 

C# 调用C++ dll

原文:https://www.cnblogs.com/seanyan/p/10101150.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!