- PR -

WMIでのIPアドレスの変更がうまくいきません。

1
投稿者投稿内容
リンダオ
会議室デビュー日: 2009/03/10
投稿数: 5
投稿日時: 2009-03-10 12:24
いつもお世話になっております。リンダオと申します。
VB2008を使ってsystem.managementクラスを利用したWindowsVistaマシンのIPアドレス変更のプログラムを作成しているのですが、どうもうまくいきません。

確認している状況は、
@プログラムからIPアドレスの変更を行った後はOKだが、PC再起動後にDefaultGatewayの設定が空欄になってしまう。
(※コントロールパネルのネットワークの設定からと、DOS窓のipconfig /allから確認。
Aプログラムは管理者として実行しているので、実行権限の問題ではないようである。
B下記Functionを呼び出してIPの変更を行っている。

-----------------------------------------------------------------------------------------
'//--IPアドレスを変更する
Function ChangeIp2(ByVal AdapterIndex As Integer, ByVal IpAddr As String, ByVal Subnet As String, _
ByVal Gateway As String, ByVal Metric As UInt16) As Integer

Dim res As Integer
Try

Dim classInstance As New ManagementObject( _
"root\CIMV2", _
"Win32_NetworkAdapterConfiguration.Index='" + CStr(AdapterIndex) + "'", _
Nothing)

' Obtain [in] parameters for the method
Dim inParams As ManagementBaseObject = _
classInstance.GetMethodParameters("EnableStatic")

' Add the input parameters.
inParams("IPAddress") = New String() {IpAddr}
inParams("SubnetMask") = New String() {Subnet}

' Execute the method and obtain the return values.
Dim outParams As ManagementBaseObject = _
classInstance.InvokeMethod("EnableStatic", inParams, Nothing)

'スタティックIPへの変更は成功(=0)
If outParams("ReturnValue") = 0 Then


' Obtain [in] parameters for the method
Dim inParams2 As ManagementBaseObject = _
classInstance.GetMethodParameters("SetGateways")

' Add the input parameters.
inParams2("DefaultIPGateway") = New String() {Gateway}
inParams2("GatewayCostMetric") = New UInt16() {Metric}

' Execute the method and obtain the return values.
Dim outParams2 As ManagementBaseObject = _
classInstance.InvokeMethod("SetGateways", inParams2, Nothing)
res = outParams2("ReturnValue")

Return 0

End If
' List outParams
'Console.WriteLine("Out parameters:")
'Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))

' List outParams
'Console.WriteLine("Out parameters:")
Return 99

'Return "00"
Catch ex As Exception
EventLogWrite(ex.Message)
Return 99
End Try

End Function
-----------------------------------------------------------------------------------------

なにか良い解決策等教えていただけたら助かります。
よろしくお願いします。

1

スキルアップ/キャリアアップ(JOB@IT)