2

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.

Wes
  • 77
  • 7
  • Provide more context. What is the compiler error? What is the definition of `CssCode` class, namely `bar()` method? Is it a `public static` method? – Xiaoy312 Apr 21 '16 at 22:41
  • I'll edit the post to give some more information. – Wes Apr 21 '16 at 22:42
  • You can mix unmanaged and managed C++ in the same compilation unit. No need to move managed C++ into a separate DLL. Just sayin'. Also, you can call a C# dll straight from unmanaged C++, either via COM-visible assemblies or via the CorXXX family of API. – Seva Alekseyev Apr 21 '16 at 22:51
  • In this case I'm attempting to interface with the CppUnitTestFramework, so setting my unmanaged code /clr flag causes the internal test engine I have to use to be unable to find these tests, so your first suggest isn't viable per my needs. As to your second suggestion the lead engineer architect for this task has recommended to use the wrapper approach for numerous reasons so your second solution I cannot use either due to greater minds then my own. – Wes Apr 21 '16 at 22:59
  • Have you added a reference to the C# assembly from the C++ DLL? – Andy Apr 21 '16 at 23:04

1 Answers1

0

Empty headed mistake.

I was putting the call from the managed c++ to the c# code in the header. Whoops. Everything seems to work just fine now!

Wes
  • 77
  • 7