Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WMI or How to change my IP address

142 views
Skip to first unread message

Marco Scheel

unread,
May 21, 2002, 5:45:53 AM5/21/02
to
hi ng,

i'm looking for a way to change the local ip address auf my notebook. i'd
like to write some code that can change my configuration from static ip to
an dhcp configuration. looked at System.Managment, but i've got no idea how
this wmi thing works.

maybe you can help?

thanx marco


Marco Scheel

unread,
May 21, 2002, 8:55:08 PM5/21/02
to

Marco Scheel

unread,
May 21, 2002, 10:38:27 PM5/21/02
to

NETMaster

unread,
May 22, 2002, 5:07:43 AM5/22/02
to
If you like, low-level IP-Helper API:
http://msdn.microsoft.com/library/en-us/tcpip/iphpport_7vz9.asp

PInvoke with C# (no IP change impl.?)
http://www.gotdotnet.com/team/p2p/
http://www.gotdotnet.com/userfiles/herveyw/netsamples.zip


WMI:
Win32_NetworkAdapterConfiguration:
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_6oq6.asp

Methods: EnableStatic + EnableDHCP
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_0ujy.asp
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_27u6.asp

Changeip VB-Scripts:
http://desktopengineer.com/index.php?topic=0080WMI
http://cwashington.netreach.net/depo/view.asp?Index=628

VERY simplified WMI sample:
// ===============================================================================
using System;
using System.Management;
using System.Threading;

namespace WmiIpChanger
{
class IpChanger
{
[MTAThread]
static void Main(string[] args)
{
ReportIP();
// SwitchToDHCP();
SwitchToStatic();
Thread.Sleep( 5000 );
ReportIP();
Console.WriteLine( "end." );
}


static void SwitchToDHCP()
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
if( ! (bool) mo["IPEnabled"] )
continue;

inPar = mo.GetMethodParameters("EnableDHCP");
outPar = mo.InvokeMethod( "EnableDHCP", inPar, null );
break;
}
}


static void SwitchToStatic()
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
if( ! (bool) mo[ "IPEnabled" ] )
continue;

inPar = mo.GetMethodParameters( "EnableStatic" );
inPar["IPAddress"] = new string[] { "192.168.1.1" };
inPar["SubnetMask"] = new string[] { "255.255.255.0" };
outPar = mo.InvokeMethod( "EnableStatic", inPar, null );
break;
}
}

static void ReportIP()
{
Console.WriteLine( "****** Current IP addresses:" );
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
if( ! (bool) mo[ "IPEnabled" ] )
continue;

Console.WriteLine( "{0}\n SVC: '{1}' MAC: [{2}]", (string) mo["Caption"],
(string) mo["ServiceName"], (string) mo["MACAddress"] );

string[] addresses = (string[]) mo[ "IPAddress" ];
string[] subnets = (string[]) mo[ "IPSubnet" ];

Console.WriteLine( " Addresses :" );
foreach(string sad in addresses)
Console.WriteLine( "\t'{0}'", sad );

Console.WriteLine( " Subnets :" );
foreach(string sub in subnets )
Console.WriteLine( "\t'{0}'", sub );
}
}
}
}

// ===============================================================================

WARNING: do MUCH more error checking, multiple NIC tests, timing!...
use all at at your own risk!

--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html


"Marco Scheel" <atW...@Visual-eVolution.de> wrote in message news:14765441.1022081866849.JavaMail.SYSTEM@oscar...

Max[MS]

unread,
May 23, 2002, 10:50:41 PM5/23/02
to
Hello,

I found a sample at
http://cwashington.netreach.net/depo/view.asp?Index=628&ScriptType=vbscript.
Though it is VBScript, you may get hints from it.

Hope this helps.

Regards,
Max
==========================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Message-ID: <10168913.1022075668347.JavaMail.SYSTEM@oscar>
From: Marco Scheel <atW...@Visual-eVolution.de>
Subject: WMI or How to change my IP address

NETMaster

unread,
May 24, 2002, 7:39:13 AM5/24/02
to
[repost]

WMI:
Win32_NetworkAdapterConfiguration:
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_6oq6.asp

// ===============================================================================


"Max[MS]" <m...@online.microsoft.com> wrote in message news:L4AE#2sACHA.2628@cpmsftngxa08...

0 new messages