2012-04-29 86 views
1

我試圖用Jacob來改變系統時間。我寫了下面的方法:通過jacob java-com橋改變系統時間

/******************************************************************************* 
* 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 說明:拒絕訪問。

任何人都可以提供幫助嗎?

由於提前, 華倫天奴

約我剛纔的問題

回答

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

它正常工作,所以我想我需要一些更多的權限設置系統時間,但我沒有什麼樣的權限的任何線索。

問候,

華倫天奴