eventlog



// logCollect.cpp : アプリケーションのクラス動作を定義します。
//


#include "stdafx.h"
#include "logCollect.h"
#include "logCollectDlg.h"
#include "Shlwapi.h"
#include "time.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#ifdef WIN32
#include 
#endif
#include 
#include 
#include
#include //
#include //for system();
#pragma comment(lib, "version.lib")
using namespace std;

CString GetFilePrePath();
CString CreateFileName();
BOOL GetSystemBits();
BOOL ExportReg(CString FilePath);
BOOL GetEventLog(CString FilePath);
BOOL ExportSystemInfo(CString FilePath);
BOOL CopyFileFormat(CString oldpath, CString newPath);
BOOL CreateChildFolder(CString FilePath);
BOOL ExportSetupApiPathLog(CString FilePath);
BOOL ExportHomeErrorFile(CString FilePath);
BOOL GetModuleFileInfo(CString FilePath);
CString GetFileVersion(CString filePath);
CString GetRegModulePath(CString reg);
BOOL WriteModuleFile(CString initPath, CString moduluRegPath, CString fileSuffixName);
void Choose32oR64RegPath(CString software, CString initFileName);
BOOL CmdRunRegistryExport(CString strGUIDFor, CString initFileName, CString strFolder, int count, CString strtxt);
BOOL RegExist(CString initFileName, CString strGUIDFor, CString strFolder, int count, CString strtxt);
BOOL Choose32OR64MuduleInfo(CString software, CString initFileName);
BOOL CopyFileOldToNew(CString initFileName);

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// ClogCollectApp

BEGIN_MESSAGE_MAP(ClogCollectApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()

// ClogCollectApp コンストラクション
ClogCollectApp theApp;
HINSTANCE g_hResource;
HINSTANCE g_hInst = NULL;

ClogCollectApp::ClogCollectApp()
{
	// 二重起動の時、アプリ終了フラグ
	m_isCloseWithMutex = FALSE;
}

// 唯一の ClogCollectApp オブジェクトです。

// ClogCollectApp 初期化

BOOL ClogCollectApp::InitInstance()
{
	//言語別リソースの取得&すり替え
	//HINSTANCE w_hResource = GetResourceHandle(NULL);

	//// ハンドル定義
	//if (w_hResource){
	//      AfxSetResourceHandle(w_hResource);
	//      g_hResource = w_hResource;
	//}
	//else{
	//      g_hResource = m_hInstance;
	//}

	//多言語画面ために


	///=========================  多重 ===========================/
	// StartMode設定
	

	// logCollect waiting
	///================Export Start=====================/

	CString InFileName;
	InFileName = CreateFileName();

	if (_T("") == InFileName) {
		return FALSE;
	}
	CreateChildFolder(InFileName);

	ExportHomeErrorFile(InFileName);

	///================Export  End======================/

	

	return TRUE;
}

BOOL CopyFileOldToNew(CString initFileName) {
	CString srcpath = _T("");
	TCHAR svzPath[MAX_PATH];
	BOOL Reg = FALSE;
	if (SUCCEEDED(SHGetFolderPath(NULL,
		CSIDL_LOCAL_APPDATA,
		NULL,
		0,
		svzPath)))
	{
		srcpath = svzPath;
		CString old1 = _T("");
		CString old2 = _T("");
		CString new1 = _T("");
		CString new2 = _T("");
		old1.Format(_T("%s\\%s"), srcpath, RESTARTMESSAGE);
		old1.Format(_T("%s\\%s"), srcpath, RESTART);

		new1.Format(_T("%s\\%s"), initFileName, LOG_CONTENT_COLLECTION);
		new2.Format(_T("%s\\%s"), initFileName, LOG_CONTENT_COLLECTION);

		/*if (flagSourceLog & flagSoureceConfig & flagSoureceProfiles) {

		}*/
		Reg = CopyFileFormat(old1, new1);

		Reg = CopyFileFormat(old1, new2);
	}
	return Reg;

}

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 L"";
	}
	TCHAR strFolder[MAX_PATH];
	SHGetPathFromIDList(pidl, strFolder);
	strFileName = strFolder;
	strFileName = strFileName + L"\\";

	return strFileName;
}

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

	CString FileName = GetFilePrePath();

	if (_T("") == FileName) {
		return _T("");
	}

	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 + _T("\\");
	}
	else {
		return _T("");
	}
}

BOOL CreateChildFolder(CString initFileName) {
	TCHAR svzPath[MAX_PATH];
	CString srcpath;
	BOOL bRet = FALSE;
	CString FolderInit = _T("");
	CString Folder = _T("");
	CString strSourceLog = _T("");
	CString strSourceLogS = _T("");
	int count = 0;

	if (SUCCEEDED(SHGetFolderPath(NULL,
		CSIDL_LOCAL_APPDATA,
		NULL,
		0,
		svzPath)))
	{
		srcpath = svzPath;
		CString strSourceLog, strSoureceConfig, strSourceProfiles;

		// create new file
		for (const TCHAR** folderInitKeys = FOLDER_INIT; **folderInitKeys; folderInitKeys++)
		{
			count++;
			if (count > sizeof(FOLDER_INIT) / 4) {
				break;
			}
			FolderInit = *folderInitKeys;
			strSourceLog.Format(_T("%s%s"), initFileName, FolderInit);
			bRet = CreateDirectory(strSourceLog, NULL);

			if (REGISTRY_INFORMATION == FolderInit && bRet == TRUE)
			{
				int folderCount = 0;
				for (const TCHAR** folderKeys = FOLDER; **folderKeys; folderKeys++)
				{
					folderCount++;
					if (folderCount > sizeof(FOLDER) / 4) {
						break;
					}
					Folder = *folderKeys;
					strSourceLogS.Format(_T("%s\\%s"), strSourceLog, Folder);
					bRet = CreateDirectory(strSourceLogS, NULL);
				}
			}
			else
			{
				continue;
			}
		}
	}
	return bRet;
}

BOOL GetSystemBits()
{
	SYSTEM_INFO si;
	GetNativeSystemInfo(&si);

	if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||

		si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_IA64)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void Choose32oR64RegPath(CString software, CString initFileName) {
	CString strGUIDFor = _T("");
	CString strRegFolderFor = _T("");
	CString strCloud = _T("");
	CString strReceiptFull = _T("");
	CString strReceiptUpdata = _T("");
	CString strTxt = _T(""); 
	CString strFolder = _T("");
	int RegArrayCount = 0;
	for (const TCHAR** keys = REGISTRY_ARRAY; **keys; keys++)
	{
		
		RegArrayCount++;
		if (RegArrayCount > sizeof(FOLDER) / 4) {
			break;
		}
		strGUIDFor = *keys;
		CString str = _T("");
		if (PFUREG == strGUIDFor)
		{
			str.Format(_T("%s%s"), software, strGUIDFor);
			strTxt.Format(_T("%s\\%s"), REGISTRY_INFORMATION, PFUREGTXT);
			if (!RegExist(str, initFileName, _T(""), 0, strTxt))
			{
				continue;
			}
		}
		else if (RUNREG == strGUIDFor)
		{
			str.Format(_T("%s%s"), software, strGUIDFor);
			strTxt.Format(_T("%s\\%s"), REGISTRY_INFORMATION, RUNREGTXT);
			if (!RegExist(str, initFileName, _T(""), 0, strTxt))
			{
				continue;
			}

		}
		else if (RUNONCEREG == strGUIDFor)
		{
			str.Format(_T("%s%s"), software, strGUIDFor);
			strTxt.Format(_T("%s\\%s"), REGISTRY_INFORMATION, RUNONCEREGTXT);
			if (!RegExist(str, initFileName, _T(""), 0, RUNONCEREGTXT))
			{
				continue;
			}
		}
		else if (APPPATHREG == strGUIDFor)
		{
			str.Format(_T("%s%s"), software, strGUIDFor);
			strTxt.Format(_T("%s\\%s"), REGISTRY_INFORMATION, APPPATHREGTXT);
			if (!RegExist(str, initFileName, _T(""), 0, APPPATHREGTXT))
			{
				continue;
			}
		}
		else if (HKEYCURRENTUSERPFU == strGUIDFor)
		{
			str.Format(_T("%s"), strGUIDFor);
			strTxt.Format(_T("%s\\%s"), REGISTRY_INFORMATION, HKEYCURRENTUSERPFUTXT);
			if (!RegExist(str, initFileName, _T(""), 0, HKEYCURRENTUSERPFUTXT))
			{
				continue;
			}
		}
		else if (SESSIONMANGER == strGUIDFor)
		{
			str.Format(_T("%s"), strGUIDFor);
			strTxt.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SESSIONMANGERTXT);
			if (!RegExist(str, initFileName, _T(""), 0, SESSIONMANGERTXT))
			{
				continue;
			}
		}
		else if (MEISHI_FILING_OCR == strGUIDFor)
		{
			str.Format(_T("%s%s%s"), software, UNINSTALL_REGISTRY, strGUIDFor);
			strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, MEISHI_FILING_OCR_FOLER);
			if (!RegExist(str, initFileName, strFolder, 0, MEISHI_FILING_OCR_TXT))
			{
				continue;
			}
		}
		else if (SCANSNAP_CONNECT_APPLICATION == strGUIDFor)
		{
			str.Format(_T("%s%s"), UNINSTALL_REGISTRY, strGUIDFor);
			strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SCANSNAP_CONNECT_APPLICATION_FOLER);
			if (!RegExist(str, initFileName, strFolder, 0, SCANSNAP_CONNECT_APPLICATION_TXT))
			{
				continue;
			}
		}
		else if (CARDMINDER == strGUIDFor)
		{
			str.Format(_T("%s%s"), UNINSTALL_REGISTRY, strGUIDFor);
			strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, CARDMINDER_FOLER);
			if (!RegExist(str, initFileName, strFolder, 0, CARDMINDER_TXT))
			{
				continue;
			}
		}
		else if (SCANSANP_ORGANIZER == strGUIDFor)
		{
			str.Format(_T("%s%s"), UNINSTALL_REGISTRY, strGUIDFor);
			strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SCANSANP_ORGANIZER_FOLER);
			if (!RegExist(str, initFileName, strFolder, 0, SCANSANP_ORGANIZER_TXT))
			{
				continue;
			}
		}
		else if (SCANSANP_MANEGER == strGUIDFor)
		{
			str.Format(_T("%s%s"), UNINSTALL_REGISTRY, strGUIDFor);
			strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SCANSANP_MANEGER_FOLER);
			if (!RegExist(str, initFileName, strFolder, 0, SCANSANP_MANEGER_TXT))
			{
				continue;
			}
		}
		else if (SCANSANP_MANULUL == strGUIDFor)
		{
			str.Format(_T("%s%s"), UNINSTALL_REGISTRY, strGUIDFor);
			strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SCANSANP_MANULUL_FOLER);
			if (!RegExist(str, initFileName, strFolder, 0, SCANSANP_MANULUL_TXT))
			{
				continue;
			}
		}
		else if (SCANASANP_CLOUD_TEMP == strGUIDFor)
		{
			int CloudCount = 0, version = 0;
			for (const TCHAR** cloudkeys = SCANASANP_CLOUD; **cloudkeys; cloudkeys++)
			{
				
				CloudCount++;
				if (CloudCount > sizeof(FOLDER) / 4) {
					break;
				}
				strCloud = *cloudkeys;
				if (SCANASANP_CLOUD_KEY1 == strCloud ||
					SCANASANP_CLOUD_KEY2 == strCloud ||
					SCANASANP_CLOUD_KEY3 == strCloud ||
					SCANASANP_CLOUD_KEY4 == strCloud ||
					SCANASANP_CLOUD_KEY5 == strCloud ||
					SCANASANP_CLOUD_KEY6 == strCloud ||
					SCANASANP_CLOUD_KEY7 == strCloud ||
					SCANASANP_CLOUD_KEY8 == strCloud ||
					SCANASANP_CLOUD_KEY9 == strCloud ||
					SCANASANP_CLOUD_KEY10 == strCloud) {

					strCloud.Format(_T("%s%s"), UNINSTALL_REGISTRY, strCloud);
					strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SCANASANP_CLOUD_FOLER);
					version++;
					if (!RegExist(initFileName, strCloud, strFolder, version, SCANASANP_CLOUD_TXT))
					{
						continue;
					}
				}
			}
		}
		else if (SCANSNAP_RECEIPT_FULL_TEMP == strGUIDFor)
		{
			int ReceiptFullCount = 0, Version = 0;
			for (const TCHAR** receiptFullKeys = SCANSNAP_RECEIPT_FULL; **receiptFullKeys; receiptFullKeys++)
			{
				
				ReceiptFullCount++;
				if (ReceiptFullCount > sizeof(FOLDER) / 4) {
					break;
				}
				strReceiptFull = *receiptFullKeys;
				
				if (SCANSNAP_RECEIPT_FULL_KEY1 == strReceiptFull ||
					SCANSNAP_RECEIPT_FULL_KEY2 == strReceiptFull ||
					SCANSNAP_RECEIPT_FULL_KEY3 == strReceiptFull ||
					SCANSNAP_RECEIPT_FULL_KEY4 == strReceiptFull ||
					SCANSNAP_RECEIPT_FULL_KEY5 == strReceiptFull ||
					SCANSNAP_RECEIPT_FULL_KEY6 == strReceiptFull ||
					SCANSNAP_RECEIPT_FULL_KEY7 == strReceiptFull ||
					SCANSNAP_RECEIPT_FULL_KEY8 == strReceiptFull) {

					strReceiptFull.Format(_T("%s%s"), UNINSTALL_REGISTRY, strReceiptFull);
					strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SCANSNAP_RECEIPT_FOLER);
					Version++;
					if (!RegExist(initFileName, strReceiptFull, strFolder, Version, SCANSNAP_RECEIPT_FULL_TXT))
					{
						continue;
					}
				}
			}
		}
		else if (SCANSNAP_RECEIPT_UPDATE_TEMP == strGUIDFor)
		{
			int i = 0, ReceiptUpdateCount = 0, version = 0;
			for (const TCHAR** receiptUpdateKeys = SCANSNAP_RECEIPT_UPDATE; **receiptUpdateKeys; receiptUpdateKeys++)
			{
				ReceiptUpdateCount++;
				if (ReceiptUpdateCount > sizeof(FOLDER) / 4) {
					break;
				}
				strReceiptUpdata = *receiptUpdateKeys;
				if (SCANSNAP_RECEIPT_UPDATE_KEY1 == strReceiptUpdata ||
					SCANSNAP_RECEIPT_UPDATE_KEY2 == strReceiptUpdata ||
					SCANSNAP_RECEIPT_UPDATE_KEY3 == strReceiptUpdata ||
					SCANSNAP_RECEIPT_UPDATE_KEY4 == strReceiptUpdata ||
					SCANSNAP_RECEIPT_UPDATE_KEY5 == strReceiptUpdata ||
					SCANSNAP_RECEIPT_UPDATE_KEY6 == strReceiptUpdata ||
					SCANSNAP_RECEIPT_UPDATE_KEY7 == strReceiptUpdata)
				{
					strReceiptUpdata.Format(_T("%s%s"), UNINSTALL_REGISTRY, strReceiptUpdata);
					strFolder.Format(_T("%s\\%s"), REGISTRY_INFORMATION, SCANSNAP_RECEIPT_FOLER);
					version++;
					if (!RegExist(initFileName, strReceiptUpdata, strFolder, version, SCANSNAP_RECEIPT_UPDATE_TXT))
					{
						continue;
					}
				}
			}
		}
	}
}

BOOL CmdRunRegistryExport(CString strGUIDFor, CString initFileName, CString strFolder, int count, CString strtxt) {
	CString strWindowsPath = _T("");
	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION pi;
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = TRUE;

	TCHAR szPath[100] = { 0 };
	GetSystemDirectory(szPath, 100);
	strWindowsPath = CString(szPath);
	BOOL reg = FALSE;
	CString str = _T(""), strCmd = _T(""), strX = _T("");

	//strX.Format(_T("%s%s"), UNINSTALL64_REGISTRY, MEISHI_FILING_OCR);

	if (_T("") != strGUIDFor) {
		if (strGUIDFor.Find(PFUREG) != string::npos ||
			strGUIDFor.Find(RUNREG) != string::npos ||
			strGUIDFor.Find(RUNONCEREG) != string::npos ||
			strGUIDFor.Find(APPPATHREG) != string::npos ||
			strGUIDFor.Find(HKEYCURRENTUSERPFU) != string::npos ||
			strGUIDFor.Find(SESSIONMANGER) != string::npos)
		{
			strCmd.Format(_T("%s\\%s %s"), initFileName, strtxt, strGUIDFor);
		}
		else if (strGUIDFor.Find(SCANSNAP_CONNECT_APPLICATION) != string::npos ||
			strGUIDFor.Find(CARDMINDER) != string::npos ||
			strGUIDFor.Find(SCANSANP_ORGANIZER) != string::npos ||
			strGUIDFor.Find(SCANSANP_MANEGER) != string::npos ||
			strGUIDFor.Find(SCANSANP_MANULUL) != string::npos)
		{
			strCmd.Format(_T("%s\\%s\\%s %s"), initFileName, strFolder, strtxt, strGUIDFor);
		}
		else
		{
			CString strCount;
			strCount.Format(_T("%s%s%s"), "V", count, strtxt);
			strCmd.Format(_T("%s%s\\%s %s"), initFileName, strFolder, strCount, strGUIDFor);
		}
	}
	else
	{
		return reg;
	}
	strWindowsPath = _T("regedit /e ") + strCmd;
	return  ::CreateProcess(NULL, (LPWSTR)(LPCTSTR)strWindowsPath, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
}

BOOL RegExist(CString strGUIDFor, CString initFileName, CString strFolder, int count, CString strtxt) {
	HKEY hKey; BOOL Flag; 
	CString str = _T("");
	//--------------------------------------------------------------------------------------//
	//				 登録テーブルが存在HKEY_LOCAL_MACHINEするかどうかを判断する	            |
	//--------------------------------------------------------------------------------------//
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, strGUIDFor, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
	{
		str.Format(_T("%s%s"), HKEY_LOCAL_MACHINE_SCAN, strGUIDFor);
		BOOL cmdFlag = CmdRunRegistryExport(str, initFileName, strFolder, count, strtxt);
		if (cmdFlag) {
			Flag = TRUE;
		}
		else
		{
			Flag = FALSE;
		}
	}
	//--------------------------------------------------------------------------------------//
	//				 登録テーブルが存在HKEY_CURRENT_USERするかどうかを判断する				|
	//--------------------------------------------------------------------------------------//
	else if ((RegOpenKeyEx(HKEY_CURRENT_USER, strGUIDFor, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS))
	{
		str.Format(_T("%s%s"), HKEY_CURRENT_USER_SCAN, strGUIDFor);
		BOOL cmdFlag = CmdRunRegistryExport(str, initFileName, strFolder, count, strtxt);
		if (cmdFlag) {
			Flag = TRUE;
		}
		else
		{
			Flag = FALSE;
		}
	}
	else
	{
		Flag = FALSE;
	}

	return Flag;
}

BOOL ExportReg(CString initFileName) {
	CString strGUIDFor = _T("");
	CString strRegFolderFor = _T("");
	CString strCloud = _T("");
	CString strReceiptFull = _T("");
	CString strReceiptUpdata = _T("");
	BOOL reg = FALSE;
	BOOL bExist = FALSE;

	if (GetSystemBits())
	{
		Choose32oR64RegPath(SOFTWARE64, initFileName);
	}
	else
	{
		Choose32oR64RegPath(SOFTWARE32, initFileName);
	}
	return reg;
}

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

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

	// [System[(Level=2) and (EventID=1000)]]'
	strWindowsPath = _T("WEVTUtil export-log Application /q:\"Event[EventData[(Data=\'") + errorModuleName + _T("\')]][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_NO_WINDOW, NULL, NULL, &si, &pi);

	return reg;
}

BOOL ExportSystemInfo(CString initFileName) {
	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 = _T("msinfo32 /nfo ") + initFileName + SYSTEM_INFO32;

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

	return reg;
}

BOOL CopyFileFormat(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 ExportSetupApiPathLog(CString initFileName) {

	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 = CopyFileFormat(strSetupApiPathLog, initFileName);
	}
	return bRet;
}

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

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

						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 Choose32OR64MuduleInfo(CString software, CString initFileName) {
	int ModuleCount = 0;
	CString str = _T("");
	CString module = _T("");
	CString fileSuffixName = _T("");
	CString strFilePath = _T("");
	BOOL reg = FALSE;
	for (const TCHAR** moduleKeys = ModuleExport; **moduleKeys; moduleKeys++)
	{
		ModuleCount++;
		if (ModuleCount > sizeof(ModuleExport) / 4)
		{
			break;
		}
		module = *moduleKeys;
		
		if (SCANSNAP_HOME_REGBIT == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_REGBIT);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, MODULE_INFO);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (OCR_ENGINE_REGBIT == module)
		{
			str.Format(_T("%s%s"), software, OCR_ENGINE_REGBIT);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, OCR_MODULE_INFO);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (SCANSNAP_HOME_ONLINE_UPDATE == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_ONLINE_UPDATE);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, SCANSNAP_HOME_ONLINE_UPDATE_INFO);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (SCANSNAP_HOME_OCR_LANGPACK1 == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_OCR_LANGPACK1);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, SCANSNAP_HOME_OCR_LANGPACK1_TXT);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (SCANSNAP_HOME_OCR_LANGPACK2 == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_OCR_LANGPACK2);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, SCANSNAP_HOME_OCR_LANGPACK2_TXT);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (SCANSNAP_HOME_OCR_LANGPACK3 == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_OCR_LANGPACK3);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, SCANSNAP_HOME_OCR_LANGPACK3_TXT);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (SCANSNAP_HOME_OCR_LANGPACK4 == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_OCR_LANGPACK4);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, SCANSNAP_HOME_OCR_LANGPACK4_TXT);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (SCANSNAP_HOME_OCR_LANGPACK5 == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_OCR_LANGPACK5);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, SCANSNAP_HOME_OCR_LANGPACK5_TXT);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
		else if (SCANSNAP_HOME_OCR_LANGPACK6 == module)
		{
			str.Format(_T("%s%s"), software, SCANSNAP_HOME_OCR_LANGPACK6);

			if (_T("") != GetRegModulePath(str))
			{
				strFilePath.Format(_T("%s\\*.*"), GetRegModulePath(str));
				fileSuffixName.Format(_T("%s\\%s"), MODULE_EXPORT, SCANSNAP_HOME_OCR_LANGPACK6_TXT);

				reg = WriteModuleFile(initFileName, strFilePath, fileSuffixName);
			}
			else
			{
				continue;
			}
		}
	}
	return reg;
}

BOOL GetModuleFileInfo(CString initFileName) {

	CStringArray strArray64, strArray32;
	CString path, moduleRegPath, fileSuffixName;

	//===================more modeule===================/


	// 64 or 32
	if (GetSystemBits()) {
		Choose32OR64MuduleInfo(SOFTWARE64, initFileName);
	}
	else {
		Choose32OR64MuduleInfo(SOFTWARE32, initFileName);
	}

	//===================more modeule===================/

	return TRUE;
}

BOOL WriteModuleFile(CString initFileName, CString moduluRegPath, CString fileSuffixName) {
	CFileFind finder;
	BOOL bWorking = finder.FindFile(moduluRegPath);
	BOOL Reg = FALSE;

	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 = moduluRegPath + _T("\\") + fileName;
		strVersion = GetFileVersion(tempPath);
		if (_T("") == GetFileVersion(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 = initFileName + fileSuffixName;
		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();
		Reg = TRUE;
	}
	return Reg;
}

CString GetRegModulePath(CString reg) {
	HKEY hKey;
	TCHAR szProductType[MY_BUFSIZE];
	DWORD dwBufLen = MY_BUFSIZE;
	LONG lRet;

	// open reg
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		reg,
		0,
		KEY_QUERY_VALUE,
		&hKey) == ERROR_SUCCESS)
	{
		// query value by path
		lRet = RegQueryValueEx(hKey,
			TEXT("Path"),
			NULL,
			NULL,
			(LPBYTE)szProductType,
			&dwBufLen);
		//return szProductType;
	}
	RegCloseKey(hKey);
	// get path value
	CString strFilePath = szProductType;

	return strFilePath;
}

BOOL ExportHomeErrorFile(CString initFileName) {
	BOOL Flag = FALSE;

	if (_T("") != initFileName) {

		//if (ExportSystemInfo(initFileName)) {
		//      // todo
		//      Flag = TRUE;
		//}
		//else {
		//      Flag = FALSE;
		//}
		if (GetEventLog(initFileName)) {
			// todo
			Flag = TRUE;
		}
		else {
			Flag = FALSE;
		}
		//if (ExportSetupApiPathLog(initFileName)) {
		//	// todo
		//	Flag = TRUE;
		//}
		//else {
		//	Flag = FALSE;
		//}
		
		if (ExportReg(initFileName)) {
			// todo
			Flag = TRUE;
		}
		else {
			Flag = FALSE;
		}
		/*if (GetModuleFileInfo(initFileName)) {
			Flag = TRUE;
		}
		else {
			Flag = FALSE;
		}*/

	}
	else {
		// todo
		//printf("CreateFileName NG");
	}
	return Flag;
}








 

你可能感兴趣的:(eventlog)