eventlog

#include "stdafx.h"
#include "time.h"
#include <afxwin.h>
#include <windows.h>
#include <afx.h>
#include <winsvc.h>
#include <string>
#include <cstring>
#include <stdio.h>
#include <strsafe.h>
#ifdef WIN32
#include <shlobj.h>
#endif
#include <tchar.h>
#include <iostream>
#include<vector>
#include<io.h> //
#include<iostream> //for system();

#pragma comment(lib, "version.lib")
using namespace std;

#define PROVIDER_NAME           L"Application Error"
#define RESOURCE_DLL            L"C:\\WINDOWS\\System32\\wer.dll"
#define ERROR_MODULE_NAME       _T("PfuSshMain.exe")
#define REGISTRY_EXPORT         _T("RegistryExport.txt")
#define SYSTEM_INFO32           _T("SystemInfo.nfo")
#define EVENT_LOG               _T("EventLog.evtx")
#define SOURCELOG               _T("PFU\\ScanSnap Home\\Logs")
#define SOURCECOFIG             _T("PFU\\ScanSnap Home\\Configs")
#define SOURCEPROFILE           _T("PFU\\ScanSnap Home\\Managed\\Profiles\\PfuSsHome.xml")
#define TARGETLOG               _T("ScanSnap Home Logs")
#define TARGETCONFIG            _T("ScanSnap Home Configs")
#define TARGETPROFILE           _T("ScanSnap Home Profiles")
#define TARGETLICENSECUSTOM     _T("ScanSnap Home Custom")
#define REGESTRYINFO64BIT         _T(" HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\PFU")
#define REGESTRYINFO32BIT         _T(" HKEY_LOCAL_MACHINE\\SOFTWARE\\PFU")
#define INSTALLFOLDERREG64BIT   _T("SoftWare\\WOW6432Node\\Youdao\\YoudaoNote")
#define INSTALLFOLDERREG32BIT   _T("SoftWare\\PFU\\ScanSnap Software\\ScanSnap Home")
#define MODULE_INFO             _T("moduleInfo.txt")
#define SETUP_API_PATH          _T("\\INF\\Setupapi.dev.log")
#define MY_BUFSIZE                               500
#define MAX_TIMESTAMP_LEN       23 + 1   // mm/dd/yyyy hh:mm:ss.mmm
#define MAX_RECORD_BUFFER_SIZE  0x10000  // 64K



CString GetFilePrePath();
CString CreateFileName();
BOOL GetSystemBits();
BOOL ExportReg(CString FilePath);
BOOL GetEventLog(CString FilePath);
BOOL ExportSystemInfo(CString FilePath);
BOOL nyCopyFileFormat(CString oldpath, CString newPath);
BOOL CopyFileW(CString FilePath);
BOOL ExportSetupApiPathLog(CString FilePath);
BOOL ExportHomeErrorFile();
CString GetRegModulePath();
BOOL GetFileDescription(const std::string& szModuleName, std::string& RetStr);
BOOL GetFileVersion(const std::string& szModuleName, std::string& RetStr);
CString getLocalProgramVersion(CString filePath);



CString GetFilePrePath() {
	BROWSEINFO  bi;
	bi.hwndOwner = NULL;
	bi.pidlRoot = CSIDL_DESKTOP;
	bi.pszDisplayName = NULL;
	bi.lpszTitle = NULL;
	bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
	bi.lpfn = NULL;
	bi.iImage = 0;
	CString strFileName;
	CString str;

	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
	if (pidl == NULL)
	{
		return "";
	}

	TCHAR strFolder[MAX_PATH];
	SHGetPathFromIDList(pidl, strFolder);
	strFileName = strFolder;
	strFileName = strFileName + "\\";
	return strFileName;
}



CString CreateFileName() {
	CString strWindowsPath;
	CString strTamp;
	TCHAR szPath[MAX_PATH] = { 0 };

	GetSystemDirectory(szPath, MAX_PATH);
	strWindowsPath = CString(szPath);
	CString FileName = GetFilePrePath();
	CTime tm = CTime::GetCurrentTime();
	strTamp = tm.Format("%Y%m%d%H%M%S");

	strWindowsPath = FileName + strTamp;

	//LPCSTR ss = InFileName + str;

	/*if (!GetFileAttributesA(ss) & FILE_ATTRIBUTE_DIRECTORY) {

	}*/
	bool flag = CreateDirectory(strWindowsPath, NULL);
	if (flag) {
		return strWindowsPath + "\\";
	}
	else {
		return "";
	}
}


BOOL GetSystemBits()
{
	SYSTEM_INFO si;
	GetNativeSystemInfo(&si);
	if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
		si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_IA64)
	{
		return TRUE;
	}
	else
	{
		return FALSE;

	}

}


BOOL ExportReg(CString FilePath) {
	CString strWindowsPath;
	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION pi;
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = TRUE;

	TCHAR szPath[100] = { 0 };
	GetSystemDirectory(szPath, 100);
	strWindowsPath = CString(szPath);
	//CString path = "c:\\hello\\";
	if (GetSystemBits()) {
		strWindowsPath = "regedit /e " + FilePath + REGISTRY_EXPORT + REGESTRYINFO64BIT;
	}
	else {
		strWindowsPath = "regedit /e " + FilePath + REGISTRY_EXPORT + REGESTRYINFO32BIT;
	}

	BOOL reg = ::CreateProcess(NULL, (LPWSTR)(LPCTSTR)strWindowsPath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
	return reg;
}



BOOL GetEventLog(CString FilePath) {
	CString strWindowsPath;
	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION pi;
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = TRUE;
	CString NFilePath = " " + FilePath;
	CString errorModuleName = ERROR_MODULE_NAME;

	TCHAR szPath[100] = { 0 };
	GetSystemDirectory(szPath, 100);
	strWindowsPath = CString(szPath);
	// [System[(Level=2) and (EventID=1000)]]'
	strWindowsPath = "WEVTUtil export-log Application /q:\"Event[EventData[(Data=\'" + errorModuleName + "\')]][System[Provider[@Name=\'Application Error\'] and (Level=2) and (EventID=1000)]]\"" + NFilePath + EVENT_LOG;

	BOOL reg = ::CreateProcess(NULL, (LPWSTR)(LPCTSTR)strWindowsPath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);

	return reg;
}



BOOL ExportSystemInfo(CString FilePath) {
	CString strWindowsPath;
	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION pi;
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = TRUE;

	TCHAR szPath[100] = { 0 };
	GetSystemDirectory(szPath, 100);
	strWindowsPath = CString(szPath);

	strWindowsPath = "msinfo32 /nfo " + FilePath + SYSTEM_INFO32;
	BOOL reg = ::CreateProcess(NULL, (LPWSTR)(LPCTSTR)strWindowsPath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);

	return reg;
}



BOOL nyCopyFileFormat(CString oldpath, CString newPath)
{
	BOOL bRet = FALSE;
	TCHAR szFrom[MAX_PATH * 2] = { 0 };
	TCHAR szTo[MAX_PATH * 2] = { 0 };
	memset(szFrom, 0, sizeof(szFrom));
	memset(szTo, 0, sizeof(szTo));
	_tcscpy_s(szFrom, MAX_PATH * 2, oldpath);
	_tcscpy_s(szTo, MAX_PATH * 2, newPath);
	szFrom[_tcslen(oldpath)] = _T('\0');
	szFrom[_tcslen(oldpath) + 1] = _T('\0');
	szTo[_tcslen(newPath)] = _T('\0');
	szTo[_tcslen(newPath) + 1] = _T('\0');
	SHFILEOPSTRUCT fop;

	ZeroMemory(&fop, sizeof(fop));
	fop.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR /*| FOF_SILENT*/;
	fop.wFunc = FO_COPY;
	fop.pFrom = szFrom;              /*_T("E:\\mywww\\server\\mysql\\5.5\\*.*");*/
	fop.pTo = szTo;                  /*_T("E:\\mywww\\server\\test\\5.5");*/
	bRet = (SHFileOperation(&fop) == 0 ? TRUE : FALSE);

	return bRet;
}



BOOL CopyFileW(CString FilePath) {
	TCHAR svzPath[MAX_PATH];
	CString srcpath;
	BOOL bRet = FALSE;

	if (SUCCEEDED(SHGetFolderPath(NULL,
		CSIDL_APPDATA,
		NULL,
		0,
		svzPath)))
	{
		// open old file
		srcpath = svzPath;
		CString strSourceLog, strSoureceConfig, strSourceProfiles;
		strSourceLog = srcpath + "\\" + SOURCELOG + "\\*.*";
		strSoureceConfig = srcpath + "\\" + SOURCECOFIG + "\\*.*";
		strSourceProfiles = srcpath + "\\" + SOURCEPROFILE;

		//BOOL reg = ::CreateProcess(NULL, (LPWSTR)(LPCTSTR)strWindowsPath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
		// create new file
		CString NewStrSourceLog, NewStrSoureceConfig, NewStrSoureceProfiles;
		NewStrSourceLog = FilePath + TARGETLOG;
		NewStrSoureceConfig = FilePath + TARGETCONFIG;
		NewStrSoureceProfiles = FilePath + TARGETPROFILE;

		BOOL flagSourceLog = CreateDirectory(NewStrSourceLog, NULL);
		BOOL flagSoureceConfig = CreateDirectory(NewStrSoureceConfig, NULL);
		BOOL flagSoureceProfiles = CreateDirectory(NewStrSoureceProfiles, NULL);

		if (flagSourceLog & flagSoureceConfig & flagSoureceProfiles) {
			bRet = nyCopyFileFormat(strSourceLog, NewStrSourceLog);
			bRet = nyCopyFileFormat(strSoureceConfig, NewStrSoureceConfig);
			bRet = nyCopyFileFormat(strSourceProfiles, NewStrSoureceProfiles);

		}
	}

	return bRet;
}



BOOL ExportSetupApiPathLog(CString FilePath) {
	TCHAR svzPath[MAX_PATH];
	CString srcpath;
	BOOL bRet = FALSE;
	if (SUCCEEDED(SHGetFolderPath(NULL,
		CSIDL_WINDOWS,
		NULL,
		0,
		svzPath)))
	{
		srcpath = svzPath;
		CString strSetupApiPathLog;
		strSetupApiPathLog = srcpath + SETUP_API_PATH;
		bRet = nyCopyFileFormat(strSetupApiPathLog, FilePath);

	}
	return bRet;
}

CString GetFileVersion(CString strFilePath)
{
	DWORD dwSize;
	DWORD dwRtn;
	CString szVersion;
	dwSize = GetFileVersionInfoSize(strFilePath, NULL);
	if (dwSize == 0)
	{
		return "";
	}
	char *pBuf;
	pBuf = new char[dwSize + 1];
	if (pBuf == NULL)
		return "";
	memset(pBuf, 0, dwSize + 1);

	dwRtn = GetFileVersionInfo(strFilePath, NULL, dwSize, pBuf);
	if (dwRtn == 0)
	{
		return "";
	}

	LPVOID lpBuffer = NULL;
	UINT uLen = 0;

	dwRtn = VerQueryValue(pBuf,
		TEXT("\\VarFileInfo\\Translation"),
		&lpBuffer,
		&uLen);
	if (dwRtn == 0)
	{
		return "";
	}

	szVersion = (char*)lpBuffer;
	delete pBuf;
	return szVersion;

}

CString getLocalProgramVersion(CString filePath)
{
	DWORD  verHandle = 0;
	UINT   size = 0;
	LPBYTE lpBuffer = NULL;
	DWORD  verSize = GetFileVersionInfoSize(filePath, &verHandle);
	CString strVersion;
	if (verSize != NULL)
	{
		LPSTR verData = new char[verSize];

		if (GetFileVersionInfo(filePath, verHandle, verSize, verData))
		{
			if (VerQueryValue(verData, L"\\", (VOID FAR* FAR*)&lpBuffer, &size))
			{
				if (size)
				{
					VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
					if (verInfo->dwSignature == 0xfeef04bd)
					{

						// Doesn't matter if you are on 32 bit or 64 bit,
						// DWORD is always 32 bits, so first two revision numbers
						// come from dwFileVersionMS, last two come from dwFileVersionLS
						/*printf("Version: %d.%d.%d.%d\n",
							(verInfo->dwFileVersionMS >> 16) & 0xffff,
							(verInfo->dwFileVersionMS >> 0) & 0xffff,
							(verInfo->dwFileVersionLS >> 16) & 0xffff,
							(verInfo->dwFileVersionLS >> 0) & 0xffff
							);*/
						
						strVersion.Format(_T("Version:%d.%d.%d.%d"),
							(verInfo->dwFileVersionMS >> 16) & 0xffff,
							(verInfo->dwFileVersionMS >> 0) & 0xffff,
							(verInfo->dwFileVersionLS >> 16) & 0xffff,
							(verInfo->dwFileVersionLS >> 0) & 0xffff);
					}
				}
			}
		}
		delete[] verData;
	}
	return strVersion;
}

BOOL GetDesignModuleFile(CString FilePath) {
   //   CFileFind finder;
   //   vector<CString>  allfile;
   //   vector<CString>  getfile;
   //   BOOL fileFlag = FALSE;
   //   CString path;
	  //path = GetRegModulePath();
   //   

	  //HANDLE hFile;
	  //CString lpFileName = path + "\\*.*";	//指定搜索目錄和文件類型,如搜索d盤的音頻文件可以是"D:\\*.mp3"
	  //WIN32_FIND_DATA pNextInfo;	//搜索得到的文件信息將儲存在pNextInfo中;
	  //hFile = FindFirstFile(lpFileName, &pNextInfo);//請注意是 &pNextInfo , 不是 pNextInfo;
	  //if (hFile == INVALID_HANDLE_VALUE)
	  //{
		 // //搜索失敗
		 // exit(-1);
	  //}
	  //while (FindNextFile(hFile, &pNextInfo))
	  //{
		 // if (pNextInfo.cFileName[0] == '.')//過濾.和..
			//  continue;
		 // //cout << "得到文件:" << pNextInfo.cFileName << endl;
		 // CString tempPath,str;
		 // tempPath = path + _T("\\") + pNextInfo.cFileName;
		 // getLocalProgramVersion(tempPath);
		 // _tprintf_s(_T("%s byte(s)\n"), pNextInfo.nFileSizeLow);

		 // /*str.Format(_T("%c"), pNextInfo.ftLastWriteTime.dwLowDateTime);
		 // _tprintf_s(_T("%Y-%m-%d-%H-%M\n"), str);*/
		 //
	  //}
	  //return TRUE;

	CString path,tPath;
	path = GetRegModulePath();
	tPath = path + _T("\\*.*");
	CFileFind finder;

	BOOL bWorking = finder.FindFile(tPath);

	while (bWorking)
	{
		bWorking = finder.FindNextFile();

		if (!finder.GetLength()) {
			continue;
		}
		
		// filename
		CString fileName,strFileName;
		fileName = finder.GetFileName();
		strFileName.Format(_T("%s"), fileName);

		// version
		CString tempPath,strVersion;
		tempPath = path + _T("\\") + fileName;
		strVersion = getLocalProgramVersion(tempPath);
		if (_T("") == getLocalProgramVersion(tempPath)) {
			strVersion.Format(_T("%s"), _T("Version:"));
		}

		//time
		CTime tempTime;
		CString strTime,str;
		if (finder.GetLastWriteTime(tempTime))
		{
			str = tempTime.Format(_T("%c"));
			strTime.Format(_T("Time:%s"), str);
		}

		// size
		CString strSize;
		strSize.Format(_T("Size:%I64u"), finder.GetLength());

		// path
		CString strPath;
		strPath.Format(_T("Path:%s"), tempPath);
		
		CStdioFile File;
		CString FileNameO,strAll;

		//strAll.Format(_T("%-30s%-30s%-20s%-20s\n"), strFileName, getLocalProgramVersion(tempPath), strTime, strSize, strPath);
		FileNameO = FilePath + MODULE_INFO;
		File.Open(FileNameO, CFile::modeReadWrite | CFile::modeNoTruncate | CFile::modeCreate);
		File.SeekToEnd();
		/*Temp = _T("\r\n-----------------------BEGIN------------------------\r\n");
		Temp += str;
		Temp += _T("\r\n-----------------------END------------------------\r\n");*/
		File.WriteString(strFileName);
		File.WriteString(_T("\t\t\t\t\t"));
		File.WriteString(strVersion);
		File.WriteString(_T("\t\t"));
		File.WriteString(strTime);
		File.WriteString(_T("\t\t"));
		File.WriteString(strSize);
		File.WriteString(_T("\t\t"));
		File.WriteString(strPath);
		File.WriteString(_T("\n"));
		File.Close();
		
	}
	return TRUE;
}



CString GetRegModulePath() {
	HKEY hKey;
	TCHAR szProductType[MY_BUFSIZE];
	DWORD dwBufLen = MY_BUFSIZE;
	LONG lRet;
	CString reg;
	// 64 or 32
	if (GetSystemBits()) {
		reg = INSTALLFOLDERREG64BIT;
	}
	else {
		reg = INSTALLFOLDERREG32BIT;
	}

	// open reg
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		reg,
		0,
		KEY_QUERY_VALUE,
		&hKey) == ERROR_SUCCESS)
	{
		// query value by path

		lRet = RegQueryValueEx(hKey,
			TEXT("Install"),
			NULL,
			NULL,
			(LPBYTE)szProductType,
			&dwBufLen);
		//return szProductType;
	}

	RegCloseKey(hKey);
	//char * strFilePath  = "C:\\Program Files\\Tencent\\QQ\\Bin\\QQ.exe";
	// get path value
	CString strFilePath = szProductType;
	/*cout << strFilePath << " FileDescription is: " << GetFileVersion(strFilePath) << endl;
	getchar();*/

	// by path query module

	return strFilePath;

}

BOOL ExportHomeErrorFile() {
	CString InFileName = CreateFileName();
	BOOL Flag = FALSE;

	if ("" != InFileName) {
		//if (GetEventLog(InFileName)) {
		//	// todo
		//	Flag = TRUE;
		//}
		//else {
		//	Flag = FALSE;
		//}
		//if (ExportSystemInfo(InFileName)) {
		//	// todo
		//	Flag = TRUE;
		//}

		//else {
		//	Flag = FALSE;
		//}
		//if (ExportSetupApiPathLog(InFileName)) {
		//	// todo
		//	Flag = TRUE;
		//}
		//else {
		//	Flag = FALSE;
		//}
		//if (CopyFileW(InFileName)) {
		//	// todo
		//	Flag = TRUE;
		//}
		//else {
		//	Flag = FALSE;
		//}
		//if (ExportReg(InFileName)) {
		//	// todo
		//	Flag = TRUE;
		//}
		//else {
		//	Flag = FALSE;
		//}
		if (GetDesignModuleFile(InFileName)){
			Flag = TRUE;
		}
	}

	else {
		// todo
		printf("CreateFileName NG");

	}
	return Flag;
}




int _tmain(int argc, _TCHAR* argv[])

{

	//GetDesignModuleFile();

	//GetFileVersion("C:\\Program Files (x86)\\PFU\\ScanSnap\\Home\\SshRegister.exe");

	//GetRegModulePath();

	ExportHomeErrorFile();

}

 

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