简体   繁体   English

VS2010中的C ++运行针对.NET 4.5的托管C#DLL

[英]C++ in VS2010 running a managed C# DLL targeting .NET 4.5

I have a C++ project, created in Visual Studio 2010, which needs to execute code from a C# DLL. 我有一个在Visual Studio 2010中创建的C ++项目,它需要从C#DLL执行代码。 I have done so using the 我已经这样做了

#import "pathname\filename.tlb" raw_interfaces_only

directive, as well as the CoInitialize(NULL) and CoUninitialize() COM calls. 指令,以及CoInitialize(NULL)和CoUninitialize()COM调用。

The C# DLL was compiled in VS2012, and targeted .NET 4.5. C#DLL在VS2012中编译,目标是.NET 4.5。

I was able to compile and run the project, and it seemed as though everything was fine, but we were noticing differences in some function calls. 我能够编译并运行该项目,似乎一切都很好,但我们注意到一些函数调用的差异。 This was verified by loading the exact same C++ project into VS 2012, turning on /clr in Configuration Properties, and changing the Targeted Framework (of the C++ project) to v4.5 by modifying the .vcxproj file in Notepad. 通过将完全相同的C ++项目加载到VS 2012,在配置属性中打开/ clr,并通过修改记事本中的.vcxproj文件将目标框架(C ++项目)更改为v4.5来验证这一点。

So, I guess my questions are: 所以,我想我的问题是:

  1. Why does a C++ project need to target a .NET framework? 为什么C ++项目需要针对.NET框架? Shouldn't the C# DLL be able to handle that by itself? C#DLL本身不应该能够处理吗? (Answered by Hans Passant) (Hans Passant回答)

  2. Why is it possible to run a C#.NET v4.5 DLL through C++ with /clr off, and not have any warnings that it might not work? 为什么可以通过带有/ clr off的C ++运行C#.NET v4.5 DLL,并且没有任何警告它可能不起作用? (Answered by Hans Passant) (Hans Passant回答)

  3. Why do I even get results back from the function call? 为什么我甚至从函数调用中得到结果? How is it possible that the C# DLL "works" (returns coherent results) at all? C#DLL怎么可能“有效”(返回连贯的结果)呢? Why does it return different values when targeting .NET v4.0? 为什么在针对.NET v4.0时会返回不同的值?

  4. Is it possible to use the C# DLL from the C++ code, while still using VS 2010? 是否可以使用C ++代码中的C#DLL,同时仍然使用VS 2010? Or do I need to switch the project to VS 2012? 或者我是否需要将项目切换到VS 2012?

Thanks in advance. 提前致谢。

A native C++ project does not target the .NET Framework at all. 本机C ++项目根本不以.NET Framework为目标。 The project creation wizard is a bit clumsy, it does not hide the combobox when you create the C++ project. 项目创建向导有点笨拙,在创建C ++项目时它不会隐藏组合框。

What framework version the C# DLL needs is established when you register it. 注册时,会建立C#DLL所需的框架版本。 Either done automatically when you tick the "Register for COM interop" build setting or explicitly by running Regasm.exe. 勾选“注册COM互操作”构建设置或通过运行Regasm.exe明确地自动完成。

That native C++ code can call C# code without trouble is just a goody you get from using COM. 本机C ++代码可以毫无问题地调用C#代码,这只是使用COM得到的一个好东西。 Whose basic purpose is to provide the contracts and the glue to allow one language to call code written in another. 其基本目的是提供合同和粘合剂,以允许一种语言调用另一种语言编写的代码。 The CLR has very good COM support and makes it look very easy. CLR具有非常好的COM支持,使其看起来非常简单。 That's not the case for C++, writing COM client code is generally unpleasant due to the verbosity and it is pretty easy to make mistakes. 对于C ++来说情况并非如此,编写COM客户端代码通常会由于冗长而令人不快,并且很容易出错。 Using raw_interfaces_only is a mistake, you don't get the benefit of memory management with smart pointers and error handling with exceptions. 使用raw_interfaces_only是一个错误,你不会通过智能指针和异常错误处理获得内存管理的好处。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM