简体   繁体   中英

Changing system time via jacob java-com bridge

I'm trying to use Jacob to change the system time. I wrote the following method:

/*******************************************************************************
* 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();
    }
}

When executing this method, get the exception

com.jacob.com.ComFailException: Invoke of: SetDateTime Source: SWbemObjectEx Description: Access denied.

Anyone can help with this?

Thanks in advance, Valentino

One more detail about my previous question.

If I write the following method:

/*******************************************************************************
* 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;
}

it works correctly, so I guess I need some more privileges to set the system time, but I don't have any clue about what kind of privileges.

Regards,

Valentino

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM