Macosx Setdns

通过C语言接口在Mac App内部对系统的DNS配置进行修改。

Mac OS X设置DNS代码

示例代码setDNS.c内容如下:

#include <SystemConfiguration/SystemConfiguration.h>

static bool setDNS(CFStringRef *resolvers, CFIndex resolvers_count)
{
    SCDynamicStoreRef ds = SCDynamicStoreCreate(NULL, CFSTR("setDNS"), NULL, NULL);

    CFArrayRef array = CFArrayCreate(NULL, (const void **) resolvers,
            resolvers_count, &kCFTypeArrayCallBacks);

    CFDictionaryRef dict = CFDictionaryCreate(NULL,
            (const void **) (CFStringRef []) { CFSTR("ServerAddresses") },
            (const void **) &array, 1, &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks);    

    CFArrayRef list = SCDynamicStoreCopyKeyList(ds,
            CFSTR("State:/Network/(Service/.+|Global)/DNS"));

    CFIndex i = 0, j = CFArrayGetCount(list);
    if (j <= 0) {
        return FALSE;
    }
    bool ret = TRUE;
    while (i < j) {
        ret &= SCDynamicStoreSetValue(ds, CFArrayGetValueAtIndex(list, i), dict);
        i++;
    }
    return ret;
}

int main(int argc, const char * argv[])
{
    CFStringRef resolvers[] = {
        CFSTR("8.8.8.8"),
        CFSTR("114.114.114.114")
    };
    setDNS(resolvers, (CFIndex) (sizeof resolvers / sizeof resolvers[0]));

    return 0;
}

对应的Makefile文件内容:

#!/usr/bin/make -f
default: setDNS.c
        cc -o setDNS setDNS.c -framework Foundation -framework SystemConfiguration
clean:
        rm setDNS


谢谢各位,欢迎交流并指正。

---- 大茶园丁@戴维营教育

http://io.diveinedu.com

http://www.diveinedu.com

https://github.com/DiveinEdu-CN

时间: 2024-10-08 22:49:12

Macosx Setdns的相关文章

Simulation.Lab.Software.SimLab.Composer.2015.v6.1.MACOSX 1CD

CA Spectrum Linux VM 10.01.00.00.103 Linux 2DVD  Tahoe Design HYDROFLO v3.0.0.4 1CD  CA Spectrum Windows VM 10.01.00.00.103 Win64 2DVD  Delcam Exchange 2016 R2 CR 8.3.1005 Win64 1CD  Delcam PowerSHAPE 2016 SP2 Update only 1CD  ESI Group VA One 2015.0

核心CEI Ensight 10.2.0(c) Win32_64 & Linux32_64 & MacOSX 5DVD

CEI Ensight 10.2.0(c) Win32_64 & Linux32_64 & MacOSX 5DVD Ensight是Computational Engineering International (CEI) 公司的最核心工具,可用于工业.科研.产品设计等领域,处理CFD.FEA等领域的百万甚至上亿的结点单元,具有并行处理与渲染的优势,并支持VR系统环境以及实时协同等功能.QQ:16264558   电话TEL:13963782271 CEI提供几种形式的EnSight: 包

DELPHI XE5轻松输出到MacOsX

配置:MACOSX10.9.3 +XCODE5.1 + VBOX + WINXP + DELPHI XE 5UP2 配置步骤从略. 1.选择firemonkey desktop application 选择HD高清模式. 2.添加OSX平台, 写点代码 放一个BUTTON 添加事件. 还是最俗的HELLO XXXX 加上最傻的SHOWMESSAGE 这里是HELLO XE5 3.运行,在MAC端的下面的文件夹里:project1就是了. 小结:编译出来的MAC应用大小还是可以忍受的.DEBUG模

COMSOL Multiphysics 4.4 Update 1 Multilanguage Windows.&.Linux.&.MacOSX 1CD

GH BLAEDE 风力机性能和载荷计算的集成化软件包 用户界面直观 提供综合模型 空气动力模型 控制系统应用 动力响应等 多种用途 ProgeCAD.2013.Professional.v13.0.16.21 1CD PROKON.v2.6.14 1CD  IBM.SPSS.Amos.v22 1CD IBM.SPSS.Data.Collection.v7.Win32 1CD IBM.SPSS.Data.Collection.v7.Win64 1CD IBM.SPSS.Statistics.v2

[MacOSX]

When i run svn up in the folder, i got these error: svn: Error converting entry in directory 'XXXXX' to UTF-8 svn: Can't convert string from native encoding to 'UTF-8': After googled, i found the resolution, just run export LC_ALL=zh_CN.UTF-8 Have fu

Adobe Photoshop Lightroom v5.4 MacOSX 专业摄影师图像处理软件

AirRadar 2.3.3 MacOSX Viscosity 1.4.8 MacOSX Iridient Developer 2.3.4 Mac OS X SideFX Houdini FX 13.0.376 Win/Mac/Linux Vinotekasoft Vinoteka 3.3.5 Multilingual MacOSX Firetask 3.6.1 Multilingual MacOSX iBackup Viewer Pro 2.70 MacOSX DivX Plus Pro 10

NextLimit.Maxwell.Render.for.SketchUp.v3.2.5.MacOSX 1CD

Global.Mapper.v17.1.0.b020216.Win32_64 2CD  Siemens.LMS.Virtual.Lab.Rev13.5.Win64 1DVD  AutoDesSys formZ Pro v8.5.3 Win32_64 2CD  CSI.SAP2000.v18.1.0.1227.Win32_64 2CD  FilmLight.Daylight.v4.4m1.8005.MacOSX 1CD  The Foundry Modo v902 SP2 Linux64 1CD

Mathematica v10.0 Winsows & Linux & MacOSX 3DVD

Carlson.Civil.Suite.2015.build.140721 1DVD Agilent GoldenGate RFIC Simulation Software 2013.10 Linux64 1DVD Agilent.Genesys.v2014.04.Win64 1DVD Altair.HyperWorks.Solvers.v12.0.220.Update.Only.Win32_64.&.Linux64 3DVD Altair.HyperWorks.Solvers.v12.0.22

11月30号 MacOSX安装

以版本10.11.1 El Capitan为例 步骤1  制作MacOSX安装U盘 步骤2  重启,按住Option(Alt)按键,选择从U盘启动 步骤3  启动磁盘工具,格式化目标硬盘 步骤4  安装MacOSX 制作MacOSX安装U盘 *插入U盘 *运行磁盘管理器,格式化U盘卷标,引导格式 GUID *运行终端 *在终端里运行如下命令: sudo     /Applications/Install\ OS\  X\  El\ Capitan.app/Contents/Resources/c