C++/CLI - 调用 DLL 中定义的非托管函数

发布于 2025-01-04 00:59:06 字数 1327 浏览 0 评论 0原文

我有一个 DLL,其中包含带有方法的非托管类。我正在尝试从 C++/CLI 调用这些方法。

我的阅读使我发现 P/Invoke 用于执行此操作。但是,我似乎无法找出如何使其发挥作用。

在 DLL 中,定义了以下类:

//Header:
namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        static __declspec(dllexport) double Add(double a, double b);
        static __declspec(dllexport) double Subtract(double a, double b);
        static __declspec(dllexport) double Multiply(double a, double b);
        static __declspec(dllexport) double Divide(double a, double b);
    };
}

//Source:
namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b) {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b) {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b) {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b) {
        if (b == 0) {
            throw new invalid_argument("b cannot be zero!");
        }
        return a / b;
    }
}

根据我的阅读,以下内容应该在我的 C++/CLI 代码中声明一个方法,然后我可以在我的代码中调用该方法:

class MyMathFuncs {
public:
[DllImport("TestDLL.dll")]
    static double Add(double a, double b);
};

其中“TestDLL.dll”是我的 DLL。

我得到的错误是在运行时。它显示“无法在 DLL 'TestDLL.dll' 中找到名为 'Add' 的入口点”。

我做错了什么?我已经阅读了 MSDN 上有关此的文章,但我不明白。

预先感谢您的帮助!

I have a DLL which contains unmanaged classes with methods. I'm trying to call those methods from C++/CLI.

My reading has lead me to find that P/Invoke is used to do this. However, I can't seem to find out how to make it work.

In the DLL, the following class is defined:

//Header:
namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        static __declspec(dllexport) double Add(double a, double b);
        static __declspec(dllexport) double Subtract(double a, double b);
        static __declspec(dllexport) double Multiply(double a, double b);
        static __declspec(dllexport) double Divide(double a, double b);
    };
}

//Source:
namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b) {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b) {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b) {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b) {
        if (b == 0) {
            throw new invalid_argument("b cannot be zero!");
        }
        return a / b;
    }
}

From what I've read, the following should declare a method in my C++/CLI code, which I can then call in my code:

class MyMathFuncs {
public:
[DllImport("TestDLL.dll")]
    static double Add(double a, double b);
};

Where "TestDLL.dll" is my DLL.

The error I get is at runtime. It says "Unable to find an entry point named 'Add' in DLL 'TestDLL.dll'".

What am I doing wrong? I've read through the MSDN articles on this, but I don't understand it.

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

迎风吟唱 2025-01-11 00:59:06

解决了。我需要在 DllImport 属性中包含 DLL 中函数的 EntryPoint。然而,MSDN 文档建议 EntryPoint 可以是 DLL 中的函数名称,但这对我不起作用 - 我必须声明函数的序号。谁能告诉我这是为什么吗?

Solved. I need to include the EntryPoint of the function in the DLL in the DllImport attribute. However, the MSDN documentation suggests that the EntryPoint can be the function name in the DLL but this doesn't work for me - I have to state the ordinal number of the function. Can anyone let me know why this is?

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