我嘗試按照此步驟: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