通过jacob java-com桥更改系统时间

时间:2012-04-29 13:54:19

标签: java jacob

我正在尝试使用Jacob来改变系统时间。我写了以下方法:

/*******************************************************************************
* Sets the system time.
*
* @param par_sSystemTime String
*******************************************************************************/
public void setSystemTime(String par_sSystemTime)
{
    ActiveXComponent os =null;
    ComThread.InitMTA();
    try
    {
        InetAddress address =    FoxEnvironment.getRemoteAddress(FoxEnvironment.getLocalHostName()); 
        String connectStr = String.format("winmgmts:{impersonationLevel=impersonate, authenticationLevel=pkt}!\\\\%s\\root\\CIMV2", address.getHostName());
        ActiveXComponent wmi = new ActiveXComponent(connectStr);
        Variant instances = wmi.invoke("InstancesOf", "Win32_OperatingSystem");
        Enumeration<Variant> en = new EnumVariant(instances.getDispatch());
        os = new ActiveXComponent(en.nextElement().getDispatch());
        os.invoke("SetDateTime", par_sSystemTime);
    }
    catch(Exception ex)
    {
       ex.printStackTrace();
       ML.logMsg(MLCon.SERR, null, BaseMessages.GENERAL_UNEXPECTED_ERROR,"setSystemTime(): " + ex);
    }
    catch(NoClassDefFoundError ex)
    {
       ex.printStackTrace();
       ML.logMsg(MLCon.SERR, null, BaseMessages.GENERAL_UNEXPECTED_ERROR,"setSystemTime(): " + ex);
    }
    finally
    {
       // Release the components
       if (os != null)
       {
          os.safeRelease();
          os = null;
       }
       ComThread.Release();
    }
}

执行此方法时,获取异常

com.jacob.com.ComFailException:调用:SetDateTime 来源:SWbemObjectEx 描述:访问被拒绝。

任何人都可以帮忙解决这个问题吗?

提前致谢, 瓦伦蒂诺

1 个答案:

答案 0 :(得分:0)

关于我之前的问题的更多细节。

如果我写下面的方法:

/*******************************************************************************
* Gets the system time.
*
* @return String
*******************************************************************************/
public String getSystemTime()
{
    String sSystemTime = null;
    ActiveXComponent os =null;
    ComThread.InitMTA();
    try
    {
        InetAddress address =    FoxEnvironment.getRemoteAddress(FoxEnvironment.getLocalHostName()); 
        String connectStr = String.format("winmgmts:{impersonationLevel=impersonate, authenticationLevel=pkt}!\\\\%s\\root\\CIMV2", address.getHostName());
        ActiveXComponent wmi = new ActiveXComponent(connectStr);
        Variant instances = wmi.invoke("InstancesOf", "Win32_OperatingSystem");
        Enumeration<Variant> en = new EnumVariant(instances.getDispatch());
        os = new ActiveXComponent(en.nextElement().getDispatch());
        sSystemTime = os.invoke("LocalDateTime");
    }
    catch(Exception ex)
    {
       ex.printStackTrace();
       ML.logMsg(MLCon.SERR, null,  BaseMessages.GENERAL_UNEXPECTED_ERROR,"setSystemTime(): " + ex);
    }
    catch(NoClassDefFoundError ex)
    {
       ex.printStackTrace();
       ML.logMsg(MLCon.SERR, null, BaseMessages.GENERAL_UNEXPECTED_ERROR,"setSystemTime(): " + ex);
    }
    finally
    {
       // Release the components
       if (os != null)
       {
          os.safeRelease();
          os = null;
       }
       ComThread.Release();
    }

    return sSystemTime;
}

它工作正常,所以我想我需要更多的权限来设置系统时间,但我不知道什么样的权限。

此致

瓦伦蒂诺