¿La simulación de una pulsación de tecla con PostMessage solo funciona en algunas aplicaciones?

Mi enfoque a este problema resultó ser correcto solo en varios programas. ¿Por qué no es universal?

Funciona bien en:

Firefox
Editor de texto de Visual Studio

Desafortunadamente, en algunos casos no pasa nada (incluso si hago clic en un área de cuadro de texto antes de ejecutar mi programa):

Google ChromeBloc

GetLastError devuelve siempre 0, incluso utilizando SendMessage en lugar de PostMessage. ¿Podría señalar mi error?

#include <Windows.h>
#include <iostream>

int main()
{
    HWND hCurrentWindow;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++) //simulate 500 keystrokes of 'E'.
        {
            PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,NULL);
            PostMessage(hCurrentWindow,WM_KEYUP,0x45,NULL);
        }

    std::cout<<GetLastError()<<std::endl;

    system("Pause");
    return 0;
}

ACTUALIZACIÓN después de la sugestión Maximus

#include <Windows.h>
#include <iostream>

int main()
{
    HWND hCurrentWindow;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    if(!hCurrentWindow)
        std::cout<<"Failed get set the window handle\n";

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++)
        {
            PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,0x45);
            PostMessage(hCurrentWindow,WM_KEYUP,0x45,0x45);
        }

    std::cout<<GetLastError()<<std::endl;

    system("Pause");
    return 0;
}

No hay diferencia en efecto.

ACTUALIZACIÓN después del comentario de Rob Kennedy y la respuesta de Hans Passant

#include <Windows.h>
#include <iostream>

int main()
{
    HWND hCurrentWindow;
    DWORD procID;
    GUITHREADINFO currentWindowGuiThreadInfo;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    if(!hCurrentWindow)
        std::cout<<"Failed get main the window handle\n";

    GetWindowThreadProcessId(hCurrentWindow,&procID); 
    GetGUIThreadInfo(procID,&currentWindowGuiThreadInfo);               
    hCurrentWindow = currentWindowGuiThreadInfo.hwndFocus;

    if(!hCurrentWindow)
        std::cout<<"Failed get the child window handle\n";

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++)
        {
            PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
            PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
        }

    std::cout<<GetLastError()<<std::endl;

    system("Pause");
    return 0;
}

Ahora, los mensajes "transparentes" son enviados cada vez. GetLastError () dice:

ERROR_INVALID_WINDOW_HANDLE

1400 (0x578)

Invalid window handle.

GetLastError () "corregido"

int main()
{
    HWND hCurrentWindow;
    DWORD procID;
    GUITHREADINFO currentWindowGuiThreadInfo;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    if(!hCurrentWindow)
        std::cout<<"Failed get main the window handle\n";

    GetWindowThreadProcessId(hCurrentWindow,&procID); 
    GetGUIThreadInfo(procID,&currentWindowGuiThreadInfo);               
    hCurrentWindow = currentWindowGuiThreadInfo.hwndFocus;

    if(!hCurrentWindow)
        std::cout<<"Failed get the child window handle\n";

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++)
        {

            if(!PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC))) std::cout<<GetLastError()<<std::endl;
            if(!PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC)))   std::cout<<GetLastError()<<std::endl;
        }



    system("Pause");
    return 0;
}

... salidas1400 Mil veces. Excepto esto, nada ha cambiado.

Respuestas a la pregunta(4)

Su respuesta a la pregunta