How to interact with C ++ using a namespace

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

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

My question is how to determine the appropriate C # structure? I believe C # code should be aware of the existence OhYeahNameSpace, am I right?

Edit: I think many people misunderstand my point of view (thanks for the typo in my original example, fixed). I ask how to get through if there is a namespace for which the exported function is enabled. One answer is missing in this part, another says that this cannot be done, and ask me to wrap it, and another one now has -1 vote.

+3
source share
3 answers

Why not flip 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
}

}
+3
source

Wrap like 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 is not 100% complete, but you will get its essence.

+1
source

P/Invoke ( ++/CLI

++ ( , , DLL UnmanagedCpp.dll:

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

#:

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

        static void Main(string[] args)
        {

            Dummy();
        }
 }

# UnmanagedCpp.dll(.. exe)

EDIT:

, . , , "".

  • ++/CLI.
  • VS 2010 ( ) , extern "C". , , . , ... , , 2005 5 ... , IMHO. , ( ), extern "C". . .
0

Source: https://habr.com/ru/post/1784967/


All Articles