Mono sysctl on Mac

Is there any way to call sysctl from a Mac based application on a Mac?

+3
source share
1 answer

Of course, just use DllImport, like any other C function. Here is an example:

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);
    }
}

Note that you need to define several overloads for the different data types returned in the second argument (you may need to determine the correct structures as indicated in the headers).

+3
source

All Articles