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

📄 changepassword.cpp

📁 这是一个变电站的监控程序
💻 CPP
字号:
// ChangePassword.cpp : implementation file
//

#include "stdafx.h"
#include "fert2000.h"
#include "ChangePassword.h"

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

extern int SetNtRegValue(LPCTSTR lpszSection,LPCTSTR lpszEntry,void *nValue,int WriteSize);
extern HKEY GetNtSectionKey(LPCTSTR lpszSection);
extern int QueryNtRegValue(LPCTSTR lpszSection, LPCTSTR lpszEntry,LPBYTE lpData,int QuerySize);
extern bool QueryPassword(CString *UserName,CString *Password=NULL,bool *bAdministrator=NULL); 

/////////////////////////////////////////////////////////////////////////////
// CChangePassword dialog


CChangePassword::CChangePassword(CWnd* pParent /*=NULL*/)
	: CDialog(CChangePassword::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChangePassword)
	m_NewPassword = _T("");
	m_OldPassword = _T("");
	m_ReNewPassword = _T("");
	m_UserName = _T("");
	m_Administrator = FALSE;
	//}}AFX_DATA_INIT
}


void CChangePassword::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChangePassword)
	DDX_Text(pDX, IDC_NEW_PASS, m_NewPassword);
	DDV_MaxChars(pDX, m_NewPassword, 19);
	DDX_Text(pDX, IDC_OLD_PASS, m_OldPassword);
	DDV_MaxChars(pDX, m_OldPassword, 19);
	DDX_Text(pDX, IDC_RE_NEW_PASS, m_ReNewPassword);
	DDV_MaxChars(pDX, m_ReNewPassword, 19);
	DDX_Text(pDX, IDC_USER_NAME, m_UserName);
	DDV_MaxChars(pDX, m_UserName, 19);
	DDX_Check(pDX, IDC_ADMINISTRATOR, m_Administrator);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChangePassword, CDialog)
	//{{AFX_MSG_MAP(CChangePassword)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChangePassword message handlers

void CChangePassword::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	if (bAdd)
	{
		if (m_NewPassword.IsEmpty() || m_UserName.IsEmpty()) 
		{
			MessageBox("密码或用户名不能为空!","错误",MB_OK|MB_ICONSTOP);
			GetDlgItem(IDC_USER_NAME)->SetFocus();
			return;
		}
		if (!(m_NewPassword == m_ReNewPassword))
		{
			MessageBox("新密码不一样,请重新输入!","重新输入",MB_OK);
			GetDlgItem(IDC_NEW_PASS)->SetFocus();
			return;
		}else
		{
			BYTE UserNum;
			QueryNtRegValue("FenlSys\\","UserNum",&UserNum,1);
			char UserName[7],UserData[41];
			if (UserNum>=99)
			{
				MessageBox("不能超过99个用户","错误",MB_OK);
				return;
			}
			sprintf(UserData,"%s",m_UserName.GetBufferSetLength(20));
			sprintf(&UserData[20],"%s",m_NewPassword.GetBufferSetLength(20));
			if (m_Administrator) UserData[40] = 1;
			else UserData[40] = 0;
			sprintf(UserName,"User%02d",UserNum+1);
			if (QueryPassword(&m_UserName)) 
			{
				MessageBox("这个用户已存在!请重新输入!","重新输入");
				GetDlgItem(IDC_USER_NAME)->SetFocus();
				return;
			}
			if (!SetNtRegValue("FenlSys\\",(LPCTSTR)UserName,(void*)UserData,41))
				MessageBox("密码读写错误!","错误",MB_OK|MB_ICONSTOP);
			else
			{
				UserNum++;
				SetNtRegValue("FenlSys\\","UserNum",&UserNum,1);
				MessageBox("用户添加成功!");
			}
		}

	}else
	{
		if (!(m_NewPassword == m_ReNewPassword))
		{
			MessageBox("新密码不一样,请重新输入!","重新输入",MB_OK);
			GetDlgItem(IDC_NEW_PASS)->SetFocus();
			return;
		}else 
		{
			int UserIndex=UserNameList->GetCurSel()+1;
			char UserName[7];
			
			sprintf(UserName,"User%02d",UserIndex);
			//QueryNtRegValue("FenlSys\\",(LPCTSTR)UserName,UserData,41);
			//OldPassword = &UserData[20];*/
			bool bAdministrator;
			BYTE UserData[41];
			if (!QueryPassword(&m_UserName,&m_OldPassword,&bAdministrator))
			{
				MessageBox("你未经授权!请立即停止操作!","口令错误",MB_OK|MB_ICONSTOP);
			}else
			{
				sprintf((char*)UserData,"%s",m_UserName.GetBufferSetLength(20));
				sprintf((char*)&UserData[20],"%s",m_NewPassword.GetBufferSetLength(20));
				UserData[40] = (bAdministrator)?1:0;
				if (!SetNtRegValue("FenlSys\\",(LPCTSTR)UserName,(void*)UserData,41))
					MessageBox("密码读写错误!","错误",MB_OK|MB_ICONSTOP);	
			}
		}
	}
	CDialog::OnOK();
}

BOOL CChangePassword::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	if (bAdd)
	{
		SetWindowText("添加用户");
		GetDlgItem(IDC_OLD_PASS) ->EnableWindow(FALSE);
		GetDlgItem(IDC_USER_NAME)->SetFocus();
	}else
	{
		SetWindowText("修改密码");
		UserNameList->GetText(UserNameList->GetCurSel(),(char *const)m_UserName.GetBufferSetLength(20));
		GetDlgItem(IDC_USER_NAME)->EnableWindow(FALSE);
		int UserIndex=UserNameList->GetCurSel()+1;
		char UserName[7];
		BYTE UserData[41];
		sprintf(UserName,"User%02d",UserIndex);
		QueryNtRegValue("FenlSys\\",(LPCTSTR)UserName,UserData,41);
		m_Administrator = (UserData[40])?true:false;
		
		GetDlgItem(IDC_ADMINISTRATOR)->EnableWindow(FALSE);
		GetDlgItem(IDC_OLD_PASS)->SetFocus();
	}
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
             // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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