C++互操作(隐式 PInvoke)的限制
Limitations of C++ Interop (implicit PInvoke)
我想在 C# 中使用一些本机(非托管(C++。我发现我有两个选择,显式和C++互操作(隐式 pinvoke(。
Microsoft的文档建议隐式而不是显式。但是关于我能做什么和不能做什么的信息并不多。这在2014年说,隐式不适用于Mono,因此不适用于Linux系统。但是今天是什么情况呢?
我想从我的C#应用程序在任何平台(win,mac,linux,android,ios(上使用C++库。它是一个用 C++14 编写的仅标头模板库。我想知道我是否可以使用推荐的C++互操作来执行此操作,或者我是否必须编写 C 样式 API 并为此使用显式 PInvoke。
此致敬意
托尔斯滕
查看 Unity的原生 dll 文件文档,它们表明在使用 Unity 执行 P/调用时应该使用显式互操作。以下是文档中的示例:
using UnityEngine;
using System.Runtime.InteropServices;
class SomeScript : MonoBehaviour {
#if UNITY_IPHONE
// On iOS plugins are statically linked into
// the executable, so we have to use __Internal as the
// library name.
[DllImport ("__Internal")]
#else
// Other platforms load plugins dynamically, so pass the name
// of the plugin's dynamic library.
[DllImport ("PluginName")]
#endif
private static extern float FooPluginFunction ();
void Awake () {
// Calls the FooPluginFunction inside the plugin
// And prints 5 to the console
print (FooPluginFunction ());
}
}
请注意,您无法使用单个 c++ dll 执行要执行的所有平台。您需要为每个平台编译一个 dll,您可以将文件放在单独的子文件夹中,以便所有 dll 文件都可以使用相同的名称,从而使 C# 代码更容易。然后,您可以使用插件检查器来设置在运行时应使用哪个平台的 dll。
相关文章:
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 编译为 wasm 的 C++ 和 Rust 程序可以以某种方式互操作吗?
- 有什么语言可以与C++互操作吗
- 使 Flutter C/C++ 互操作示例正常工作
- C++互操作(隐式 PInvoke)的限制
- COM 互操作结构定义与内存布局不匹配
- _Complex 和 std::complex 之间的 C/C++ 互操作
- C# COM 服务器互操作 - System.InvalidCastException.
- 从C++与 C 灵活阵列成员互操作的正确方法是什么?
- 如何通过 COM 互操作访问复杂对象的属性?
- 如何区分"QObject"的亲子关系关注点,例如与其他所有者进行互操作?
- DirectWrite GDI 互操作:使用"IDWriteFontFace"绘制文本的简单方法
- opencl openGL互操作内核参数用于读取 写入
- Rust 互操作与 C++ std::string
- 将互操作 HwndSource 包装在 c++/cli 中,以用于本机 c++/MFC 应用程序
- 跨 C# 和 COM 互操作使用对象C++
- 尝试为c++互操作自动生成库时出错
- 在MS Visual Studio 2010上使用带有Cuda互操作的OpenGL缓冲区时出现异常错误
- WinRT 组件内C++类之间的互操作
- 从c#到c++的互操作传递结构