⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page2.cpp

📁 一个pmfix程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Page2.cpp : implementation file
//

#include "stdafx.h"
#include "win32.h"
#include "Page2.h"
#include "pmfix.h"
#include <pekmfc.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPage2 property page

IMPLEMENT_DYNCREATE(CPage2, CPropertyPage)

CPage2::CPage2() : CPropertyPage(CPage2::IDD)
{
	//{{AFX_DATA_INIT(CPage2)
	m_advanced = FALSE;
	m_backupfile = _T("");
	//}}AFX_DATA_INIT
}

CPage2::~CPage2()
{
}

void CPage2::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPage2)
	DDX_Control(pDX, IDC_JOBLIST, m_joblist);
	DDX_Check(pDX, IDC_CHECK_ADVANCEDMODE, m_advanced);
	DDX_Text(pDX, IDC_EDIT_BACKUPFILE, m_backupfile);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPage2, CPropertyPage)
	//{{AFX_MSG_MAP(CPage2)
	ON_BN_CLICKED(IDC_CHECK_ADVANCEDMODE, OnCheckAdvancedmode)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTTON_VIEWLOG, OnButtonViewlog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPage2 message handlers

BOOL CPage2::OnSetActive() 
{
	
	TRACE("On set active page 2\n");

	((CPmfix*) GetParent())->SetWizardButtons(PSWIZB_NEXT);

	
	return CPropertyPage::OnSetActive();
}

BOOL CPage2::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();


	ListView_SetExtendedListViewStyle(m_joblist.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES);

	CString s;

	s.LoadString(IDS_LIST_VARIABLE);
	m_joblist.InsertColumn(0, s, LVCFMT_LEFT, 56);
	s.LoadString(IDS_LIST_NEWVALUE);
	m_joblist.InsertColumn(1, s, LVCFMT_LEFT, 150);
	s.LoadString(IDS_LIST_OLDVALUE);
	m_joblist.InsertColumn(2, s, LVCFMT_LEFT, 150);
	s.LoadString(IDS_LIST_PATH);
	m_joblist.InsertColumn(3, s, LVCFMT_LEFT, 96);


	UpdateData();

	//Advaned mode off/on
#ifdef _DEBUG
	m_advanced = true;
#else
	m_advanced = false;
#endif

	//Create default backupfilename
	CString buffer = pek::mfcGetSpecialFolderPath(CSIDL_DESKTOP);
	CTime t (CTime::GetCurrentTime());

	m_backupfile.LoadString(IDS_FILE_PREFIX);
	m_backupfile = buffer + "\\" + m_backupfile + " " + t.Format("%Y-%m-%d %H%M%S") + ".reg";

	UpdateData(FALSE);

	OnCheckAdvancedmode();


	//If no errors...
	if(CheckRegErrors() == 0)
	{
		//then set no error message...
		TRACE("There is no errors. Go to next page directly.\n");
		((CPmfix*) GetParent())->finalmessage.LoadString(IDS_STATUS_NOERRFOUND);

		//we switch page the first time paint is called.
	}
	else
	{
		//yes, we have som errors!
		//Create message and view it to user.
		CString temp;
		temp.Format(IDS_STATUS_XERRFOUND, joblist.size());

		GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(temp);			
	}

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CPage2::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	static bool firsttime = true;

	if(firsttime && joblist.size() == 0)
	{
		TRACE("No errors... go to next page\n");
		((CPmfix*) GetParent())->SetActivePage(2);
	}

	firsttime = false;
	
}


//Hide/Show controls.
void CPage2::OnCheckAdvancedmode() 
{
	UpdateData();
	
	UINT controls[] = {	IDC_STATIC_BACKUP,
						IDC_EDIT_BACKUPFILE, 
						IDC_JOBLIST, 
						IDC_BUTTON_BROWSE, 
						IDC_STATIC_UNSELECTINFO
						};

	for(int i = 0; i < sizeof(controls)/sizeof(UINT); i++)
	{
		GetDlgItem(controls[i])->EnableWindow(m_advanced);	
		GetDlgItem(controls[i])->ShowWindow(m_advanced);	
	}
}

//Check errors and create a solution...
int CPage2::CheckRegErrors()
{

	CString &log = ((CPmfix*) GetParent())->log,
			logtemp;

	//Clear log...
	log = "";
	

	
	UpdateData();
	
	//Clear joblist
	joblist.clear();

	//Check in this path's:
	const CString pathlist[]	= {	"Run",					"RunOnce",				"Run",					"RunOnce",				"RunServices",			"RunServicesOnce",		"RunOnce\\Setup"		};
	const CString rootlist[]	= {	"HKEY_CURRENT_USER",	"HKEY_CURRENT_USER",	"HKEY_LOCAL_MACHINE",	"HKEY_LOCAL_MACHINE",	"HKEY_LOCAL_MACHINE",	"HKEY_LOCAL_MACHINE",	"HKEY_LOCAL_MACHINE"	};

	//Check if user is administrator by oper a HKEY_LOCACL_MACHINE key
	//Noo... why... we never use this?
	/*bool isAdmin = false;
	
	HKEY tempKey;
	if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, LPCTSTR("Software\\Microsoft\\Windows\\CurrentVersion\\" + pathlist[3]), 0,
			KEY_ALL_ACCESS, &tempKey) == ERROR_SUCCESS)
	{
		isAdmin = true;
		::RegCloseKey(tempKey);
	}*/


	//Check all path's
	for(int key = 0; key < sizeof(pathlist)/sizeof(CString); key ++)
	{

		HKEY hKey;

		//Set key to machine...
		HKEY m_hRootKey = HKEY_LOCAL_MACHINE;
		
		//Change to current_user?
		if(rootlist[key] == "HKEY_CURRENT_USER")
			m_hRootKey = HKEY_CURRENT_USER;


		TRACE("Checking key: %s\\Software\\Microsoft\\Windows\\CurrentVersion\\%s\n", rootlist[key], pathlist[key]);	

		//Add to log:
		//If not first time, add some space
		if(log != "")
			log += "\r\n\r\n";

		logtemp.Format(IDS_LOG_OPENKEY, rootlist[key], pathlist[key]);
		log += logtemp;


		//Open registry in current path
		//Continue to next path if an error happens here (user haven't permission
		//or path does'nt exist)
		if (::RegOpenKeyEx(	m_hRootKey, 
							LPCTSTR("Software\\Microsoft\\Windows\\CurrentVersion\\" + pathlist[key]),
							0,
							KEY_READ, 
							&hKey)
							!= ERROR_SUCCESS)
		{
			TRACE("Couldn't open!\n");	

			logtemp.LoadString(IDS_LOG_NOKEY);
			log += logtemp;
			
			
			//Check next key
			continue;
		}

		//Check all values in this key....
		DWORD index = 0;

		//Bufflength... make sure it is big enough
		const BUFLENGTH = 4096;

		while(1)
		{
			DWORD buflength = BUFLENGTH;		//Buf length
			DWORD type		= REG_SZ;			//Type (string)

			char name[BUFLENGTH];				//Save value name here
		

			//If we read data and value at the same time it breaks if we get a swedish character in the name

			//Get name on next value...
			if( RegEnumValue(	hKey,			// handle to key to query
								index,			// index of value to query
								name,			// address of buffer for value string
								&buflength,		// address for size of value buffer
								NULL,			// reserved, must be NULL
								&type,			// address of buffer for type code
								NULL,			// address of buffer for value data		(LPBYTE)data
								NULL			// address for size of data buffer		&buflength
							)
						!= ERROR_SUCCESS
				)
						break; //No more values in this key

			TRACE("Checking value: %s... ", name);	

			logtemp.Format(IDS_LOG_CHECKVALUE, name);
			log += logtemp;

			//Count up index to next chect
			index++;

			//Make sure this is a string
			if(type != REG_SZ)
			{
				TRACE("Hey! This is not a string\n");

				logtemp.LoadString(IDS_LOG_NOTASTRING);
				log += logtemp;

				//Check next value
				continue;
			}

			//First, get size that it necessary for data
			//Then allcolare memory and read data...
		
			//Get data size
			DWORD datasize=0;
			::RegQueryValueEx(	hKey,			// handle to key to query
								name,			// address of name of value to query
								NULL,			// reserved
								&type,			// address of buffer for value type
								NULL,			// address of data buffer
								&datasize		// address of data buffer size
								);
			//To be safe..
			datasize++;

			//allcolate memory
			char* data	= new char[datasize];				//Save data here

			//Read data
			::RegQueryValueEx(	hKey,			// handle to key to query
								name,			// address of name of value to query
								NULL,			// reserved
								&type,			// address of buffer for value type
								(LPBYTE)data,	// address of data buffer
								&datasize		// address of data buffer size
								);


			//Log...
			logtemp.Format(IDS_LOG_CHECKDATA, data);
			log += logtemp;

			//Save better value in here...
			CString newValue;

			//Is the current value OK?
			if( !isLegalValue(data, newValue ))
			{
				//This is not a legal value, add job
				job newjob;

				newjob.originalvalue = data;
				newjob.path = pathlist[key];
				newjob.root = rootlist[key];
				newjob.variable = name;
							
				newjob.newvalue = newValue;
						
				//Add in joblist.		
				joblist.insert(joblist.begin(), newjob);

				TRACE("Bad! Change to: \"%s\"\n", newValue);

				//Log... what to do?
				CString logtemp2;
				if(newValue == "")
					logtemp2.LoadString(IDS_LOG_BADVALUE_DELETE);
				else
					logtemp2.Format(IDS_LOG_BADVALUE_CHANGE, newValue);

				//Create new line...
				logtemp.Format(IDS_LOG_BADVALUE, logtemp2);
				log += logtemp;
			}
			else
			{
				TRACE("OK\n");

				//Add "OK" to log
				logtemp.Format(IDS_LOG_OK);
				log += logtemp;
			}

			//Clear
			delete [] data;
		
		}
					
		//Close registry
		::RegCloseKey(hKey);

	} //Next key

	//Update list
	DoUpdateList();

	//Return number of errors.
	return joblist.size();
}


//Check if values i legal...
//if not, create a better value i newValue
bool CPage2::isLegalValue(CString value, CString &newValue)
{

	//Empty value or single quote error?
	if(value.IsEmpty() || value == _T("\""))
	{
		newValue = _T("");
		return false;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -