| 分類:[VB.NET/VB2005 以降]
以前にWMIを使用した固定IPの変更する処理を作成したのですが、今回Windows8の端末が導入され同処理では固定IPの変更ができなくなりました。 WMIでコンピュータ名やIPアドレスの取得は動くのでWMIが完全に動かない訳ではなさそうです。
また、Windows8の導入に合わせてCOM参照(Microsoft WMI Scripting V1.2 Library)からSystem.Managementの参照に変更しました。 それでもWindows7では動作するのですが、以下のソースでは、Windows8固定IPに変更する際に「2147749891」のエラーが戻ってきてしまいます。
Windows8で動作させるのに別の方法をとらなければならないのでしょうか? ローカルのAdministrator権限はついているのですが、新たに権限等が追加する必要があるのでしょうか?
Public Function SetStaticIPAddress(ByVal sAdapter As String, _ ByRef sIP As String, _ ByRef sSubnet As String, _ ByRef sGateWay As String, _ ByVal sDNSDiv() As String, _ ByRef sErrMsg As String) As Boolean
Dim sQuery As String = String.Empty Dim oSearcher As New ManagementObjectSearcher() Dim oMoc As ManagementObjectCollection Dim oMo As ManagementObject Dim oScope As New ManagementScope
Dim oNewIp As ManagementBaseObject = Nothing Dim oSetIp As ManagementBaseObject = Nothing
Dim oNewGate As ManagementBaseObject = Nothing Dim oSetGate As ManagementBaseObject = Nothing
Dim oNewDns As ManagementBaseObject = Nothing Dim oSetDns As ManagementBaseObject = Nothing
Dim lRetIp As Long = Nothing Dim lRetGate As Long = Nothing Dim lRetDns As Long = Nothing
Try
oScope = New ManagementScope("\\localhost\root\cimv2") oScope.Connect() If oScope.IsConnected <> True Then Exit Try End If
sQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = true and Description Like '%" & sAdapter & "%'" oSearcher = New ManagementObjectSearcher(sQuery) oMoc = oSearcher.Get
If oMoc.Count <> 0 Then For Each oMo In oMoc oNewIp = oMo.GetMethodParameters("EnableStatic") oNewIp("IPAddress") = New String() {sIP} oNewIp("SubnetMask") = New String() {sSubnet}
oSetIp = oMo.InvokeMethod("EnableStatic", oNewIp, Nothing) '結果 lRetIp = Convert.ToInt64(oSetIp.Properties("ReturnValue").Value) ←ここで「2147749891」のエラー番号が返ってきます。
oNewGate = oMo.GetMethodParameters("SetGateways") oNewGate("DefaultIPGateway") = New String() {sGateWay} oNewGate("GatewayCostMetric") = New Integer() {1}
oSetGate = oMo.InvokeMethod("SetGateways", oNewGate, Nothing) '結果 lRetGate = Convert.ToInt64(oSetGate.Properties("ReturnValue").Value)
oNewDns = oMo.GetMethodParameters("SetDNSServerSearchOrder") oNewDns("DNSServerSearchOrder") = sDNSDiv
oSetDns = oMo.InvokeMethod("SetDNSServerSearchOrder", oNewDns, Nothing) '結果 lRetDns = Convert.ToInt64(oSetDns.Properties("ReturnValue").Value)
Next Else Exit Try End If
Return True Catch exErr As Exception sErrMsg = exErr.Message Return False End Try End Function |