抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

靖待的技术博客

小清新IT旅程 | 为中华之崛起而读书



  
  
写了一个小玩具,自动记录每次开机时间。 
  
  

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <windows.h>
#include <ctime>
#include <fstream>
#include <tchar.h>
#include <string>
using namespace std;

string getTime()
{
time_t nowTime;
time(&nowTime);
struct tm t;
localtime_s(&t, &nowTime);

char chYear[64], chMonth[64], chDay[64], chHour[64], chMin[64], chSec[64];
int iYear = t.tm_year + 1900;
int iMonth = t.tm_mon + 1;
int iDay = t.tm_mday;
int iHour = t.tm_hour;
int iMin = t.tm_min;
int iSec = t.tm_sec;
_itoa_s(iYear, chYear, sizeof(chYear), 10);
_itoa_s(iMonth, chMonth, sizeof(chMonth), 10);
_itoa_s(iDay, chDay, sizeof(chDay), 10);
_itoa_s(iHour, chHour, sizeof(chHour), 10);
_itoa_s(iMin, chMin, sizeof(chMin), 10);
_itoa_s(iSec, chSec, sizeof(chSec), 10);

string sYear = chYear;
string sMonth = chMonth;
string sDay = chDay;
string sHour = chHour;
string sMin = chMin;
string sSec = chSec;

string sTmp;
sTmp = "开机时间记录:" + sYear + "年" + sMonth + "月" + sDay + "日" + sHour + "时" + sMin + "分" + sSec + "秒";

return sTmp;
}

int main()
{
//写入txt
ofstream fout("D:\\WatchYourPC.txt", ios::app);
string time = getTime();
fout << time << endl;

CHAR pathtofile[MAX_PATH] = { 0 };
HMODULE GetModH = GetModuleHandle(NULL);
GetModuleFileName(GetModH, (LPWSTR)pathtofile, sizeof(pathtofile));

//写入注册表
HKEY hKey;
RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_SET_VALUE, &hKey);
int iLen = sizeof(pathtofile);
RegSetValueEx(hKey, _T("WatchYourPC"), 0, REG_SZ, (const unsigned char*)pathtofile, iLen);
RegCloseKey(hKey);

return 0;

}

效果

效果

评论