2015-12-13 35 views
0

我想更改我的系統時間,如何更改Qt中的Windows系統時間? 我用這種方式,但失敗了!如何在Qt中更改Windows系統時間?

#include <QApplication> 
#include <iostream> 
#include <time.h> 
#include <windows.h> 
#include <QDateTime> 
#include <QDebug> 
using namespace std; 
bool setDate(int,int,int);  
int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 
    qDebug()<<QDateTime::currentDateTime()<<endl; //before change time       
    if(setDate(2015,1,1))       //set time 
    { 
    qDebug()<<QDateTime::currentDateTime()<<endl; //if succeed,output time 
    } 
    return a.exec(); 
} 
bool setDate(int year,int mon,int day) 
{ 
    SYSTEMTIME st; 
    GetSystemTime(&st); // Win32 API get time 
    st.wYear=year;   //set year 
    st.wMonth=mon;   //set month 
    st.wDay=day;   //set day 

    return SetSystemTime(&st); //Win32 API set time 
} 

在此先感謝您。

+0

至少'if(setDate(2015,1,1);)' - >'if(setDate(2015,1,1))'使其被編譯。 – MikeCAT

+0

哦,謝謝... – YFLu

+1

嘗試通過['GetLastError'函數]檢查失敗的原因(https://msdn.microsoft.com/ja-jp/library/windows/desktop/ms679360(v = vs.85 )的.aspx)。 – MikeCAT

回答

3

更改系統時間需要管理員權限。這意味着您需要:

  • requireAdministrator選項添加到您的清單中,以便程序始終具有管理權限。這是一個壞主意,你每次開始時都不會喜歡UAC對話框。
  • 或者,通過啓動以管理員身份運行的單獨進程來更改時間。另一個帶有適當清單的可執行文件,一個以runas shell動詞開頭的進程,或者一個以COM高程名字對象開頭的進程。

如果這是給你的gobbledygook,你需要閱讀UAC。從這裏開始:https://msdn.microsoft.com/en-us/library/windows/desktop/dn742497(v=vs.85).aspx

+0

非常感謝。我已經解決了這個問題 – YFLu

相關問題