日期:2014-05-19  浏览次数:20881 次

(急)将 COM 对象与其基础 RCW 分开后不能再使用该对象
我想实现通过WMI来修改计算机的IP配置,在我自己的机器上都可以调试成功,但是换到另一台机器(有.Net   Framework   1.1     没有VS.Net2003)后,就提示 "将   COM   对象与其基础   RCW   分开后不能再使用该对象 ",听人说是因为离开了vs.net环境的原因,请哪位高手帮我看看该如何改才能让程序正常啊

我发现问题就出在foreach()语句中,不会弄了,帮我改下吧

try
{
    ConnectionOptions   Conn   =new   ConnectionOptions();
    ManagementScope   Ms   =   new   ManagementScope   (   "\\\\127.0.0.1\\root\\cimv2 "   ,   Conn   )      
    ManagementObjectSearcher   query   =   new   ManagementObjectSearcher( "SELECT   *   FROM   Win32_NetworkAdapterConfiguration   WHERE   IPEnabled   =   'TRUE ' ")   ;  
    ManagementObjectCollection   queryCollection   =   query.Get();
    ManagementBaseObject   iObj   =   null;
    ManagementBaseObject   oObj   =   null;
    string[]   strDefaultGateway   =   new   string[]{this.DefaultGateway};
    string[]   strIPAddress             =   new   string[]{this.IPaddress};
    string[]   strSubnetMask           =   new   string[]{this.SubnetMask};
    string[]   strFirstDNS               =   new   string[]{this.FirstDNS,this.SecondDNS};
    if   (queryCollection.Count <=0)
    {
        label3.Text+= "没找到本机上的网卡,无法进行IP配置!\n ";
        return;
    }
    foreach(   ManagementObject   MO   in   queryCollection   )  
    {  
        iObj   =   MO.GetMethodParameters(   "EnableStatic "   );
        iObj[ "IPAddress "]     =   strIPAddress;
        iObj[ "SubnetMask "]   =   strSubnetMask;
        oObj   =   MO.InvokeMethod( "EnableStatic ",   iObj,   null);
        iObj   =   MO.GetMethodParameters(   "SetGateways "   );
        iObj[ "DefaultIPGateway "]   =   strDefaultGateway;
        oObj   =   MO.InvokeMethod( "SetGateways ",   iObj,   null);
        iObj   =   MO.GetMethodParameters( "SetDNSServerSearchOrder ");  
        iObj[ "DNSServerSearchOrder "]   =   strFirstDNS;  
        oObj   =   MO.InvokeMethod( "SetDNSServerSearchOrder ",   iObj,   null);
    }
    //效验修改
    string   strResultIP= " ";
    System.Net.IPAddress[]   addressList   =   Dns.GetHostByName(Dns.GetHostName()).AddressList;    
    for   (int   i   =   0;   i   <   addressList.Length;   i   ++)  
    {  
        strResultIP   +=   addressList[i].ToString();  
    }  

    if(strResultIP==this.IPaddr