Here is the complete source code for my, uh, C++ desktop organizer program for windows.

Code:
#include <windows.h>
#include <iostream>

using namespace std;
 
 void SetActiveWND(HWND WND)
 {
        AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(WND,NULL),true);
        SetActiveWindow(WND);
        AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(WND,NULL),false);
        }

 int main()

 {  RegisterHotKey(NULL,1,MOD_ALT ,VK_F3);
    RegisterHotKey(NULL,2,MOD_ALT ,VK_F2);
    MSG msg = {0};
	HWND * hwnd,*temp,next;
	char i=-1,size,TEMP;
	size=10;
	hwnd=new HWND[size];    
	
    while (GetMessage(&msg, NULL, 0, 0) != 0)
    {   if(msg.lParam==7471105)
        {
             if(i>=(size-1))
             {
             temp=new HWND[size];
             for(TEMP=0;TEMP<size;TEMP++)
                  {temp[TEMP]=hwnd[TEMP];}
             delete [] hwnd;
             size+=5;
             hwnd=new HWND[size];
             TEMP=size-5;
             for(TEMP=0;TEMP<size;TEMP++)
                  {hwnd[TEMP]=temp[TEMP];}
             delete [] temp;}
                               
        i++;
        hwnd[i]=GetForegroundWindow();
        next=GetWindow(hwnd[i],GW_HWNDNEXT);
        ShowWindow(hwnd[i],SW_HIDE);
        SetActiveWND(next);                            }
        if((msg.lParam==7405569)&&(i>=0))
        {ShowWindow(hwnd[i],SW_SHOWDEFAULT);
        SetActiveWND(hwnd[i]);
        i--;}
    
    } 
    UnregisterHotKey(NULL,1);
    UnregisterHotKey(NULL,2);
    return 0;
}
The part I want to focus on is the function SetActiveWND() at the top of my code. According to http://msdn.microsoft.com/en-us/libr...11(VS.85).aspx,
The SetActiveWindow function activates a window. The window must be attached to the calling thread's message queue.
I have done that with the function AttachThreadInput(http://msdn.microsoft.com/en-us/library/cc429027.aspx) that is mentioned by the MSDN documentation. However, if you compile the source, the SetActiveWND() always fails, and the window that gets unhidden is never set as the active window, just the foreground window. Any advice?