Native C++ and C# interop

允我心安 提交于 2020-01-03 15:18:13

问题


So I'm architecting an application that does necessarily C++ work, but MFC/ATL is too messy for my liking, so I had this brilliant idea of doing all the "thinking" code in native C++ and all the pretty UI code in C#. The problem, though, is interoperability between the two of them. Before I get too carried away with this, I was wondering if this is a solved problem, and there's a really good way to do this. Note that I don't want to mix logic and display in the same module, as it gives rise to annoyingly high coupling.

Here's what I have so far:

So tell me, can it be done better?


回答1:


The easiest way to handle this is to use C++/CLI, and expose your logic as .NET types.

It's very easy to wrap a native C++ class in a ref class that's usuable directly from a C# user interface.

That being said - this was my plan, originally, in my current project. My thinking was that I'd need the native code for some of the heavy math work we typically do. I've found, however, that it's been easier, faster, and nicer to just move most of my logic directly into C# (separated from the UI code, but still in a C# assembly) rather than try to implement it in C++.

My experience has been that speed has not been an issue - unsafe C# code has nearly always managed to be as fast or faster than the equivelent C++ when tuned, and it's easier to profile and tune the C# code.




回答2:


You may have a good reason for your core to be a DLL. But it isn't necessary. I've had success with an application that is C++, it creates an App Domain and loads an assembly into it, then instantiates an object in the app domain and calls it, passing it an interface to the application. This is basically what the C-runtime does when you make an unmanaged app anyway.

The interface that the C++ code exposes to the C# code is defined in C#. I think this is easier than defining the intefaces in pure COM. C# interop gives you more control of the marshalling than C++ does.

  • create a file app_interface.cs that defines the interface
  • compile app_interface.cs into a dll, we don't care about the dll. we need the type library.
  • in the C++ code #import <app_interface.tlb> raw_interfaces_only this turns the typelib into a C++ .h file with interface definitions: app_interface.tlh
  • Implement the interfaces

You can sprinkle your app_interface.cs with attributes that control the marshalling like this

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("ffffffff-ffff-ffff-ffff-ffffffffffff")] // guid changed from what I really use 
public interface IMyHostApplication
{
   ...

   void   AddErrorDetails (
      [MarshalAs(UnmanagedType.Error)] uint hr, 
      [MarshalAs(UnmanagedType.LPWStr)] string  szErrDetails);

   ...
}


来源:https://stackoverflow.com/questions/1987284/native-c-and-c-sharp-interop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!