从C ++调用C#dll [英] Calling C# dll from C++
问题描述
我有一个本机C ++ DLL,我想导入一个C#DLL并使用其某些功能(例如,连接到数据库).
I have a native C++ DLL, and I want to import a C# DLL and use some of its functions (for example connecting to a database).
现在我读到您可以将DLL转换为TLB COM文件,而我做到了没有问题.我的问题在于C ++.要调用该TLB文件,我需要设置CLR支持.我使用它们来帮助保护我所有的DLL和PEC.它们不支持.net DLL,当我启用CLR时,程序会将其识别为.net DLL.
Now I have read that you can turn the DLL into a TLB COM file, and I have done that no problems. My problem lies with the C++. To call that TLB file I need to set CLR support. I use themida to help secure all my DLL's as well as PEC. They do not support .net DLL's and when I enable CLR the programs recognize it as a .net DLL.
总有没有在不启用CLR的情况下从本地C ++ DLL内调用.net DLL内的c#函数吗?
Is there anyway to call a c# function inside a .net DLL from inside a native C++ DLL without enabling CLR?
推荐答案
我认为最好的方法是创建一个C ++/CLI库,您可以使用该库在各层之间进行通信.
The best way in my opinion is to crate a C++/CLI lib that you can use to communicate between the layers.
该第三方库将在启用/clr的情况下进行编译.
This 3rd party library will be compiled with /clr enabled.
然后,您可以将标头定义用于适当的c ++函数包装器,以调用C#函数.
Then you can use the header definitions to the proper c++ function wrappers to call the C# functions.
C ++/CLI可以帮助您以所需的方式管理呼叫,并让您以所需的方式设计类型强制转换.
C++/CLI can help you manage the call just the way you want it, and let you design the type casts the way you want them.
这篇关于从C ++调用C#dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!