Send Keystroke to a generic window c++ with SendInput
<p>I have a C++ console application that would to send some Keystroke to one generic window...
I'm trying to find ALL OPEN WINDOW WITH MESSAGE QUEUE (for input) with <code>EnumWindows()</code> and my callback store some information about, it has <code>GetWindowText()</code>, <code>GetWindowThreadProcessId()</code> and <code>PostThreadMessage()</code> (to check if has Message queue, with <code>WM_NULL</code> message) and also its <code>HWND hWnd</code> ... <strong>[code below]</strong></p>

<p><strong>Hints:</strong> Is this correct or I should use <strong>also</strong> <code>EnumChildWindows()</code> ?</p>

<p><strong>continue...</strong></p>

<p>I wish to send keystroke for example <code>ALT+F4</code> or <code>CTRL+T</code> etc. to one of the windows ...</p>

<p><strong>Hints:</strong> Which is the best way to do this? <code>SendInput()</code> or <code>PostThreadMessage()</code> ?</p>

<p><strong>[HALF auto-reply]:</strong>  I'm currently trying <code>SendInput()</code>, sending a group of <code>INPUT</code> using the translation of Scan Code in VirtualKey with <code>myVKey = MapVirtualKeyEx(..., MAPVK_VSC_TO_VK, ...)</code> , for example <code>ALT</code> and <code>F4</code> is <code>56</code> and <code>62</code> as <code>int</code> type, in <code>HEX</code> is <code>38</code> and <code>3E</code> ... I make input with <code>dwFlags = 0</code> and after, in reverse order, with <code>dwFlags = KEYEVENTF_KEYUP</code>
(I need to handle only a group of command, no other like single char etc..);</p>

<p><strong>I think VKey is better than Hardware Scan Code (right?)</strong></p>

<p><strong>Third Question: Before using <code>SendInput()</code>, how I should bring the window?</strong>
Let's supposing I have all info about the window...</p>

<p>What and in which order I must use <code>SetFocus()</code>, <code>SetActiveWindow()</code> and / or <code>SetForegroundWindow()</code> ? <strong>[all are commented]</strong></p>

<p><strong>a bit of code:</strong></p>

<pre><code>bool _res = false;
_res = AttachThreadInput(GetCurrentThreadId(), targetThreadID, TRUE);
if (!_res) {
    cout &lt;&lt; "Error: " &lt;&lt; GetLastError() &lt;&lt; endl;
}
else {
    //SetFocus(focusHandler);
    //SetActiveWindow(focusHandler);
    //SetForegroundWindow(focusHandler);
enter code here
    if (!prevH) {
        cout &lt;&lt; "Error: " &lt;&lt; GetLastError() &lt;&lt; endl;
    }
    else {
    UINT res = SendInput(nTotVKey, numInputVect, sizeof(INPUT));
    if (res != nTotVKey) {
        cout &lt;&lt; "Error: " &lt;&lt; GetLastError() &lt;&lt; endl;
    }
    else {
        _res = AttachThreadInput(GetCurrentThreadId(), targetThreadID, FALSE);
        if (!_res) {
            cout &lt;&lt; "Error: " &lt;&lt; GetLastError() &lt;&lt; endl;
        }
    }
}
</code></pre>

<p>Any help would be appreciated. Thanks..</p>

<p><strong>EDIT: REAL SCENARIO</strong> Is a C/S application in which client says "I do a CTRL+W to the selected window" and Imagine a C# client that send to the server a "CTRL+W" message for Google Chrome (for example) that the server should send to its thread input queue... I need to put in foregound "Google Chrome" to send the input "CTRL+W" ..don't worry about any race condition or other.. If needed the client will send again the command...</p>