Have Unmanaged c++ code call managed c++ code that calls c# code

时间:2016-04-21 22:35:53

标签: c# c++ unmanaged managed

I'm having trouble figuring out exactly how to reference managed c++ code that calls c# code from unmanaged c++ code. Let me throw out a few scenarios:

  1. I have my unmanaged code reference and call my managed code, my managed code's call to my c# code is commented out, builds and works just fine. I uncomment my c# code, I now get compiler errors saying my c# namespace doesn't exist.
  2. In my unmanaged code I comment out the reference and call to my managed code. My managed code calls my c# code. Builds and runs just fine... You get the picture from here.

Compiler error is C2653.

Here's what I'm doing:

Unmanaged c++ code: I've set the linker to include my managed c++ lib file.

#include "ManagedCpp.h"

ManagedCpp::foo();

Managed C++:

extern "C" __declspec(dllexport) void __stdcall foo()
{
    CssCode::bar();
}

C#

public static void bar()
{
    // From here it initializes some stuff from the registry 
    // into some data structures which I plan on marshaling 
    // back with other method calls, which I know involves placing
    // things on the stack that can be returned normally to the 
    // managed c++ code which then will need to be marshed back to the
    // unmanaged c++ code.
    // All code will be static.
}

What am I doing wrong? As far as I can tell I need to hide my c# calls from the unmanaged code but I'm not quite sure how to do that.

1 个答案:

答案 0 :(得分:0)

空头的错误。

我将来自托管c ++的调用放入标头中的c#代码。哎呦。现在一切似乎都很好!