我尝试按照以下步骤操作:Calling C# from C++, Reverse P/Invoke, Mixed Mode DLLs and C++/CLI 1. 我将 C# Dll 命名为 TestLib:
namespace TestLib
{
public class TestClass
{
public float Add(float a, float b)
{
return a + b;
}
}
}
2. 然后我创建名为 WrapperLib 的 C++/CLI Dll 并添加对 C# TestLib 的引用。
// WrapperLib.h
#pragma once
using namespace System;
using namespace TestLib;
namespace WrapperLib {
public class WrapperClass
{
float Add(float a, float b)
{
TestClass^ pInstance = gcnew TestClass();
//pInstance
// TODO: Add your methods for this class here.
return pInstance->Add(a, b);
}
};
}
C+ 3. 对于检查 tis 示例,我创建了 C++/CLI 控制台应用程序并尝试调用此代码:
// ConsoleTest.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace WrapperLib;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
WrapperClass cl1 = new WrapperClass();
return 0;
}
但我得到一些错误:
error C2065: 'WrapperClass' : undeclared identifier C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp 11 1 ConsoleTest
error C2146: syntax error : missing ';' before identifier 'cl1' C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp 11 1 ConsoleTest
error C2065: 'cl1' : undeclared identifier C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp 11 1 ConsoleTest
error C2061: syntax error : identifier 'WrapperClass' C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp 11 1 ConsoleTest
好吧,我知道我错过了某个地方,但是在哪里呢?