1. 问题或需求描述:
使用 outlook 2010遇到无法保存密码自动登录的情况(又或者是,想实现用资源管理器快速打开ftp空间等等,本文对你或许有所帮助)

2.解决方法或原理:
使用C/C++语言编程实现,核心是消息模拟,用消息(Message)来模拟用户正常的按键与鼠标操作。

3.开发环境:
WIN10 + VS2017

4.主要源码:

C/C++代码:

#include "stdafx.h"
#include "MailLogger.h"
#include 

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;
}