我如何在c#中使用c ++ dll

编程入门 行业动态 更新时间:2024-10-28 10:29:36
本文介绍了我如何在c#中使用c ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我喜欢在我的c#应用程序中导入c ++ dll我该怎么办?我需要使用什么概念?

解决方案>

假设这个使用MinGW编译的DLL。 在C-sharp你可以遵循这个代码:

使用System.Runtime.InteropServices; 使用系统; class call_dll { [StructLayout(LayoutKind.Sequential,Pack = 1)] private struct STRUCT_DLL { public Int32 count_int; public IntPtr ints; } [DllImport(mingw_dll.dll)] private static extern int func_dll( int an_int, [MarshalAs(UnmanagedType.LPArray )] byte [] string_filled_in_dll, ref STRUCT_DLL s ); public static void Main(){ byte [] string_filled_in_dll = new byte [21]; STRUCT_DLL struct_dll = new STRUCT_DLL(); struct_dll.count_int = 5; int [] ia = new int [5]; ia [0] = 2; ia [1] = 3; ia [2] = 5; ia [3] = 8; ia [4] = 13; GCHandle gch = GCHandle.Alloc(ia); struct_dll.ints = Marshal.UnsafeAddrOfPinnedArrayElement(ia,0); int ret = func_dll(5,string_filled_in_dll,ref struct_dll); Console.WriteLine(返回值:+ ret); Console.WriteLine(String filled in DLL:+ System.Text.Encoding.ASCII.GetString(string_filled_in_dll)); } } $ / pre>

i like import c++ dll in my c# application how can i Do this?, what is concept i need to use for this?

解决方案

suppose this DLL that compile with MinGW. in C-sharp you can follow this code:

using System.Runtime.InteropServices; using System; class call_dll { [StructLayout(LayoutKind.Sequential, Pack=1)] private struct STRUCT_DLL { public Int32 count_int; public IntPtr ints; } [DllImport("mingw_dll.dll")] private static extern int func_dll( int an_int, [MarshalAs(UnmanagedType.LPArray)] byte[] string_filled_in_dll, ref STRUCT_DLL s ); public static void Main() { byte[] string_filled_in_dll = new byte[21]; STRUCT_DLL struct_dll = new STRUCT_DLL(); struct_dll.count_int = 5; int[] ia = new int[5]; ia[0] = 2; ia[1] = 3; ia[2] = 5; ia[3] = 8; ia[4] = 13; GCHandle gch = GCHandle.Alloc(ia); struct_dll.ints = Marshal.UnsafeAddrOfPinnedArrayElement(ia, 0); int ret=func_dll(5,string_filled_in_dll, ref struct_dll); Console.WriteLine("Return Value: " + ret); Console.WriteLine("String filled in DLL: " + System.Text.Encoding.ASCII.GetString(string_filled_in_dll)); } }

更多推荐

我如何在c#中使用c ++ dll

本文发布于:2023-06-10 18:29:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/616398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   dll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!