如何在 C++ 中创建 dll 以在 C# 中使用

How to create dll in C++ for using in C#(如何在 C++ 中创建 dll 以在 C# 中使用)
本文介绍了如何在 C++ 中创建 dll 以在 C# 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题要问你.

I've a little question to ask you.

我有一个 C++ 源代码和一个头文件.C++文件使用windows.h库,使用串口进行操作(基本操作:read(), write()等).

I have one C++ source and one header files. The C++ file uses windows.h library, makes operations using serial port(basic operations: read(), write() etc.).

我想要做的是,使用这些文件创建一个库,并在我的 C#.Net 解决方案中使用该库.

What I want to do is, creating a library using these files, and use that library in my C#.Net solution.

我需要创建什么类型的库?我该怎么做?创建库后,如何将其导入 C# 解决方案?

What type of library I need to create? How can I do it? After creating library, How can I import it to C# solution?

我最诚挚的问候.

我正在使用的代码部分:

// MathFuncsDll.h

namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}

// MathFuncsDll.cpp
// compile with: /EHsc /LD

#include "MathFuncsDll.h"

#include <stdexcept>

using namespace std;

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# 导入部分:

[DllImport("SimpleDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern double Add(double a, double b);

static void Main(string[] args)
{
    string a = Add(1.0, 3.0));
}

推荐答案

评论了几句,这里试试:

After several comments, here a try:

C++ 代码 (DLL),例如.math.cpp,编译成 HighSpeedMath.dll:

C++ Code (DLL), eg. math.cpp, compiled to HighSpeedMath.dll:

extern "C"
{
    __declspec(dllexport) int __stdcall math_add(int a, int b)
    {
        return a + b;
    }
}

C# 代码,例如.程序.cs:

C# Code, eg. Program.cs:

namespace HighSpeedMathTest
{
    using System.Runtime.InteropServices;

    class Program
    {
        [DllImport("HighSpeedMath.dll", EntryPoint="math_add", CallingConvention=CallingConvention.StdCall)]
        static extern int Add(int a, int b);

        static void Main(string[] args)
        {
            int result = Add(27, 28);
        }
    }
}

当然,如果入口点已经匹配,则不必指定它.调用约定也一样.

Of course, if the entry point matches already you don't have to specify it. The same with the calling convention.

正如评论中提到的,DLL 必须提供 C 接口.这意味着,外部C",没有例外,没有引用等.

As mentioned in the comments, the DLL has to provide a C-interface. That means, extern "C", no exceptions, no references etc.

如果您有 DLL 的头文件和源文件,它可能如下所示:

If you have a header and a source file for your DLL, it could look like this:

数学.hpp

#ifndef MATH_HPP
#define MATH_HPP

extern "C"
{
    __declspec(dllexport) int __stdcall math_add(int a, int b);
}

#endif

数学.cpp

#include "math.hpp"

int __stdcall math_add(int a, int b)
{
    return a + b;
}

这篇关于如何在 C++ 中创建 dll 以在 C# 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)