标签:services str cdecl har rto bsp def main define
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 }
标签:services str cdecl har rto bsp def main define
原文地址:https://www.cnblogs.com/seanyan/p/10101150.html