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

📄 pwdspydlg.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PwdSpyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PwdSpy.h"
#include "PwdSpyDlg.h"
#include "PwdSpyHk.h"
#include <atlbase.h>
#include <oleacc.h>
#include <Mshtml.h>
#include <winuser.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// Register our Window Message
// This message is passed to us if the user tries to active multiple copies of the app
UINT CPwdSpyDlg::s_wmActivateApp = RegisterWindowMessage(CUSTOM_WNDMSG);
void CopyToClipBoard(LPCTSTR szText)
{	OpenClipboard(GetActiveWindow()) ;
EmptyClipboard(); 
HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, 
							   (_tcslen(szText)+ 1) * sizeof(TCHAR)); 
if (hglbCopy == NULL) 
{ 
	CloseClipboard(); 
	return ; 
} 
// Lock the handle and copy the text to the buffer. 
LPTSTR  lptstrCopy = (LPTSTR)GlobalLock(hglbCopy); 
memcpy(lptstrCopy,(void *)szText, 
	   _tcslen(szText) * sizeof(TCHAR)); 
lptstrCopy[_tcslen(szText)] = (TCHAR) 0;    // null character 
GlobalUnlock(hglbCopy); 

// Place the handle on the clipboard. 

SetClipboardData(CF_TEXT, hglbCopy); 

CloseClipboard(); 
return;
}

void SaveToFile(const char* szFileName,BYTE* szBuffer,int nLen)
{
	
	HANDLE hFile=CreateFile(szFileName,
		GENERIC_WRITE|GENERIC_READ,
		FILE_SHARE_WRITE|FILE_SHARE_READ,
		NULL,
		OPEN_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if(hFile==INVALID_HANDLE_VALUE)return ;
	long dwHigh=0;
	SetFilePointer(hFile,0,&dwHigh,FILE_END);
	DWORD dwlen;
	WriteFile(hFile,szBuffer,nLen,&dwlen,NULL);
	CloseHandle(hFile);
}
char* __fastcall stristrA(const char* pszMain, const char* pszSub)
{
	pszMain;    // compiler thinks these are unreferenced because
	pszSub;     // they are in ecx and edx registers

	char* pszTmp1;
	char* pszTmp2;
	char  lowerch, upperch;
	if(lstrlen(pszMain)==0||lstrlen(pszSub)==0)return NULL;
	// We keep the first character of pszSub in lowerch and upperch (lower and
	// upper case). First we loop trying to find a match for this character. Once
	// we have found a match, we start with the second character of both pszMain
	// and pszSub and walk through both strings doing a CharLower on both
	// characters before comparing. If we make it all the way through pszSub with
	// matches, then we bail with a pointer to the string's location in pszMain.

	_asm {
		mov ecx,pszMain
			mov edx,pszSub
			mov esi, ecx    // 
			mov edi, edx    // pszSub

			// Check for NULL pointers

			test ecx, ecx
			je short NoMatch // NULL pointer for pszMain
			test edx, edx
			je short NoMatch // NULL pointer for pszSub

			sub eax, eax
			mov al, [edi]
			push eax
				call DWORD PTR CharLower
				mov lowerch, al
				push eax
				call DWORD PTR CharUpper
				mov upperch, al

				push edi    // increment the second string pointer
				call DWORD PTR CharNext
				mov  edi, eax

				mov pszTmp2, edi
				mov edi, DWORD PTR CharNext // faster to call through a register

Loop1:
			mov al, [esi]
			test al, al
				je short NoMatch        // end of main string, so no match
				cmp al, lowerch
				je short CheckString    // lowercase match?
				cmp al, upperch
				je short CheckString    // upppercase match?
				push esi
				call edi                // Call CharNext to update main string pointer
				mov esi, eax
				jmp short Loop1

CheckString:
			mov pszTmp1, esi    // save current pszMain pointer in case its a match
				push esi
				call edi            // first character of both strings match,
				mov  esi, eax       // so move to next pszMain character
				mov edi, pszTmp2
				mov al, [edi]
				jmp short Branch1

Loop3:
				push esi
					call DWORD PTR CharNext    // CharNext to change pszMain pointer
					mov  esi, eax
					push edi
					call DWORD PTR CharNext    // CharNext to change pszSub pointer
					mov  edi, eax

					mov al, [edi]
Branch1:
					test al, al
						je short Match       // zero in sub string, means we've got a match
						cmp al, [esi]
						je short Loop3

							// Doesn't match, but might be simply a case mismatch. Lower-case both
							// characters and compare again

							sub ecx, ecx
							mov cl, al  // character from pszSub
							push ecx
							call DWORD PTR CharLower
							mov cl, al
							sub eax, eax
							mov al,  [esi]   // character from pszMain
							push ecx    // preserve register
								push eax
								call DWORD PTR CharLower
								pop ecx
								cmp al, cl
								je short Loop3  // we still have a match, keep checking

								// No match, put everything back, update pszMain to the next character
								// and try again from the top

								mov esi, pszTmp1
								mov  edi, DWORD PTR CharNext
								push esi
								call edi
								mov  esi, eax
								jmp short Loop1

Match:
							mov eax, pszTmp1
								jmp short Done  // Don't just return -- always let the C portion of the code handle the return

NoMatch:
							sub eax, eax
Done:
	}

	// Note lack of return in the C portion of the code. Return value is always in
	// eax register which we have set by the time we get here
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About



/////////////////////////////////////////////////////////////////////////////
// CPwdSpyDlg dialog

//***********************************************
/*函数名:GetDocInterface
  参数:hWnd,WebBrowser控件的窗口句柄
  功能:通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口
*/
IHTMLDocument2* GetDocInterface(HWND hWnd) 
{
	// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
	HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
	IHTMLDocument2* pDoc2=NULL;
	if ( hInst != NULL ){
		if ( hWnd != NULL ){
			CComPtr<IHTMLDocument> spDoc=NULL;
			LRESULT lRes;

			UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
			::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

			LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
			if ( pfObjectFromLresult != NULL ){
				HRESULT hr;
				hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
				if ( SUCCEEDED(hr) ){
					CComPtr<IDispatch> spDisp;
					CComQIPtr<IHTMLWindow2> spWin;
					spDoc->get_Script( &spDisp );
					spWin = spDisp;
					spWin->get_document( &pDoc2 );
				}
			}
		}
		::FreeLibrary(hInst);
	} 
	else{//如果没有安装MSAA
		AfxMessageBox(_T("请您安装Microsoft Active Accessibility"));
	}
	return pDoc2;
} 

/*函数名:GetDocInterfaceByMSAA
  参数:hwnd,WebBrowser控件的窗口句柄
  功能:取得hwnd对应的Webbrowser控件的IHTMLDocument2*接口.
*/
IHTMLDocument2* GetDocInterfaceByMSAA(HWND hwnd)
{
	HRESULT hr;
	HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );

	IHTMLDocument2* pDoc2=NULL;
	if ( hInst != NULL ){
		if ( hwnd != NULL ){
			//取得AccessibleObjectFromWindow函数
			LPFNACCESSIBLEOBJECTFROMWINDOW pfAccessibleObjectFromWindow =
				(LPFNACCESSIBLEOBJECTFROMWINDOW)::GetProcAddress(hInst,_T("AccessibleObjectFromWindow"));
			if(pfAccessibleObjectFromWindow != NULL){
				CComPtr<IAccessible> spAccess;
				hr=pfAccessibleObjectFromWindow(hwnd,0,
					IID_IAccessible,(void**) &spAccess);//取得Webbrowser控件的IAccessible接口
				if ( SUCCEEDED(hr) ){
					CComPtr<IServiceProvider> spServiceProv;
					hr=spAccess->QueryInterface(IID_IServiceProvider,(void**)&spServiceProv);
					if(hr==S_OK){
						CComPtr<IHTMLWindow2> spWin;
						hr=spServiceProv->QueryService(IID_IHTMLWindow2,IID_IHTMLWindow2,
							(void**)&spWin);
						/*
						注意:并不是每次都能取得IHTMLWindow2接口,如果调用失败,可以尝试取得IHTMLElement接口:
						CComPtr<IHTMLElement> spElement;
						hr=spServiceProv->QueryService(IID_IHTMLElement,IID_IHTMLElement,(void**)&spElement);
						*/
						if(hr==S_OK)
							spWin->get_document(&pDoc2);
					}
				}
			}
		}
		::FreeLibrary(hInst);
	}
	else{
		AfxMessageBox(_T("请您安装Microsoft Active Accessibility"));
	}
	return pDoc2;
}


CString GetPassword(IHTMLDocument2 *pDoc2,POINT pt)
{
	if(pDoc2==NULL)return "";
	CComPtr<IHTMLElement> pElement;
	HRESULT hr=pDoc2->elementFromPoint(pt.x,pt.y,&pElement);
	if(SUCCEEDED(hr)){
		CComPtr<IHTMLInputTextElement> pPwdElement;
		hr=pElement->QueryInterface(IID_IHTMLInputTextElement,
			(void**)&pPwdElement);
		if(SUCCEEDED(hr)){
			CComBSTR type;
			hr=pPwdElement->get_type(&type);
			if(SUCCEEDED(hr)){
				if(type==_T("password")){
					CComBSTR pwd;
					hr=pPwdElement->get_value(&pwd);
					if(SUCCEEDED(hr)){
						if(pwd.Length()!=0){
							CComBSTR msg;
							msg=pwd;
							CString str(msg);
							return str;
						}
						else{
							return "";
						}
					}
				}
			}
		}
	}
   	pDoc2->Release();
   return "";
 }
CPwdSpyDlg::CPwdSpyDlg(CWnd *pParent)
	: CDialog(CPwdSpyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPwdSpyDlg)
	m_strMousePos = _T("");
	m_strHwnd = _T("");
	m_strCaption = _T("");
	m_strWndClass = _T("");
	m_strIsPwd = _T("");
	m_strPwd = _T("");
	//}}AFX_DATA_INIT

	// Load the icons and cursors
	m_hIconLarge = (HICON)LoadImage(AfxGetApp()->m_hInstance,
		MAKEINTRESOURCE(IDR_MAINFRAME),
		IMAGE_ICON,
		GetSystemMetrics(SM_CXICON),
		GetSystemMetrics(SM_CYICON),
		0);
	m_hIconSmall = (HICON)LoadImage(AfxGetApp()->m_hInstance,
		MAKEINTRESOURCE(IDI_SMALLICON),
		IMAGE_ICON,
		GetSystemMetrics(SM_CXSMICON),
		GetSystemMetrics(SM_CYSMICON),
		0);
	m_hIconBlank = (HICON)LoadImage(AfxGetApp()->m_hInstance,
		MAKEINTRESOURCE(IDI_BLANK_ICON),
		IMAGE_ICON,
		GetSystemMetrics(SM_CXICON),
		GetSystemMetrics(SM_CXICON),
		0);
	m_hIconScan = (HICON)LoadImage(AfxGetApp()->m_hInstance,
		MAKEINTRESOURCE(IDI_LOOK_ICON),
		IMAGE_ICON,
		GetSystemMetrics(SM_CXICON),
		GetSystemMetrics(SM_CXICON),
		0);
	m_hCursorScan = (HCURSOR)LoadImage(AfxGetApp()->m_hInstance,
		MAKEINTRESOURCE(IDC_LOOK_CUR),
		IMAGE_CURSOR,
		GetSystemMetrics(SM_CXCURSOR),
		GetSystemMetrics(SM_CXCURSOR),
		0);
	m_hCursorPrev = NULL;

	m_hWndPrev = m_hWndScanEx = NULL;
	m_bIsLooking = false;
	m_bAlwaysOnTop = true;
	m_nScanLevel = 0;

	// Use ScanEx if Win2K or WinXP
	m_bScanEx = (m_osi.IsNT() && m_osi.GetMajor() >= 5) ? true : false;

	m_wndPopupTip.Create(this);
}

//***********************************************
CPwdSpyDlg::~CPwdSpyDlg()
{
	// Make absolutely sure we unhook before closing
	RemoveHook();

	if(m_hIconLarge != NULL) DestroyIcon(m_hIconLarge);
	if(m_hIconSmall != NULL) DestroyIcon(m_hIconSmall);
	if(m_hIconBlank != NULL) DestroyIcon(m_hIconBlank);
	if(m_hIconScan != NULL) DestroyIcon(m_hIconScan);
	if(m_hCursorScan != NULL) DestroyCursor(m_hCursorScan);
}

//***********************************************
void CPwdSpyDlg::DoDataExchange(CDataExchange *pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPwdSpyDlg)
	DDX_Control(pDX, IDC_LOOK, m_ctrlLook);
	DDX_Text(pDX, IDC_EDIT_MOUSEPOS, m_strMousePos);
	DDX_Text(pDX, IDC_EDIT_HWND, m_strHwnd);
	DDX_Text(pDX, IDC_EDIT_CAPTION, m_strCaption);
	DDX_Text(pDX, IDC_EDIT_WNDCLASS, m_strWndClass);
	DDX_Text(pDX, IDC_EDIT_ISPWD, m_strIsPwd);
	DDX_Text(pDX, IDC_EDIT_PWD, m_strPwd);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPwdSpyDlg, CDialog)
	//{{AFX_MSG_MAP(CPwdSpyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_GETMINMAXINFO()
	ON_WM_COPYDATA()
	//}}AFX_MSG_MAP
	ON_REGISTERED_MESSAGE(s_wmActivateApp, OnActivateApp)
	ON_STN_CLICKED(IDC_LOOK, OnStnClickedLook)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPwdSpyDlg message handlers

//***********************************************
BOOL CPwdSpyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// IDM_ALWAYS_ON_TOP must be in the system command range.
	_ASSERTE((IDM_ALWAYS_ON_TOP & 0xFFF0) == IDM_ALWAYS_ON_TOP);
	_ASSERTE(IDM_ALWAYS_ON_TOP < 0xF000);
	// IDM_ABOUTBOX must be in the system command range.


	CMenu *pSysMenu = GetSystemMenu(FALSE);
	if(pSysMenu != NULL)
	{
		// Remove the "Size" and "Maximize" menu options from the system menu
		pSysMenu->DeleteMenu(SC_SIZE, MF_BYCOMMAND);
		pSysMenu->DeleteMenu(SC_MAXIMIZE, MF_BYCOMMAND);

		// Append a separator, Always On Top, and About
		CString strMenu;
		pSysMenu->AppendMenu(MF_SEPARATOR);
		strMenu.LoadString(IDS_ALWAYS_ON_TOP);
		pSysMenu->AppendMenu(MF_STRING, IDM_ALWAYS_ON_TOP, strMenu);
		strMenu.LoadString(IDS_ABOUTBOX);
		_ASSERTE(!strMenu.IsEmpty());
		pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strMenu);
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIconLarge, TRUE);		// Set big icon
	SetIcon(m_hIconSmall, FALSE);		// Set small icon

	((CComboBox*)GetDlgItem(IDC_COMBO_LEVEL))->SetCurSel(m_nScanLevel);

	OnAlwaysOnTop();

	return TRUE;
}

//***********************************************
void CPwdSpyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if((nID & 0xFFF0) == IDM_ALWAYS_ON_TOP)
	{
		m_bAlwaysOnTop ^= true;
		OnAlwaysOnTop();
	}

	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

//***********************************************
void CPwdSpyDlg::OnPaint()
{
	// If you add a minimize button to your dialog, you will need the code below
	// to draw the icon.  For MFC applications using the document/view model,
	// this is automatically done for you by the framework.

	if(IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM)dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);

⌨️ 快捷键说明

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