C++ 註冊表取值 按行讀取txt文件 時間差天數 格林威治時間轉標準時間

// regedit.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//

#include "pch.h"//vs新建項目自動生成
#include <iostream>
#include <assert.h>
#include "windows.h"
#include "tchar.h"
#include "conio.h"
#include "stdio.h"
//file
#include <fstream>
#include <string>
//time
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;


void wcharTochar(const wchar_t *wchar, char *chr, int length)//寬字符轉char
{
	WideCharToMultiByte(CP_ACP, 0, wchar, -1,
		chr, length, NULL, NULL);
}

bool OpenRegKey(HKEY& hRetKey)//打開註冊表
{
	LPCWSTR sw = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Bandizip.exe");//_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Bandizip.exe");
	wprintf(L"SW is %s\n",sw);
	if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, sw, &hRetKey))
	{
		return true;
	}
	printf("OpenRegKey return is false!\n");
	return false;
}

bool QueryRegKey(LPCWSTR strSubKey, LPCWSTR strValueName, char *strValue,int length)//從註冊表裏取值
{
	DWORD dwType = REG_SZ;//定義數據類型
	DWORD dwLen = MAX_PATH;

	wchar_t data[MAX_PATH];
	HKEY hKey;
	HKEY hSubKey;
	if (OpenRegKey(hKey))
	{
		if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, strSubKey, &hSubKey))
		{
			TCHAR buf[256] = { 0 };
			
			if (ERROR_SUCCESS == RegQueryValueEx(hSubKey, strValueName, 0, &dwType, (LPBYTE)data, &dwLen))
			{
				wcharTochar(data, strValue,length);
				wprintf(L"data = %s,len= %d\n", data,strlen((const char *)data));
				RegCloseKey(hKey); //關閉註冊表
				return true;
			}
		}
		RegCloseKey(hKey); //關閉註冊表
	}

	return false;
}
//文件

int CountLines(char *filename)
{
	ifstream ReadFile;
	int n = 0;
	string tmp;
	ReadFile.open(filename, ios::in);//ios::in 表示以只讀的方式讀取文件
	if (ReadFile.fail())//文件打開失敗:返回0
	{
		return 0;
	}
	else//文件存在
	{
		while (getline(ReadFile, tmp, '\n'))
		{
			n++;
		}
		ReadFile.close();
		return n;
	}
}

string ReadLine(char *filename, int line)
{
	int lines, i = 0;
	string temp;
	fstream file;
	file.open(filename, ios::in);
	lines = CountLines(filename);

	if (line <= 0)
	{
		return "Error 1";
	}
	if (file.fail())
	{
		return "Error 2";
	}
	if (line > lines)
	{
		return "Error 3";
	}
	while (getline(file, temp) && i < line - 1)
	{
		i++;
	}
	file.close();
	return temp;
}
//time
/** @fn: int CHttpUtil::GetLocalTime(char* szLocTime)
 *  @brief : 獲取系統本地時間
 *  @param (out) char * szLocTime: 存放本地時間的緩存區,外部傳入
 *  @return int : szLocTime的實際長度
 */
int GetLocalTime(char* szLocTime)//獲取本地標準時間
{
	if (szLocTime == NULL)
	{
		return -1;
	}
	time_t rawTime;
	struct tm* timeInfo;
	char szTemp[30] = { 0 };

	time(&rawTime);
	timeInfo = localtime(&rawTime);
	strftime(szTemp, sizeof(szTemp), "%Y-%m-%d-%H-%M-%S", timeInfo);

	strcpy_s(szLocTime, strlen(szTemp) + 1, szTemp);
	return strlen(szLocTime);
}
int stamp_to_standard(time_t nSrc, char *sDestTime)//格林威治時間轉標準時間
{
	struct tm p;
	p = *localtime(&nSrc);
	strftime(sDestTime, 1000, "%Y-%m-%d-%H-%M-%S", &p);
	return 0;
}
time_t convert(int year, int month, int day,int H,int M,int S)
{
	tm info = { 0 };
	info.tm_year = year-1900;
	info.tm_mon = month-1;
	info.tm_mday = day;
	info.tm_sec=S;   // seconds after the minute - [0, 60] including leap second
	info.tm_min=M;   // minutes after the hour - [0, 59]
	info.tm_hour= H;
	return mktime(&info);
}
int get_days(const char* from, const char* to)
{
	int year, month, day,H,M,S;
	sscanf(from, "%d-%d-%d-%d-%d-%d", &year, &month, &day, &H, &M, &S);
	printf("163--------------%d-%d-%d-%d-%d-%d\n", year, month, day, H, M, S);
	int fromSecond = (int)convert(year, month, day, H, M, S);
	printf("--------------%d\n", fromSecond);
	sscanf(to, "%d-%d-%d-%d-%d-%d", &year, &month, &day, &H, &M, &S);
	int toSecond = (int)convert(year, month, day, H, M, S);
	printf("%d\n", toSecond);
	return (toSecond - fromSecond) / 24 / 3600;
}
int main()
{
	HKEY  hKey = NULL;
	string result;
	LPCWSTR strSubKey= _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Bandizip.exe");
	LPCWSTR strValueName= _T("Path");
	char strValue[256];
	int length = 256;
	bool status = QueryRegKey(strSubKey,strValueName,strValue,length);
	printf("status is %d\n", status);
	printf("result is %s\n", strValue);
	
	int line;
	char filename[] = "C:\\Program Files\\Bandizip\\savapi\\update_AVIRA.txt";
	string result_file;
	
	line = CountLines(filename);
	if (line > 0)
	{
		result_file = ReadLine(filename, 1);
		cout << result_file << endl;
	}
	else
	{
		cout<<"no file"<<endl;
	}
	//舊
	time_t a = 1343976859;
	stamp_to_standard(a, strValue);
	printf("time is %s\n", strValue);
//本地時間
	char szTemp[30] = { 0 };
	GetLocalTime(szTemp);
	printf("szTemp is %s\n", szTemp);

	int days = get_days(strValue, szTemp);
	printf("From:%s\nTo:%s\n", strValue, szTemp);
	printf("%d\n", days);
	return 0;
}

 

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