从 C++/CLI 调用 C# dll 函数

发布于 2024-10-14 18:00:06 字数 1056 浏览 9 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2024-10-21 18:00:06

您必须使用 c++ CLI,否则无法调用 DllImport。
如果是这种情况,您只需引用 c# dll 即可。

在 c++ CLI 中,您可以执行以下操作:

using namespace Your::Namespace::Here;

#using <YourDll.dll>

YourManagedClass^ pInstance = gcnew YourManagedClass();

其中“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);   
}

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:

using namespace Your::Namespace::Here;

#using <YourDll.dll>

YourManagedClass^ pInstance = gcnew YourManagedClass();

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(...)

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);   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文