1. 問題或需求描述:
使用 outlook 2010遇到沒法保存密碼自動登陸的狀況(又或者是,想實現用資源管理器快速打開ftp空間等等,本文對你或許有所幫助)編程
2.解決方法或原理:
使用C/C++語言編程實現,核心是消息模擬,用消息(Message)來模擬用戶正常的按鍵與鼠標操做。api
3.開發環境:
WIN10 + VS2017安全
4.主要源碼:ide
C/C++代碼:code
#include "stdafx.h" #include "MailLogger.h" #include <Shellapi.h> BOOL CALLBACK MyEnumChildFunc(HWND _hwnd, LPARAM _lParam); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { HWND hWnd = NULL; unsigned iTime = 0; //運行 outlook ShellExecute(NULL, L"open", L"C:\\Program Files\\Microsoft Office\\Office14\\OUTLOOK.EXE", NULL, NULL, SW_SHOW); //30秒內進行受權登錄 while (iTime < 60) { hWnd = FindWindow(L"#32770", L"Windows 安全"); if (hWnd != NULL) break; iTime++; Sleep(500); } if (hWnd == NULL) return 1; Sleep(500); EnumChildWindows(hWnd, MyEnumChildFunc, NULL); return 0; } VOID FillPwd(HWND _hwnd) { char pwd[32] = "password"; PostMessage(_hwnd, WM_SETFOCUS, (WPARAM)0x00000000, (LPARAM)0x00000000); //Set focus PostMessage(_hwnd, WM_KEYDOWN, (WPARAM)0x00000024, (LPARAM)0x01470001); //home key PostMessage(_hwnd, WM_KEYUP, (WPARAM)0x00000024, (LPARAM)0xC1470001); //足夠多的Del以清空原有內容(多是過時或失效的密碼) for (int i=0; i<32; i++) { PostMessage(_hwnd, WM_SETFOCUS, (WPARAM)0x00000000, (LPARAM)0x00000000); //Set focus PostMessage(_hwnd, WM_KEYDOWN, (WPARAM)0x0000002E, (LPARAM)0x01530001); //del key PostMessage(_hwnd, WM_KEYUP, (WPARAM )0x0000002E, (LPARAM)0xC1530001); Sleep(5); } for (int i=0; i< strlen(pwd); i++) { PostMessage(_hwnd, WM_SETFOCUS, (WPARAM)0x00000000, (LPARAM)0x00000000); //Reset focus PostMessage(_hwnd, WM_CHAR, (WPARAM)(pwd[i] & 0xFF), (LPARAM)0x00000000); Sleep(5); } PostMessage(_hwnd, WM_CHAR, (WPARAM) 0x0000000D, (LPARAM)0x011C0001); //enter key } BOOL CALLBACK MyEnumChildFunc(HWND _hwnd, LPARAM _lParam) { TCHAR szWindowClass[256], szWindowName[256]; LONG windowStyle; GetWindowText(_hwnd, szWindowName, 256); GetClassName(_hwnd, szWindowClass, 256); if (wcscmp(szWindowClass, L"Edit") == 0 && wcscmp(szWindowName, L"") == 0) { windowStyle = GetWindowLong(_hwnd, GWL_STYLE); if (windowStyle & ES_PASSWORD) { FillPwd(_hwnd); } } return TRUE; }