How to Interop to C++ with Namespace

浪尽此生 提交于 2019-12-21 20:26:12

问题


I have a C++ function, that I want to expose to C# for consumption. The catch is that the C++ code declaration is wrapped in namespace:

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

My question is, how to define the corresponding C# structure? I believe that C# code must be made aware of the existence of OhYeahNameSpace, am I right?

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?

//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
}

}



回答2:


Wrap as C and use 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.




回答3:


One possible solution would be to use P/Invoke (besides the already mentioned one wrapping in a C++/CLI class

in C++ (I assume your function is in a project resulting in a DLL named UnmanagedCpp.dll:

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

in 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 (i.e. you can copy it in the bin of the exe)

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.
  2. Currently the VS 2010 compiler (and its predecessors) disable name mangling of functions in namespaces if the function is exported with extern "C". 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. If one wants the name properly decorated ( also with the namespace name ) then one should't declare it extern "C". But that's only my personal opinion. So take this in consideration.


来源:https://stackoverflow.com/questions/4669792/how-to-interop-to-c-with-namespace

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