[英]using c# dll in project c++
i want use dll made in c#(visual studio 2008) in c++ project(visual studio 2003). 我想在c ++项目中使用c#(visual studio 2008)制作的dll(visual studio 2003)。 how to do that ? 怎么做 ? please heeelp 请嘿嘿
There is more than just COM interop, the MSDN FAQ also lists lesser known methods: 不仅仅是COM互操作, MSDN FAQ还列出了鲜为人知的方法:
2.2 How do I call .NET assembly from native Visual C++? 2.2如何从本机Visual C ++调用.NET程序集?
There are basically four methods to call .NET assembly from native VC++ code. 基本上有四种方法可以从本机VC ++代码调用.NET程序集。 Microsoft All-In-One Code Framework has working examples that demonstrate the methods. Microsoft All-In-One代码框架具有演示方法的工作示例。
Native VC++ module calls CLR Hosting APIs to host CLR, load and call the .NET assembly. Native VC ++模块调用CLR Hosting API来托管CLR,加载和调用.NET程序集。 (All-In-One Code Framework Sample Code: CppHostCLR) (一体化代码框架示例代码:CppHostCLR)
If the .NET assembly can be exposed as a COM component, native VC++ module can call into the .NET assembly through .NET – COM interop. 如果.NET程序集可以作为COM组件公开,则本机VC ++模块可以通过.NET - COM interop调用.NET程序集。 (All-In-One Code Framework Sample Code: CppCOMClient) (All-In-One代码框架示例代码:CppCOMClient)
Reverse PInvoke: the managed code call native passing a delegate the native code can call back. 反向PInvoke:托管代码调用本机传递本机代码可以回调的委托。 (All-In-One Code Framework Sample Code: CSPInvokeDll) (多合一代码框架示例代码:CSPInvokeDll)
If the module containing native VC++ code is allowed to enable CLR , the native VC++ code can call .NET assembly directly through the “It Just Works”, or “IJW”, mechanism. 如果允许包含本机VC ++代码的模块启用CLR ,则本机VC ++代码可以通过“It Just Works”或“IJW”机制直接调用.NET程序集。 (All-In-One Code Framework Sample Code: CppCLIWrapLib) (All-In-One代码框架示例代码:CppCLIWrapLib)
在将“Resolve #using References”(Project-> C / C ++ - > General)编译器选项设置到包含所需程序集的目录之后,然后在代码中添加这样的行。
#using <NiftyAssembly.dll>
You cannot directly use .NET assemblies from unmanaged C++ code. 您不能直接使用非托管C ++代码中的.NET程序集。 One possible solution is to expose the assembly as COM object using the regasm.exe utility and then consume it from C++. 一种可能的解决方案是使用regasm.exe实用程序将程序集公开为COM对象,然后从C ++中使用它。 Note that .NET types that need to be exposed as COM objects might need to be decorated with the [COMVisible(true)]
attribute and would still require the .NET framework installed on the target computer running the C++ code. 请注意,需要作为COM对象公开的.NET类型可能需要使用[COMVisible(true)]
属性进行修饰,并且仍然需要在运行C ++代码的目标计算机上安装.NET框架。
There are many ways to do this. 有很多方法可以做到这一点。
Is the C# DLL a COM DLL? C#DLL是COM DLL吗? If so you can use the regular COM API/Specification to access it in your C++ program. 如果是这样,您可以使用常规COM API /规范在C ++程序中访问它。 There are many tutorials on making your C# DLL COM visible. 有许多关于使C#DLL COM可见的教程。 It's not that difficult, a few compile switches and C# attributes basically. 它并不困难,基本上只有一些编译开关和C#属性。
Otherwise, can you compile your C++ project using the /clr compiler switch? 否则,您可以使用/ clr编译器开关编译C ++项目吗? If so you can use the Using Directive to import your C# DLL directly. 如果是这样,您可以使用Using Directive直接导入C#DLL。
As Darin said, one way is via COM. 正如达林所说,一种方式是通过COM。 The other two ways (I know of) are: 另外两种方式(我知道)是:
Make library file [.Tlb ] file and then only we can use the dll's. 制作库文件[.Tlb]文件然后我们才能使用dll。 You have to make .TLB (Type Library file) of your original c# DLL and use that.TLB library in the VC++ code. 您必须制作原始c#DLL的.TLB(类型库文件),并在VC ++代码中使用该.TLB库。 For that you have to register .TLB file on your pc as well as it is neccessary to import that .TLB file in your vc++ apllication. 为此,您必须在您的电脑上注册.TLB文件,并且必须在您的vc ++ apllication中导入该.TLB文件。 When you write C# DLL remember to use interfaces that would be implemented in the class. 编写C#DLL时,请记住使用将在类中实现的接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.