Is it possible to somehow call sysctl from Mono-based app on Mac?
Asked
Active
Viewed 651 times
1 Answers
5
Sure, just use DllImport, like with any other C function. Here is a sample:
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 should define multiple overloads for different data types returned in the second argument (you may have to define the proper structs, as they are specified in the headers).

lupus
- 3,963
- 1
- 18
- 13
-
Thanks. Is there any Mono/Mac interop directory similar to http://pinvoke.net? It's quite difficult to figure out correct type marshaling... – ivan May 08 '12 at 12:22
-
I don't know of any such directory. You can read about DllImport and P/Invoke on msdn and we have a page about some specifics for mono on http://www.mono-project.com/Interop_with_Native_Libraries. – lupus May 09 '12 at 07:29