Skip to content

Commit

Permalink
Merge pull request #6 from 223n/feature/issue_3
Browse files Browse the repository at this point in the history
create First Enable DHCP Option.
  • Loading branch information
223n authored Dec 16, 2017
2 parents a3e644b + d855e56 commit 6d4a549
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/WinIpChanger/WinIPChargerDesktop/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void Click_UpdateToolStripMenuItem(object sender, EventArgs args)
};
foreach (var dns in setting.DnsServers)
adapter.DnsServers.Add(IPAddress.Parse(dns));
var result = Network.NetworkAdapterUtility.SetAdapterConfig(adapter);
var result = Network.NetworkAdapterUtility.SetAdapterConfig(adapter, false);
switch (result)
{
case Network.Results.Success:
Expand Down
19 changes: 11 additions & 8 deletions src/WinIpChanger/WinIpChanger/Network/NetworkAdapterUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class NetworkAdapterUtility
/// <summary>
/// WMI APIが最後に返却したエラーコード
/// </summary>
[CLSCompliantAttribute(false)]
[CLSCompliant(false)]
public static uint LastApiErrorCode { get; set; } = 0;

/// <summary>
Expand All @@ -41,9 +41,10 @@ public static Collection<NetworkAdapter> GetNetworkAdaptersForIPEnabled()
/// ネットワークアダプタの設定を反映します。
/// </summary>
/// <param name="value">ネットワークアダプタの設定</param>
/// <param name="firstEnableDHCP">最初にDHCPの有効化を実行するかどうか</param>
/// <returns>設定結果</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static Results SetAdapterConfig(NetworkAdapter value)
public static Results SetAdapterConfig(NetworkAdapter value, bool firstEnableDHCP)
{
if (value == null) throw new ArgumentNullException("value");
uint apiResult = 0;
Expand All @@ -58,14 +59,16 @@ public static Results SetAdapterConfig(NetworkAdapter value)
{
isFound = true;
if (!(bool)config["IPEnabled"]) return Results.TargetIsNotEnableIPError;
// Enable DHCP
apiResult |= (uint)config.InvokeMethod("EnableDHCP", null);
if (apiResult != 0 && apiResult != 1)
// Static
if (value.IsDhcpEnabled || firstEnableDHCP)
{
LastApiErrorCode = apiResult;
return Results.ApiFailedError;
apiResult |= (uint)config.InvokeMethod("EnableDHCP", null);
if (apiResult != 0 && apiResult != 1)
{
LastApiErrorCode = apiResult;
return Results.ApiFailedError;
}
}
// Static
if (!value.IsDhcpEnabled)
{
apiResult |= (uint)config.InvokeMethod("EnableStatic", new object[] { value.GetIPAddressStringArray(), value.GetSubnetMaskStringArray() });
Expand Down

0 comments on commit 6d4a549

Please sign in to comment.