[Title]: simple Windows Password Viewer
[Time]: 2009-10-09
[Abstract]: Obtain the window control handle at the current mouse using a global hook, and then call GetWindowText () to obtain the Password text.
[Keyword]: Password, view, asterisk, global Hook, Hook, WM_COPYDATA, DLL, XP style
[Environment]: Visual Studio 2008, Visual C ++ 6.0
[Author]: Heaven dew (wintys@gmail.com)
[Body]: This password viewer principle: Obtain the window control handle at the current mouse using a global hook, and then directly call GetWindowText () to obtain the Password text. The project is compiled in VC ++ 6.0 and VS2008. 1. For the DLL project for viewing the password, the global mouse Hook is required for viewing the password function, so you need to put the function into a DLL. PasswordViewerMouseHookDLL. h: # pragma once # ifdef PSWMOUSEHOOKDLL_API_EXPORTS
# Define PSWMOUSEHOOKDLL_API _ declspec (dllexport)
# Else
# Define PSWMOUSEHOOKDLL_API _ declspec (dllimport)
# Endif /*
Winty: 2009-09-29
Call SetHook () to set the Hook, but you do not need to uninstall the Hook. Clear the Hook in DLLMain.
*/
// My Declaration ====================================
# Define MAXCOUNT 200 // Maximum Password Length
# Define DWDATA_PSW_NOTIFY 1 // COPYDATASTRUCT's dwData user-defined value // password information structure, used for sending
Typedef struct tagPswNotify
{
Char psw [MAXCOUNT]; // password/text
POINT pt; // mouse position
HWND hWnd; // control handle
} PSWNOTIFY, * PPSWNOTIFY;/* extern indicates that only the variable declaration is used here, and the variable definition is in the cpp file */
Extern PSWMOUSEHOOKDLL_API BOOL g_bReadySend; // WM_COPYDATA mutex flag, because WM_COPYDATA cannot overlap
Extern PSWMOUSEHOOKDLL_API HWND g_hWnd; // form handle for receiving messages
Extern PSWMOUSEHOOKDLL_API HHOOK g_hhk; // hook handle
Extern PSWMOUSEHOOKDLL_API BOOL g_bView; // whether to view the password // mouse hook Process
PSWMOUSEHOOKDLL_API lresult callback MouseProc (int nCode, WPARAM wParam, LPARAM lParam );
// Set the hook (which can be called in OnInitDialog () of the window)
PSWMOUSEHOOKDLL_API void SetHook (HWND hWnd); PasswordViewerMouseHookDLL. cpp: # include "stdafx. h" # define PSWMOUSEHOOKDLL_API_EXPORTS
# Include "PasswordViewerMouseHookDLL. h" # include <stdio. h> // share data between processes. Add the following to the Linker Options: "/SECTION:. MyShare, RWS"
// Or: # pragma comment (linker, "/SECTION:. MyShare, RWS ")
// View the result: dumpbin/headers *. DLL
# Pragma data_seg (". MyShare ")
HHOOK g_hhk = NULL;/* Hook handle */
HWND g_hWnd = NULL;/* handle of the window for receiving messages */
BOOL g_bReadySend = TRUE;/* used to synchronize COPYDATA messages */
# Pragma data_seg ()
# Pragma comment (linker, "/SECTION:. MyShare, RWS") bool apientry DllMain (HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
Switch (ul_reason_for_call)
{
Case DLL_PROCESS_ATTACH:
Break;
Case DLL_PROCESS_DETACH:
If (g_hhk! = NULL)
{
UnhookWindowsHookEx (g_hhk );
G_hhk = NULL;
G_hWnd = NULL;
}
Break;
Case DLL_THREAD_ATTACH:
Case DLL_THREAD_DETACH:
Break ;}
Return TRUE;
} Lresult callback MouseProc (
Int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
If (nCode = HC_ACTION)
{
PMOUSEHOOKSTRUCT pMouseHookStruct
= Reinterpret_cast <PMOUSEHOOKSTRUCT> (lParam );
LONG x = pMouseHookStruct-& gt; pt. x;
LONG y = pMouseHookStruct-& gt; pt. y;
HWND hWnd = pMouseHookStruct-& gt; hwnd; hWndFromPoint =: WindowFromPoint(PMouseHookStruct-& gt; pt); if (hWndFromPoint! = G_hWnd & g_bReadySend) // the content of the text box showing the password cannot be obtained.
{
G_bReadySend = FALSE;
Char psw [MAXCOUNT];
: GetWindowText (hWndFromPoint, psw, MAXCOUNT );
Pswy y PSWNOTIFY;
Strcpy (pswNotify. psw, psw );
Pswyy.pt. x = x;
PswNotify.pt. y = y;
PswNotify. hWnd = hWndFromPoint; COPYDATASTRUCT cd;
Cd. lpData = & pswy y;
Cd. cbData = sizeof (PSWNOTIFY );
Cd. dwData = DWDATA_PSW_NOTIFY;
: SendMessage (g_hWnd, WM_COPYDATA, NULL, (LPARAM) (LPVOID) & cd );
}
} Return CallNextHookEx (g_hhk, nCode, wParam, lParam );
} Void SetHook (HWND hWnd)
{
G_hWnd = hWnd;
G_hhk = SetWindowsHookEx (WH_MOUSE,
MouseProc,
GetModuleHandle ("PasswordViewerMouseHookDLL "),
NULL );
} To send the password message to the main window, use the WM_COPYDATA message (or other inter-process communication mode). Otherwise, the message will fail to be sent. If WM_SETTEXT is used, the string pointer lParam of the message sent by the global Hook may not be accessed by the main window, resulting in access exceptions. G_bReadySend is set because the WM_COPYDATA message cannot be sent continuously. The next message can be sent only after the previous message is taken away. 2. Create a dialog box for the project PasswordViewer that displays the password. To use DLL:
- Copy PasswordViewerMouseHookDLL, PasswordViewerMouseHookDLL. dll, PasswordViewerMouseHookDLL. lib, and PasswordViewerMouseHookDLL. h generated by the PasswordViewerMouseHookDLL project to the PasswordViewer project.
[References]:
[Attachment]:
- Add # include "PasswordViewerMouseHookDLL. h" and # pragma comment (lib, "PasswordViewerMouseHookDLL. lib") to PasswordViewerDlg. cpp ")
If you want to set the window that finally generates the XP style, you must go to PasswordViewerDlg. add # pragma comment (linker, "/manifestdependency: \" type = 'win32 'name = 'Microsoft. windows. common-Controls 'version = '6. 0.0.0 'processorarchitecture = 'x86 'publicKeyToken = '6595b64144ccf1df' language = '*' \ "") PasswordViewerDlg. the main code of cpp is to respond to the WM_COPYDATA message and display the message content to the window: BOOL CPasswordViewerDlg: OnCopyData (CWnd * pWnd, COPYDATASTRUCT * pCopyDataStruct)
{
If (pCopyDataStruct-& gt; dwData = dwdata_psw_policy &&! G_bReadySend)
{
Ppswy y PPSWNOTIFY = (pPswNotify) pCopyDataStruct-& gt; lpData; CString strMousePosition;
StrMousePosition. Format ("(% 3d, % 3d)", ppswy y-& gt; pt. x, pPswNotify-& gt; pt. y );
M_strMousePosition = strMousePosition;
CString strPsw;
StrPsw. Format ("% s", ppswy y-& gt; psw );
M_strPsw = strPsw;
CString strHWnd;
StrHWnd. Format ("% p", ppswy y-& gt; hWnd );
M_strHWnd = strHWnd;
UpdateData (FALSE); g_bReadySend = TRUE; return TRUE;
}
Else
{
CString str (_ T ("no window found "));
M_strPsw = str; UpdateData (FALSE); return CDialog: OnCopyData (pWnd, pCopyDataStruct );
}
} Do not forget to call the SetHook (m_hWnd) of the DLL in CPasswordViewerDlg: OnInitDialog () to initialize the global hook. 3. Running result: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src = ".. /attachment/200910/200910101255176621750 .jpg" alt = "" border = "0"/> cpp_PasswordViewer.jpg] [attachment]: PasswordViewer.zip: PasswordViewer.exe, PasswordViewerMouseHookDLL. dll, project source code
This article is from the "wintys too many others blog, please be sure to keep this source http://wintys.blog.51cto.com/425414/210675