Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Set system time

Help
valentino
2012-04-28
2013-05-20
  • valentino
    valentino
    2012-04-28

    I'm trying to use Jacob to change the system type. 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

     
  • valentino
    valentino
    2012-04-30

    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