「ローカルネットワークの接続 有効・無効」の編集履歴(バックアップ)一覧はこちら

ローカルネットワークの接続 有効・無効」(2008/06/22 (日) 13:11:18) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; public class NetWorkUtil { #region プロパティ private static Guid GUID_DEVCLASS_NET = new Guid(0x4d36e972, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18); private const int DIGCF_PRESENT = 2; private const int DIF_PROPERTYCHANGE = 0x00000012; private const int DICS_FLAG_GLOBAL = 0x00000001; private const int INVALID_HANDLE_VALUE = -1; private const int DICS_ENABLE = 0x00000001; private const int DICS_DISABLE = 0x00000002; private const int DICS_FLAG_CONFIGSPECIFIC = 0x00000002; [StructLayout(LayoutKind.Sequential)] private struct SP_DEVINFO_DATA { public int cbSize; public Guid ClassGuid; public int DevInst; public int Reserved; } [StructLayout(LayoutKind.Sequential)] private struct SP_CLASSINSTALL_HEADER { public int cbSize; public int InstallFunction; } [StructLayout(LayoutKind.Sequential)] private struct SP_PROPCHANGE_PARAMS { public SP_CLASSINSTALL_HEADER ClassInstallHeader; public int StateChange; public int Scope; public int HwProfile; public void Init() { ClassInstallHeader = new SP_CLASSINSTALL_HEADER(); } } #endregion #region SetUp API [DllImport("setupapi.dll")] private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hWndParent, int Flags); [DllImport("setupapi.dll")] private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int Supplies, ref SP_DEVINFO_DATA DeviceInfoData); [DllImport("setupapi.dll")] private static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); [DllImport("setupapi.dll")] private static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_CLASSINSTALL_HEADER ClassInstallParams, int ClassInstallParamsSize); [DllImport("setupapi.dll")] private static extern bool SetupDiCallClassInstaller(int InstallFunction, IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData); #endregion private static IntPtr hDevInfo = (IntPtr)0; private static bool StateChange(int NewState, int SelectedItem, IntPtr hDevInfo) { SP_PROPCHANGE_PARAMS PropChangeParams; SP_DEVINFO_DATA DeviceInfoData; PropChangeParams = new SP_PROPCHANGE_PARAMS(); PropChangeParams.Init(); DeviceInfoData = new SP_DEVINFO_DATA(); PropChangeParams.ClassInstallHeader.cbSize = Marshal.SizeOf(PropChangeParams.ClassInstallHeader); DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); if (!SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, ref DeviceInfoData)) return false; PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; PropChangeParams.Scope = DICS_FLAG_GLOBAL; PropChangeParams.StateChange = NewState; if (!SetupDiSetClassInstallParams(hDevInfo, ref DeviceInfoData, ref PropChangeParams.ClassInstallHeader, Marshal.SizeOf(PropChangeParams))) return false; if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, ref DeviceInfoData)) return false; PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; PropChangeParams.Scope = DICS_FLAG_CONFIGSPECIFIC; PropChangeParams.StateChange = NewState; if (!SetupDiSetClassInstallParams(hDevInfo, ref DeviceInfoData, ref PropChangeParams.ClassInstallHeader, Marshal.SizeOf(PropChangeParams))) return false; if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, ref DeviceInfoData)) return false; return true; } /// <summary> /// ネットワークを無効にします。 /// </summary> /// <returns>bool:処理の成否</returns> public static bool DisableNetAdapter() { IntPtr hdi; bool res = true; hdi = SetupDiGetClassDevs(ref GUID_DEVCLASS_NET, (IntPtr)0, (IntPtr)0, DIGCF_PRESENT); if (hdi == (IntPtr)INVALID_HANDLE_VALUE) return false; res = StateChange(DICS_DISABLE, 0, hdi); SetupDiDestroyDeviceInfoList(hdi); System.Threading.Thread.Sleep(500); return res; } /// <summary> /// <para>ネットワーク接続を有効にします</para> /// </summary> /// <returns>bool:処理の成否</returns> public static bool EnableNetAdapter() { IntPtr hdi; Boolean res = true; hdi = SetupDiGetClassDevs(ref GUID_DEVCLASS_NET, (IntPtr)0, (IntPtr)0, DIGCF_PRESENT); if (hdi == (IntPtr)INVALID_HANDLE_VALUE) return false; res = StateChange(DICS_ENABLE, 0, hdi); SetupDiDestroyDeviceInfoList(hdi); System.Threading.Thread.Sleep(500); return res; } }

表示オプション

横に並べて表示:
変化行の前後のみ表示:
目安箱バナー