Bog*_*vic 12 c# dll interop c++-cli dllimport
我有一个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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我打算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);
Run Code Online (Sandbox Code Playgroud)
这是函数,其中称为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);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:"语法错误:标识符'计算'".有人可以帮我解决这个可怕的错误吗?
san*_*oIT 22
您必须使用c ++ CLI,否则无法调用DllImport.如果是这种情况,您可以只引用c#dll.
在c ++ CLI中,您可以执行以下操作:
using namespace Your::Namespace::Here;
#using <YourDll.dll>
YourManagedClass^ pInstance = gcnew YourManagedClass();
Run Code Online (Sandbox Code Playgroud)
其中'YourManagedClass'是在c#项目中使用输出程序集'YourDll.dll'定义的.
**编辑**添加了您的示例.
这是你的例子在CLI中需要的样子(为清楚起见我假设G etResult不是静态函数,否则你只需要调用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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16055 次 |
| 最近记录: |