如何从 Mono(命令行)应用程序访问本机 API?
How to access native APIs from Mono (command-line) app?
是否可以(如果可以,如何)从普通命令行 Mono 应用程序调用外部 API?
例如:我已经通过 this page 在我的 Mac 上安装了 Mono,并且一直在愉快地编写命令行应用程序,但现在想访问一些简单的 CoreFoundation API。我真的不想为此切换到 Xamarin.Mac。这可能吗?
作为一个密切相关的问题,我可以创建自己的共享库来公开 C API,并以某种方式从我的 Mono 应用程序中调用它吗?
Mono 文档涵盖 P/Invoke 此处:http://www.mono-project.com/docs/advanced/pinvoke/
引用:
The Common Language Infrastructure (CLI) is designed to make it “easy” to interoperate with existing code. In principle, all you need to do is create a DllImport function declaration for the existing code to invoke, and the runtime will handle the rest. For example:
[DllImport ("libc.so")]
private static extern int getpid ();
因为你特地询问了Mac:
Note: Mono uses GLib to load libraries, and GLib has a bug on Mac OS X where it doesn’t use a .dylib extension, but instead uses the Unix .so extension. While this should eventually be fixed, the current workaround is to write a .config file which maps to the .dylib file, e.g. [and so on ...]
是否可以(如果可以,如何)从普通命令行 Mono 应用程序调用外部 API?
例如:我已经通过 this page 在我的 Mac 上安装了 Mono,并且一直在愉快地编写命令行应用程序,但现在想访问一些简单的 CoreFoundation API。我真的不想为此切换到 Xamarin.Mac。这可能吗?
作为一个密切相关的问题,我可以创建自己的共享库来公开 C API,并以某种方式从我的 Mono 应用程序中调用它吗?
Mono 文档涵盖 P/Invoke 此处:http://www.mono-project.com/docs/advanced/pinvoke/
引用:
The Common Language Infrastructure (CLI) is designed to make it “easy” to interoperate with existing code. In principle, all you need to do is create a DllImport function declaration for the existing code to invoke, and the runtime will handle the rest. For example:
[DllImport ("libc.so")] private static extern int getpid ();
因为你特地询问了Mac:
Note: Mono uses GLib to load libraries, and GLib has a bug on Mac OS X where it doesn’t use a .dylib extension, but instead uses the Unix .so extension. While this should eventually be fixed, the current workaround is to write a .config file which maps to the .dylib file, e.g. [and so on ...]