简体   繁体   English

在C#中为Windows设置和获取DHCP选项

[英]Setting and Getting DHCP option for Windows in C#

Using C#, I'm trying to set dhcp option 12 using ManageDHCP.dll . 使用C#,我尝试使用ManageDHCP.dll设置dhcp选项12。 In a nutshell, this is what I'm doing: 简而言之,这就是我在做什么:

var dhcpManager = new DHCPManager(dhcpServerName)
var hostRecord = new HostInfo
               {
                  Name = deviceDhcpName,
                  IPv4 = deviceIpAddress,
                  IsReserved = true,
                  MAC = deviceMacAddress
               };

dhcpManager.SetRecord(dhcpServerScope, hostRecord);

String hostName = deviceDhcpName + "." + dnsZoneName;
var hostnameOpt = new Option
                  {
                     ID = 12,
                     Values = new List<string> { hostName }
                  };
dhcpManager.SetOption(hostnameOpt, dhcpServerScope, deviceIpAddress);

The thing is, is that it just dies at this point and the logs don't record an exception. 事实是,它只是在此时死亡,并且日志没有记录异常。 So I was thinking maybe the format I'm sending it is not correct. 所以我想也许我发送的格式不正确。 So I went to try and get an option. 所以我去尝试了一个选择。 So instead, after creating the reservation, with SetRecord function above - I called this instead, just to try an see if I could get an option to see the correct format: 因此,在创建保留后,使用上面的SetRecord函数-我改为调用此方法,只是尝试看看是否可以选择查看正确的格式:

Option test = dhcpManager.GetOption(3, dhcpServerScope, deviceIpAddress);

Which throws this exception: 引发此异常:

ManageDHCP.DhcpException: DhcpGetOptionInfo at ManageDHCP.DHCPManager._GetOption(OptionTarget target, UInt32 optionID, String subnet, String reservedIP) ManageDHCP.DhcpException:ManageDHCP.DHCPManager._GetOption上的DhcpGetOptionInfo(OptionTarget目标,UInt32 optionID,字符串子网,字符串reservedIP)

I'm new to C# and I'm not really sure what's causing this exception. 我是C#的新手,我不太确定是什么导致了此异常。 Here is a link to the source code for ManageDHCP. 是ManageDHCP的源代码的链接。 What am I doing wrong? 我究竟做错了什么? Is my format incorrect? 我的格式不正确吗?

似乎32位dll的setOption函数不起作用(DhcpGetOptionInfo引发异常),因此我切换到64位dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM