<small id='WFdHc'></small><noframes id='WFdHc'>

<i id='WFdHc'><tr id='WFdHc'><dt id='WFdHc'><q id='WFdHc'><span id='WFdHc'><b id='WFdHc'><form id='WFdHc'><ins id='WFdHc'></ins><ul id='WFdHc'></ul><sub id='WFdHc'></sub></form><legend id='WFdHc'></legend><bdo id='WFdHc'><pre id='WFdHc'><center id='WFdHc'></center></pre></bdo></b><th id='WFdHc'></th></span></q></dt></tr></i><div id='WFdHc'><tfoot id='WFdHc'></tfoot><dl id='WFdHc'><fieldset id='WFdHc'></fieldset></dl></div>

    1. <tfoot id='WFdHc'></tfoot>

      <legend id='WFdHc'><style id='WFdHc'><dir id='WFdHc'><q id='WFdHc'></q></dir></style></legend>
      • <bdo id='WFdHc'></bdo><ul id='WFdHc'></ul>

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

        时间:2023-11-10
            • <bdo id='Sm7ll'></bdo><ul id='Sm7ll'></ul>

                <tbody id='Sm7ll'></tbody>
              <legend id='Sm7ll'><style id='Sm7ll'><dir id='Sm7ll'><q id='Sm7ll'></q></dir></style></legend>
              <i id='Sm7ll'><tr id='Sm7ll'><dt id='Sm7ll'><q id='Sm7ll'><span id='Sm7ll'><b id='Sm7ll'><form id='Sm7ll'><ins id='Sm7ll'></ins><ul id='Sm7ll'></ul><sub id='Sm7ll'></sub></form><legend id='Sm7ll'></legend><bdo id='Sm7ll'><pre id='Sm7ll'><center id='Sm7ll'></center></pre></bdo></b><th id='Sm7ll'></th></span></q></dt></tr></i><div id='Sm7ll'><tfoot id='Sm7ll'></tfoot><dl id='Sm7ll'><fieldset id='Sm7ll'></fieldset></dl></div>
                1. <tfoot id='Sm7ll'></tfoot>

                  <small id='Sm7ll'></small><noframes id='Sm7ll'>

                  本文介绍了如何在 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# 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 .NET/C# 进行健壮的 SerialPort 编程? 下一篇:System.IO.Ports.SerialPort 和多线程

                  相关文章

                    <bdo id='3I6Gs'></bdo><ul id='3I6Gs'></ul>

                  <tfoot id='3I6Gs'></tfoot>

                  <small id='3I6Gs'></small><noframes id='3I6Gs'>

                    1. <legend id='3I6Gs'><style id='3I6Gs'><dir id='3I6Gs'><q id='3I6Gs'></q></dir></style></legend>

                    2. <i id='3I6Gs'><tr id='3I6Gs'><dt id='3I6Gs'><q id='3I6Gs'><span id='3I6Gs'><b id='3I6Gs'><form id='3I6Gs'><ins id='3I6Gs'></ins><ul id='3I6Gs'></ul><sub id='3I6Gs'></sub></form><legend id='3I6Gs'></legend><bdo id='3I6Gs'><pre id='3I6Gs'><center id='3I6Gs'></center></pre></bdo></b><th id='3I6Gs'></th></span></q></dt></tr></i><div id='3I6Gs'><tfoot id='3I6Gs'></tfoot><dl id='3I6Gs'><fieldset id='3I6Gs'></fieldset></dl></div>