简体   繁体   English

从非托管C Dll加载托管C ++ Dll?

[英]Load Managed C++ Dll from Unmanaged C Dll?

I have an off the shelf product which supports custom plugins which need to be written in Unmanaged C. It doesn't support Managed Dll's and my language of preference is c#. 我有一个现成的产品,它支持需要用Unmanaged C编写的自定义插件。它不支持Managed Dll,我的首选语言是c#。

The information which needs to be passed back to the off the shelf product is very basic and can be held in a string. 需要传递回现货产品的信息非常基本,可以保存在一个字符串中。

So I was thinking I could do the following: 所以我想我可以做到以下几点:

  1. Write the bulk of my code in a C# Dll. 将我的大部分代码写在C#Dll中。
  2. Write a wrapper in C++ Managed Code which calls my C# Methods. 在C ++管理代码中编写一个包装器,它调用我的C#方法。
  3. Write a basic Dll in unmanaged C which calls the Managed C++ Dll. 在非托管C中编写一个基本的Dll,它调用托管C ++ Dll。

Now communicating between a Managed C++ and C# Dll is easy. 现在,在托管C ++和C#Dll之间进行通信很容易。

But I cant figure out how to call a managed c++ function from an unmanaged c dll. 但我无法弄清楚如何从非托管c dll调用托管c ++函数。 Any help with some simple sample code would be great. 任何简单示例代码的帮助都会很棒。

Thanks 谢谢

EDIT: I created a Code Project article on how I did this with Alex's answer below. 编辑:我创建了一个代码项目文章,关于我是如何使用Alex的答案在下面做的。 http://www.codeproject.com/Tips/695387/Calling-Csharp-NET-methods-from-unmanaged-code http://www.codeproject.com/Tips/695387/Calling-Csharp-NET-methods-from-unmanaged-code

You almost got it right. 你几乎做对了。 Just put numbers 2 and 3 in the same C++/CLI DLL. 只需将数字2和3放在同一个C ++ / CLI DLL中。 You can both use .NET classes and export C functions from a C++/CLI project. 您既可以使用.NET类,也可以从C ++ / CLI项目中导出C函数。

extern "C" __declspec( dllexport ) void __stdcall PureExportC(
                                                    const wchar_t* param
)
{
    CSharpAssemblyNamespace::CSharpWorker worker;
    worker.DoWork( gcnew String(param) );
}

I don't think, you can do this. 我不认为,你可以做到这一点。 But you can write a c++ (unmanaged) plugin for the product. 但是你可以为产品编写一个c ++(非托管)插件。 Then write a stand allone managed app with c#, start it from the plugin and communicate between them using named pipes or sockets. 然后使用c#编写一个支持allone托管应用程序,从插件启动它并使用命名管道或套接字在它们之间进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在非托管c ++ dll中使用托管c#dll - Using a managed c# dll inside an unmanaged c++ dll 将非托管 dll 嵌入托管 C# dll - Embedding unmanaged dll into a managed C# dll 通过托管C ++包装程序从非托管C ++ dll运行C#函数 - Run a C# function from an unmanaged C++ dll through a managed C++ Wrapper 将依赖的托管DLL添加到非托管C ++项目 - Add managed DLL dependencied to unmanaged C++ project 将IWICStream / IStream从非托管C ++ DLL返回到托管C#并读取它 - Returning IWICStream / IStream from unmanaged C++ DLL to managed C# and reading it 从C#windows服务调用C ++ DLL(非托管代码)(用托管代码编写) - Calling a C++ dll (unmanaged code) from a C# windows service (written in managed code) 在从托管C#应用程序进行的非托管C ++ Dll调用中,如何处理数据类型之间的冲突 - In Unmanaged C++ Dll call From Managed C# Application, How to handle conflicts between Datatypes 如何将结构从C#托管结构传递到C ++非托管DLL并得到结果结构? - How can I pass struct from C# managed to C++ unmanaged DLL and get struct in result? 无法将托管的C ++ dll加载到C#Dll中 - Unable to load the Managed C++ dll into C# Dll 如何从 C++ 非托管/本机 DLL 中的免注册 COM C# 托管 DLL 创建 COM 对象 - How to create a COM object from registration free COM C# managed DLL in a C++ unmanaged/native DLL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM