gpt4 book ai didi

macos - Mac 上 Mono 的 sysctl

转载 作者:行者123 更新时间:2023-12-02 00:22:05 24 4
gpt4 key购买 nike

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

最佳答案

当然,只需使用 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);
}
}

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

关于macos - Mac 上 Mono 的 sysctl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10483363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com