当然,只需使用 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)
请注意,您应该为第二个参数中返回的不同数据类型定义多个重载(您可能必须定义正确的结构,因为它们在标头中指定)。
归档时间: |
|
查看次数: |
663 次 |
最近记录: |