Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Interop to C++ with Namespace

Tags:

c++

c#

interop

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.

like image 902
Graviton Avatar asked Sep 20 '25 19:09

Graviton


2 Answers

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
}

}
like image 97
T33C Avatar answered Sep 22 '25 08:09

T33C


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.

like image 45
rjnilsson Avatar answered Sep 22 '25 09:09

rjnilsson