windows下使用C++修改系統時間

修改windows系統時間,使用windows的API:SetLocalTime()

但是一定要使用管理員權限運行程序(切記),否則不會生效。

vs設置:項目–>屬性–>鏈接器–>清單文件–>UAC執行級別–>requireAdministrator (/level=‘requireAdministrator’)

從實現到測試搞了兩天,直接上代碼:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

using namespace std;

int main()
{
	//windows下的時間結構體 
	SYSTEMTIME system_time = { 0 };

	//先獲取本地時間 
	GetLocalTime(&system_time);

	//賦值 
	system_time.wYear = 2018;
	system_time.wMonth = 8;
	system_time.wDay = 8;
	system_time.wDayOfWeek = 1;  //GetLocalTime()不設置此項,但是要賦值,避免出錯
	system_time.wHour = 8;
	system_time.wMinute = 8;
	system_time.wSecond = 8;
	system_time.wMilliseconds = 8;

	//修改時間 
	SetLocalTime(&system_time);

	//修改後輸出結果,也可以查看系統時間,顯示已經修改 
	GetLocalTime(&system_time);
	cout << system_time.wYear << endl;
	cout << system_time.wMonth << endl;
	cout << system_time.wDay << endl;
	cout << system_time.wDayOfWeek << endl;
	cout << system_time.wHour << endl;
	cout << system_time.wMinute << endl;
	cout << system_time.wSecond << endl;
	cout << system_time.wMilliseconds << endl;


	system("pause");
	return EXIT_SUCCESS;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章