假设用户的登陆方式为网络登陆,那么我们可以安以下的步骤,来实现保存密码和防止ESC键:
1.在RunService处(HKLM\)填上一个服务程序,可保证在登陆框出现前启动。
2.在Service的程序中用FindWindow,FindWindowEx找到窗口和Edit,Button的Handle,便可控制。
代码如下:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <winuser.h>
#include <process.h>
#include "UsbEkey.h"
//-----------------------------------------------------------------------
// G L O B A L S
//-----------------------------------------------------------------------
const LPCSTR DLGTTLE = "请输入网络密码";
const LPCSTR CHKTTLE = "保存密码";
const LPSTR CHK_APP_NAME = "ChkBox.exe";
const UINT RSP_SIMPLE_SERVICE = 1;
const UINT RSP_UNREGISTER_SERVICE = 0;
typedef DWORD (WINAPI * pRegFunction)(
DWORD dwProcessId,
DWORD dwType);
HWND hDlg = NULL;
HWND hUserName = NULL;
HWND hPwd = NULL;
HWND hDomain = NULL;
HWND hBtnOk = NULL;
HWND hBtnCancel = NULL;
HWND hChkBox = NULL;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ACCOUNT g_Account,g_NewAccount;
//-----------------------------------------------------------------------
// Find The Application CreateCheckBox Contrl's
//-----------------------------------------------------------------------
HWND GetMyChkBox(HWND hDlg)
{
HWND hTmp = NULL,hChk = NULL;
bool b1 = false,b2 = false;
hTmp =FindWindowEx(hDlg,NULL,"button",NULL);
hChk = hTmp;
while (!b1 && !b2)
{
hChk = GetNextDlgGroupItem(hDlg,hChk,FALSE);
char buf[255];
GetWindowText(hChk,buf,255);
if (strcmp(buf,CHKTTLE) == 0 )
{
b1 = true;
}
if (hTmp == hChk)
{
b2 = true;
OutputDebugString("Not Find The CheckBox\n");
}
}
return hChk;
}
//-----------------------------------------------------------------------
// Get The UserName/Pwd/Domain And Save To The UsbEkey Thread
//-----------------------------------------------------------------------
void GetTextFunProc(PVOID parg)
{
while (NULL != hDlg )
{
SendMessage(hUserName,WM_GETTEXT,255,(LPARAM)g_NewAccount.UserName);
SendMessage(hPwd,WM_GETTEXT,255,(LPARAM)g_NewAccount.PassWord);
SendMessage(hDomain,WM_GETTEXT,255,(LPARAM)g_NewAccount.Domain);
hChkBox = GetMyChkBox(hDlg);
if (NULL == hChkBox) {
break;
}
int iRet = SendMessage(hChkBox,BM_GETCHECK ,0,0);
if (BST_CHECKED == iRet){
g_NewAccount.Flag = FLAG_SAVE_PWD;
}
if (BST_UNCHECKED == iRet){
g_NewAccount.Flag = FLAG_NOT_SAVE_PWD;
}
char buf[255];
sprintf(buf,"GetText:\t UserName=%s;Pwd=%s;Domain=%s\;Flag=%d\n",
g_NewAccount.UserName,g_NewAccount.PassWord,g_NewAccount.Domain,
g_NewAccount.Flag);
OutputDebugString(buf);
hDlg = FindWindow(NULL,DLGTTLE);
Sleep(100);
}
//
// On There Save UserName/Pwd/Domain to UsbEkey
//
if (g_NewAccount.Flag != g_Account.Flag
|| g_NewAccount.Flag == FLAG_SAVE_PWD)
{
if (0 == WriteAccount(g_NewAccount))
{
memcpy(&g_Account,&g_NewAccount,sizeof(ACCOUNT));
}
}
//
// Close CreaeChkBox Application
//
TerminateProcess(pi.hProcess,0);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return;
}
//-----------------------------------------------------------------------
// Set The UserName/Pwd/Domain Thread
//-----------------------------------------------------------------------
void SetTextFunProc(PVOID parg)
{
//DWORD dwStartTime;
//dwStartTime = GetTickCount();
while (true)
{
if (strlen(g_Account.PassWord) <= 0
&& strlen(g_Account.UserName) <= 0
&& strlen(g_Account.Domain) <= 0)
{
OutputDebugString("ReadAccount Start....\n");
ReadAccount(g_Account);
}
hDlg = FindWindow(NULL,DLGTTLE);
if (NULL != hDlg)
{
OutputDebugString("Find The Login Dlg\n");
hUserName = FindWindowEx(hDlg,NULL,"edit",NULL);
if (hUserName == NULL)
{
OutputDebugString("Can Not Find The Edit_UserName\n");
return;
}
hPwd = GetNextDlgTabItem(hDlg,hUserName,FALSE);
if (hPwd == NULL)
{
OutputDebugString("Can Not Find The Edit_PassWord\n");
return;
}
hDomain = GetNextDlgTabItem(hDlg,hPwd,FALSE);
if (hDomain == NULL)
{
OutputDebugString("Can Not Find The Edit_Domain\n");
return;
}
hBtnOk = GetNextDlgTabItem(hDlg,hDomain,FALSE);
if (hDomain == NULL)
{
OutputDebugString("Can Not Find The Btn_OK\n");
return;
}
hBtnCancel = GetNextDlgTabItem(hDlg,hBtnOk,FALSE);
if (hDomain == NULL)
{
OutputDebugString("Can Not Find The Btn_Cancel\n");
return;
}
//
// Exec CreateChkBox Application
//
OutputDebugString("CreateProcess CreateCheckBox\n");
CreateProcess(NULL,CHK_APP_NAME,NULL,
NULL, FALSE,0,NULL,NULL,&si,&pi );
Sleep(500);
hChkBox = GetMyChkBox(hDlg);
if (g_Account.Flag == FLAG_SAVE_PWD){
SendMessage(hChkBox,BM_SETCHECK ,(WPARAM)BST_CHECKED,0);
}
//
// Set Pwd/UserName/Domain
//
if (g_Account.Flag == FLAG_SAVE_PWD) {
if (strlen(g_Account.UserName) > 0) {
SendMessage(hUserName,WM_SETTEXT,255,(LPARAM)g_Account.UserName);
}
if (strlen(g_Account.PassWord) > 0) {
SendMessage(hPwd,WM_SETTEXT,255,(LPARAM)g_Account.PassWord);
}
if (strlen(g_Account.Domain) > 0) {
SendMessage(hDomain,WM_SETTEXT,255,(LPARAM)g_Account.Domain);
}
//
// Disable The "ESC" and "Cancel" Buttton
//
char szBuf[255];
GetWindowText(hBtnCancel,szBuf,255);
if (strcmp("取消",szBuf) == 0)
{
EnableWindow(hBtnCancel,FALSE);
}
//keybd_event( VK_RETURN,0x45,KEYEVENTF_EXTENDEDKEY | 0,0 );
}
GetTextFunProc(NULL);
OutputDebugString("Create The GetTextFunProc Thread!\n");
}
Sleep(1000*3);
//
// After Half Hour Application Exit
//
//if ((GetTickCount() - dwStartTime)/(1000*60) >=30) {
// break;
//}
OutputDebugString("SetText Thread Is Runing...\n");
}
return ;
}
//-----------------------------------------------------------------------
// InitlizeFuntions
//-----------------------------------------------------------------------
bool Initlize()
{
//bool pOld;
//SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,true,&pOld,SPIF_UPDATEINIFILE);
//
// RegisterServiceProcess then TaskMgr Can't Find It
//
HINSTANCE hKernelLib;
pRegFunction RegisterServiceProcess;
hKernelLib = LoadLibrary("kernel32.dll");
if(hKernelLib)
{
RegisterServiceProcess =
(pRegFunction)GetProcAddress(hKernelLib,"RegisterServiceProcess");
if(RegisterServiceProcess){
RegisterServiceProcess(GetCurrentProcessId(),RSP_SIMPLE_SERVICE);
}
}
memset(&g_Account,0,sizeof(ACCOUNT));
memset(&g_NewAccount,0,sizeof(ACCOUNT));
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
return true;
}
//-----------------------------------------------------------------------
// UnInitlizeFuntions
//-----------------------------------------------------------------------
bool UnInitlize()
{
DestoryEKey();
// FreeEKeyFuns();
return true;
}
//------------------------------------------------------------------------
// WinMain Entry
//-----------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HANDLE hThead;
Initlize();
hThead = (HANDLE)_beginthread(SetTextFunProc,NULL,NULL);
OutputDebugString("Create The SetTextFunProc Thread!\n");
WaitForSingleObject(hThead,INFINITE);
CloseHandle(hThead);
OutputDebugString("Application End!\n");
UnInitlize();
return 0;
}
完整的VC工程可来信索取.
borland@163.com