I just tested this on Visual Studio 2005, which worked:
//Project: InteropTestCsLib, class1.cs
namespace InteropTestCsLib
{
public class Class1
{
public int Add(int a, int b) { return a+b; }
}
}
A C++/CLI project with an assembly reference to the C# one, and the project link option "Ignore import library" disabled (it was enabled by default)
//Project: InteropTestCppCliLib, InteropTestCppCliLib.h
namespace InteropTestCppCliLib {
public ref class MyWrapper abstract sealed
{
public:
static int Add(int a, int b);
};
}
//Project: InteropTestCppCliLib, InteropTestCppCliLib.cpp
namespace InteropTestCppCliLib {
int MyWrapper::Add(int a, int b)
{
InteropTestCsLib::Class1^ obj = gcnew InteropTestCsLib::Class1();
return obj->Add(a, b);
}
}
//Free function, neither in a namespace nor class, but still managed code
extern "C" int __stdcall MyWrapperAdd(int a, int b)
{
return InteropTestCppCliLib::MyWrapper::Add(a, b);
}
And the module-definition file (.def):
LIBRARY "InteropTestCppCliLib"
EXPORTS
MyWrapperAdd
And finally an empty native Win32 C/C++ project, with a project dependency on the C++/CLI DLL (or simply, a project that imports the import library it generated):
/*Project: InteropTestUnmanagedApp, main.c*/
#include <stdio.h>
__declspec(dllimport) int __stdcall MyWrapperAdd(int a, int b);
int main(void)
{
int x = 10;
int y = 32;
int result = MyWrapperAdd(x, y);
printf("%d + %d = %d\n", x, y, result);
return 0;
}
The C project called the C++/CLI one seamlessly.