从 C++/CLI 调用 C# dll 函数
我有一个C#
dll。代码如下:
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。
[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 的函数,
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 have a C#
dll. The code is below:
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()
{
}
}
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);
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用 c++ CLI,否则无法调用 DllImport。
如果是这种情况,您只需引用 c# dll 即可。
在 c++ CLI 中,您可以执行以下操作:
其中“YourManagedClass”在 c# 项目中定义,输出程序集为“YourDll.dll”。
** 编辑 **
添加了您的示例。
这就是您的示例在 CLI 中的样子(为了清楚起见,我假设 G
etResult 不是静态函数,否则您只需调用Calculate::GetResult(...)
You must be using c++ CLI, otherwise you could not call DllImport.
If that is the case you can just reference the c# dll.
In c++ CLI you can just do as follows:
where 'YourManagedClass' is defined in the c# project with output assembly 'YourDll.dll'.
** EDIT **
Added your example.
This is how your example needs to look like in CLI (for clarity I am assuming that G
etResult is not a static function, otherwise you would just call Calculate::GetResult(...)