问题描述
我有C#DLL。贝娄是代码
I have C# dll. Bellow is code
public class Calculate
{
public static int GetResult(int arg1, int arg2)
{
return arg1 + arg2;
}
public static string GetResult(string arg1, string arg2)
{
return arg1 + " " + arg2;
}
public static float GetResult(float arg1, float arg2)
{
return arg1 + arg2;
}
public Calculate()
{
}
}
现在,我计划从调用C ++这个DLL在这条路上。
Now, I am planning to call this dll from C++ on this way.
[DllImport("CalculationC.dll",EntryPoint="Calculate", CallingConvention=CallingConvention::ThisCall)]
extern void Calculate();
[DllImport("CalculationC.dll",EntryPoint="GetResult", CallingConvention=CallingConvention::ThisCall)]
extern int GetResult(int arg1, int arg2);
下面是函数,其中被称为调用getResult
Here is function where is called GetResult
private: System::Void CalculateResult(int arg1, int arg2)
{
int rez=0;
//Call C++ function from dll
Calculate calculate=new Calculate();
rez=GetResult(arg1,arg2);
}
我得到了错误:语法错误:标识符'计算'。
谁能帮我这个可怕的错误?
I got the error : "syntax error : identifier 'Calculate'".Can someone help me with this terrible error?
推荐答案
您必须使用C ++ CLI,否则你不能打电话的DllImport。
。如果这是你可以参考C#DLL的情况下
You must be using c++ CLI, otherwise you could not call DllImport.If that is the case you can just reference the c# dll.
在C ++ CLI中,您可以做如下:
In c++ CLI you can just do as follows:
using namespace Your::Namespace::Here;
#using <YourDll.dll>
YourManagedClass^ pInstance = gcnew YourManagedClass();
其中YourManagedClass'与输出单元YourDll.dll的C#项目定义。
where 'YourManagedClass' is defined in the c# project with output assembly 'YourDll.dll'.
**编辑**
增加了你的榜样。
** EDIT **Added your example.
这是你的榜样,需要怎样的样子在CLI(为清楚起见,我假设使得G
etResult不是一个静态的功能,否则,你只会调用计算::调用getResult(...)
This is how your example needs to look like in CLI (for clarity I am assuming that GetResult is not a static function, otherwise you would just call Calculate::GetResult(...)
private: System::Void CalculateResult(int arg1, int arg2)
{
int rez=0;
//Call C++ function from dll
Calculate^ calculate= gcnew Calculate();
rez=calculate->GetResult(arg1,arg2);
}
这篇关于从C ++ / CLI调用C#DLL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!