开发者

How to Interop to C++ with Namespace

开发者 https://www.devze.com 2023-02-03 16:24 出处:网络
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:

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 开发者_StackOverflow中文版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.


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
}

}


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号