简体   繁体   English

如何使用命名空间与C ++交互

[英]How to Interop to C++ with Namespace

I have a C++ function, that I want to expose to C# for consumption. 我有一个C ++函数,我希望将其暴露给C#以供使用。 The catch is that the C++ code declaration is wrapped in namespace : 问题是C ++代码声明包含在命名空间中

namespace OhYeahNameSpace
{
 extern "C"  __declspec(dllexport) void Dummy();
}

My question is, how to define the corresponding C# structure? 我的问题是,如何定义相应的C#结构? I believe that C# code must be made aware of the existence of OhYeahNameSpace , am I right? 我相信必须让C#代码知道OhYeahNameSpace的存在,对吗?

Edit: I think a lot of people misunderstand my point ( no thanks due to a typo in my original example; fixed). 编辑:我认为很多人误解了我的观点(不用多谢,因为我原来的例子中有一个拼写错误;已修复)。 I am asking about how to go by if there is a namespace for which the exported function is dwell on. 我问的是如果存在导出函数所针对的命名空间,该如何过去。 One answer are missing on this part, another one says this can't be done and ask me to wrap it around, and another one is now currently with -1 vote. 这一部分缺少一个答案,另一个答案说不能这样做并要求我把它包起来,而另一个现在正在进行-1投票。

Why not wrap in C++/CLI? 为什么不用C ++ / CLI换行?

//ohyeah.h
namespace OhYeahNameSpace
{
  public ref class MyClass
  {
     void Dummy();
  }
}

//ohyeah.cpp
#include "ohyeah.h"
namespace OhYeahNameSpace
{
void MyClass::Dummy()
{
// call the real dummy in the DLL
}

}

Wrap as C and use P/Invoke: 换行为C并使用P / Invoke:

--- OhYeahNameSpace_C.h ---
#ifdef __cplusplus
extern "C" {
#endif

void _declspec(dllexport) OhYeahNameSpace_Dummy();

#ifdef __cplusplus
}
#endif

--- OhYeahNameSpace_C.c ---
#include "OhYeahNameSpace_C.h"
#include <OhYeahNameSpace.h>

void OhYeahNameSpace_Dummy()
{
  ::OhYeahNameSpace::Dummy();
}

The example isn't 100% complete, but you get the gist of it. 这个例子不是100%完整,但你得到了它的要点。

One possible solution would be to use P/Invoke (besides the already mentioned one wrapping in a C++/CLI class 一种可能的解决方案是使用P / Invoke(除了已经提到的C ++ / CLI类中的一个包装)

in C++ (I assume your function is in a project resulting in a DLL named UnmanagedCpp.dll: 在C ++中(我假设你的函数在一个项目中导致一个名为UnmanagedCpp.dll的DLL:

namespace OhYeahNameSpace
{
   extern "C" __declspec(dllexport) void Dummy(); //extern "C" to disable name mangling
}

in C#: 在C#中:

 class Program
 {
       [DllImport("UnmanagedCpp.dll")]
       public static extern void Dummy();

        static void Main(string[] args)
        {

            Dummy();
        }
 }

The C# app should be able to find the UnmanagedCpp.dll (ie you can copy it in the bin of the exe) C#应用程序应该能够找到UnmanagedCpp.dll(即你可以将它复制到exe的bin中)

EDIT: 编辑:

This solution has some downsides that should be considered. 该解决方案有一些应该考虑的缺点。 Following the comments, some things I did not state clearly when proposing my "solution". 在评论之后,我在提出“解决方案”时没有明确说明。

  1. Wrapping in C++/CLI is the preferable solution. 在C ++ / CLI中包装是首选解决方案。
  2. Currently the VS 2010 compiler (and its predecessors) disable name mangling of functions in namespaces if the function is exported with extern "C". 目前,如果使用extern“C”导出函数,VS 2010编译器(及其前身)将禁用名称空间中的函数名称修改。 However this is considered to be a bug and this means it could be fixed sometime in the future. 然而,这被认为是一个错误,这意味着它可以在将来的某个时候修复。 I will not hold my breath until they do... The bug one of the commenters provided a link to is reported in 2005 and 5 years later... Besides the current behavior is more like a feature IMHO. 我不会屏住呼吸,直到他们这样做...其中一个评论者提供了一个链接,在2005年和5年后报告...除了当前的行为更像是一个功能恕我直言。 If one wants the name properly decorated ( also with the namespace name ) then one should't declare it extern "C". 如果想要正确装饰名称(也使用命名空间名称),则不应将其声明为extern“C”。 But that's only my personal opinion. 但这只是我个人的意见。 So take this in consideration. 所以考虑到这一点。

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

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