简体   繁体   English

Visual Studio 2008-在C ++ / C#多项目解决方案中共享数据

[英]Visual Studio 2008 - Sharing Data in C++/C# Multi Project Solution

I have a solution with 2 projects: 我有2个项目的解决方案:

  1. a c++ main project 一个c ++主项目
  2. ac# project (display simulator) ac#项目(显示模拟器)

Today these 2 apps share data using a loopback TCP client/server connection, but that's not very optimal (timing issues..). 今天,这两个应用程序使用环回TCP客户端/服务器连接共享数据,但这并不是最佳选择(定时问题..)。

I was wondering if there was a way to access the c# data from the c++ project directly and vice versa? 我想知道是否有一种方法可以直接从c ++项目访问c#数据,反之亦然? (It seems to be possible with 2 c# projects..) If it's not possible, what's the best way to implement this with shared memory? (似乎可以用2个c#项目实现。)如果不可能,用共享内存实现此目的的最佳方法是什么?

thanks! 谢谢! Michael 麦可

EDIT: thanks for the answers. 编辑:感谢您的答案。 The 2 projects used to be independant solutions and are both executables - I'm actually trying to merge the 2 into 1 solution / executable. 2个项目曾经是独立的解决方案,并且都是可执行文件-我实际上是在尝试将2个项目合并为1个解决方案/可执行文件。 For info: The c++ app is a PC version of an embedded app - the c# app is a lcd/HMI simulator. 有关信息:c ++应用程序是嵌入式应用程序的PC版本-c#应用程序是LCD / HMI模拟器。

Converting the C++ project to a C++/CLI project might be the easiest way to go. 将C ++项目转换为C ++ / CLI项目可能是最简单的方法。 Note however that some code doesn't play well with C++/CLI (we've had problems using libraries that use boost::thread in a managed executable). 但是请注意,某些代码在C ++ / CLI中无法正常运行(在托管可执行文件中使用使用boost :: thread的库时,我们遇到了问题)。

You can use COM Interop or Platform Invoke to access native code in C#. 您可以使用COM InteropPlatform Invoke来访问C#中的本机代码。

If that's not what you're asking for, please elaborate. 如果这不是您要的内容,请详细说明。

Named Pipes? 命名管道?

For interprocess communication via named pipes you can check out the .NET 3.5 feature http://msdn.microsoft.com/en-us/library/system.io.pipes.aspx for the C# side. 对于通过命名管道的进程间通信,您可以在C#端签出.NET 3.5功能http://msdn.microsoft.com/en-us/library/system.io.pipes.aspx From the C++ side, I assume you know the equivalent :). 从C ++方面,我假设您知道等效的:)。

There are two ways I know of to get direct access to memory between c++ and c# without the overhead of marshaling/demarshaling and com. 我知道有两种方法可以直接访问c ++和c#之间的内存,而无需处理封送/拆组和com的开销。 If it is required to keep the c++ part of your code native then the only way I know to achieve this is to host the clr from your c++ application. 如果需要保留代码的c ++本机部分,那么我知道实现此目标的唯一方法是从c ++应用程序托管clr。 A relatively complicated undertaking. 一个相对复杂的任务。 Google "hosting the common language runtime". Google“托管公共语言运行库”。 Then you can load your c# code as an assembly, call into the c# code and provide a common shared memory style interface. 然后,您可以将c#代码作为程序集加载,调用c#代码并提供通用的共享内存样式接口。 Although you will have to write all of the shared memory support yourself as I have found no direct support for shared memory in c#/.net. 尽管您必须自己编写所有共享内存支持,因为在c#/。net中我没有找到对共享内存的直接支持。

The other way is to compile your c++ code with common language runtime support. 另一种方法是使用公共语言运行时支持来编译c ++代码。 This is by far easier and will allow you all the power and glory of c++ while allowing access to clr based data types, and the ability to call back and forth between your c++ and c# code. 到目前为止,这很容易,它将为您提供c ++的所有功能和荣耀,同时允许访问基于clr的数据类型以及在c ++和c#代码之间来回调用的能力。 See "Language Features for Targeting the CLR" in your VS2008 documentation. 请参阅VS2008文档中的“用于定位CLR的语言功能”。 pin_ptr will become your close friend and ally in this process. 在此过程中,pin_ptr将成为您的密友和盟友。

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

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