我可以在 ASP MVC 应用程序中添加 C++ DLL
Can I add C++ DLL in a ASP MVC application
我正在尝试在 ASP.Net MVC 5 应用程序中添加对使用 C++ 构建的 DLL 的引用。
我想从应用程序调用 DLL 中的函数。
是的,你可以。这里的关键是"platform invoke".
- 您需要创建一个 class 保存 DLL 方法(示例中的 Win32)
- 您需要定义 prototypes/signatures 的 DLL 方法,使用 DllImport 注释
- 现在你可以调用那些方法了
此示例取自 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);
}
}
是的,任何本机或非托管 DLL 都可以与 ASP MVC 应用程序一起使用。 构建应该针对相同的平台。如果为 x64 平台构建了 C++ DLL,那么导入它的应用程序 (web/console) 也应该为相同的配置构建。
有关使用 DllImport 导入的信息,请参阅@bdongus 的 post。此外,服务器应 运行 在同一平台上。
我正在尝试在 ASP.Net MVC 5 应用程序中添加对使用 C++ 构建的 DLL 的引用。 我想从应用程序调用 DLL 中的函数。
是的,你可以。这里的关键是"platform invoke".
- 您需要创建一个 class 保存 DLL 方法(示例中的 Win32)
- 您需要定义 prototypes/signatures 的 DLL 方法,使用 DllImport 注释
- 现在你可以调用那些方法了
此示例取自 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);
}
}
是的,任何本机或非托管 DLL 都可以与 ASP MVC 应用程序一起使用。 构建应该针对相同的平台。如果为 x64 平台构建了 C++ DLL,那么导入它的应用程序 (web/console) 也应该为相同的配置构建。
有关使用 DllImport 导入的信息,请参阅@bdongus 的 post。此外,服务器应 运行 在同一平台上。