Setting default gateway

G

google_groups3

Below is a snippet of code I am using. I want to set the IP, subnet
and default gateway of my local computer via code. However, when I do
it, I get a "not found" (error number 5) error on the default gateway
line.

Dim GateArray() As String = {"99.99.99.1"} 'Array

Dim inPar As ManagementBaseObject = Nothing
Dim outPar As ManagementBaseObject = Nothing
Dim blah As ManagementBaseObject()

inPar = mo.GetMethodParameters("EnableStatic")
inPar("IPAddress") = New String() {"99.99.99.2"}
inPar("SubnetMask") = New String() {"255.255.255.0"}
inPar("DefaultIPGateway") = LBound(GateArray) 'New String()
{"99.99.99.1"}

outPar = mo.InvokeMethod("EnableStatic", inPar, Nothing)

Any ideas what is wrong? I read that the default gateway is an array
so I am trying to find it the value that way but I get the same error
if I just use a string.

Thanks
 
M

Mr Newbie

Correct me if I am wrong, but is are the number here not supposed to be Base
8 ( Where 99 would be illegal ? )
 
M

Mr Newbie

OK, silly question here. Does this IP address exists ?, if not are you
suprised at the error ?
 
G

google_groups3

Thanks for responding.

That won't matter. When setting a static IP you can add whatever you
want. As far as I can tell this has to be a problem with the code.
 
M

Mr Newbie

what does this method do and is this the actual line which returns the
error?

inPar("DefaultIPGateway") = LBound(GateArray) 'New String()
 
G

google_groups3

Mr said:
what does this method do and is this the actual line which returns the
error?

inPar("DefaultIPGateway") = LBound(GateArray) 'New String()

This line changes (or should) the default gateway. This is where i get
the "not found" error. If i comment out this line then my IP and
subnet mask is changed by the code. So, either "DefaultIPGateway" is
incorrect, or something is wrong elsewhere.

Now, using the following code, i can expose all the property names:

For Each moa As ManagementBaseObject In objCollection
MsgBox(moa.GetText(TextFormat.Mof))
Next

And, DefaultIPGateway is one of them. So I have no idea what is wrong.
Has anyone else beenable to change their gateway via code?????
 
M

Mr Newbie

Well, have you tried setting the IP address to 127.0.0.0 ( Your local
machine ) just to see if it is looking for a valip IP address ??
 
G

google_groups3

This works. My code only works if I comment out the Default Gateway
line. As far as I can tell, this is a big in the WMI/VB connection.
So I guess it is just not going to be possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top