我尝试按照此步骤:Calling C# from C++, Reverse P/Invoke, Mixed Mode DLLs and C++/CLI 1.我让C#DLL的命名TESTLIB:调用C#函数C
namespace TestLib
{
public class TestClass
{
public float Add(float a, float b)
{
return a + b;
}
}
}
2.然后,我创建C++/CLI的Dll名为WrapperLib和添加对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.检查那朵例子中,我创建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
那么我知道我错过了什么地方,但在哪里?
如果编译器告诉你“嗨,出现了一些错误,请修复”,你首先看哪里? :)请告诉我们错误是什么。 –
已修复。我添加了VS输出。如何从本地C++或C右键调用该函数? – Superjet100