【问题标题】:Can I add C++ DLL in a ASP MVC application我可以在 ASP MVC 应用程序中添加 C++ DLL
【发布时间】:2020-04-15 03:36:51
【问题描述】:

我正在尝试在 ASP.Net MVC 5 应用程序中添加对使用 C++ 构建的 DLL 的引用。 我想从应用程序调用 DLL 中的函数。

【问题讨论】:

    标签: c++ asp.net-mvc dll dllimport


    【解决方案1】:

    是的,你可以。这里的关键是“平台调用”。

    1. 您需要创建一个包含 DLL 方法的类(示例中为 Win32)
    2. 您需要使用 DllImport 注释定义 DLL 方法的原型/签名
    3. 现在您可以调用这些方法

    此示例取自 Microsoft documentation

    using System;
    using System.Runtime.InteropServices;
    
    public class Win32 {
         [DllImport("user32.dll", CharSet=CharSet.Auto)]
         public static extern IntPtr MessageBox(int hWnd, String text, 
                         String caption, uint type);
    }
    
    public class HelloWorld {
        public static void Main() {
           Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0);
        }
    }
    

    【讨论】:

    • 非常感谢!我曾尝试导入为 x64 平台构建的 DLL,并将其与在调试模式下构建的 MVC 应用程序和用于平台的 Any CPU 一起使用。所以我遇到了这个问题。现在已经解决了。
    【解决方案2】:

    是的,任何本机或非托管 DLL 都可以与 ASP MVC 应用程序一起使用。 构建应针对同一平台。如果已经为 x64 平台构建了 C++ DLL,那么导入它的应用程序(web/console)也应该为相同的配置构建。 有关使用 DllImport 导入的信息,请参阅 @bdongus 的帖子。服务器也应该在同一平台上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多