C#如何调用由非托管代码文件功能编写的C ++ dll [英] C# how to call a C + + dll which written by unmanaged code file function

查看:100
本文介绍了C#如何调用由非托管代码文件功能编写的C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中如何调用由非托管代码文件功能编写的C ++ dll?我在线检查并以委托调用的形式提供了dllimport的使用,但是当数量相对较小时调用此函数很容易,但是例如,如果需要调用的函数数量更多,我编写一个C ++ dll文件包含有50个函数,并且如果所有C#都使用dllimport方式引用,那么定义的代码将非常长.虽然效率不高,请问老师有一个快速的方法来实现函数调用的C ++ dll文件吗?与在C#中调用C#函数时一样,当通过引用命名库以获取名称空间时,该函数可以在自己使用的情况下执行.专家请教!谢谢.

In C# how to call a C++ dll which written by unmanaged code file function? I checked online and the use of dllimport are provided in the form of commission calls, but this function is called when the number is relatively small it is quite easy, but if the number of functions need to call more, for example, I write a C++ dll file contains there are 50 functions, and that if all the C# reference to the use of dllimport way, then the defined code will very long. While efficiency is not high, may I ask the teacher has a quickly way to achieve C++ dll file for the function call? As in C# call C# function when the library is named by reference to get a namespace and then the function can be carried out in their own use. Experts your advise! Thank you.

推荐答案

我只能看到一种可以更安全,更快速地实现的方法,尤其是在您确实有很多导出的非托管函数要使用的情况下.使用C ++/CLI编写混合模式(托管+取消管理)库.它可以使用与DLL头文件中完全相同的声明来使用非托管DLL.另一方面,您可以创建托管类型以公开导入的功能,并将混合模式DLL用作常规.NET程序集.您可以在C#代码中将其引用为任何其他程序集.

—SA
I can see only one way which can be safer and faster in implementation, especially if you really have many exported unmanaged function to use. Write a mixed-mode (managed+unmanages) library using C++/CLI. It can use unmanaged DLL using the exact same declarations you have in the DLL''s header files. From the other hand, you can create managed types to expose imported functionality and use your mixed-mode DLL as a regular .NET assembly. You can reference it as any other assembly in your C# code.

—SA


AFIK,您应该使用"dllimport方式",没有快捷方式.
但是,如果您可以修改源DLL,则可以考虑构建函数的简化接口"(一种外观模式 [ ^ ])或将其打包为COM组件.
AFIK, you should use the ''dllimport way'' there is no shortcut for that.
However, if you can modify the source DLL then you may consider building a ''simplified interface to your functions'' (a sort of Facade pattern[^]) or pack it as COM component.


这篇关于C#如何调用由非托管代码文件功能编写的C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆