1: [DllImport("iphlpapi.dll", SetLastError = true)]
2: static extern UInt32 AddIPAddress(UInt32 Address, UInt32 IpMask, int IfIndex, out IntPtr NTEContext, out IntPtr NTEInstance);
3:
4: [DllImport("iphlpapi.dll", SetLastError = true)]
5: static extern UInt32 DeleteIPAddress(IntPtr NTEContext);
6:
7: static IntPtr ptrNteContext = new IntPtr(0);
8:
9: public static UInt32 AddIPAddressToInterface(string ipAddress, string subnetMask, int ifIndex)
10: {
11: System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(ipAddress);
12: System.Net.IPAddress subNet = System.Net.IPAddress.Parse(subnetMask);
13: unsafe
14: {
15: int nteContext = 0;
16: int nteInstance = 0;
17: ptrNteContext = new IntPtr(nteContext);
18: IntPtr ptrNteInstance = new IntPtr(nteInstance);
19: return AddIPAddress(IpAddressToUInt32(ipAdd), IpAddressToUInt32(subNet), ifIndex, out ptrNteContext, out ptrNteInstance);
20: }
21: }
22:
23: public static void DeletePreviouslyAddedIP()
24: {
25: DeleteIPAddress(ptrNteContext);
26: }