Mac 上 Mono 的 sysctl

iva*_*van 4 macos mono

是否有可能以某种方式从 Mac 上基于 Mono 的应用程序调用sysctl ?

lup*_*pus 5

当然,只需使用 DllImport,就像使用任何其他 C 函数一样。这是一个示例:

using System;
using System.Runtime.InteropServices;

class TestSysctl {

    [DllImport ("libc")]
    static extern int sysctlbyname (string name, out int int_val, ref IntPtr length, IntPtr newp, IntPtr newlen);

    static void Main (string[] args) {
            int value;
            IntPtr size = (IntPtr)4;
            string param = "kern.maxproc";
            if (args.Length > 0)
                    param = args [0];
            int res = sysctlbyname (param, out value, ref size, IntPtr.Zero, (IntPtr)0);
            Console.WriteLine ("{0}: {1} {2} (res: {3})", param, value, size, res);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您应该为第二个参数中返回的不同数据类型定义多个重载(您可能必须定义正确的结构,因为它们在标头中指定)。