Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3027

Top-level window frozen in a short period of time?

$
0
0
Code:

#include <Windows.h>
#include <tchar.h>

#include <iostream>
#include <sstream>
#include <stdio.h>
#include "atlbase.h"

using namespace std; 

#define WIN32_LEAN_AND_MEAN


bool InitMainWindow(HINSTANCE, int);
HDESK CreateHiddenDesktop(CHAR *desktop_name);
void QueryRegister();
LRESULT CALLBACK MsgProc(HWND, UINT, WPARAM, LPARAM);

HDESK _original_desktop, _hidden_desktop;
HWND vdesktop = NULL;
HINSTANCE gInstance = NULL;
FILE* consoleWin = NULL;

HWND CreateFullscreenWindow(HWND parent)
{
 HMONITOR hmon = MonitorFromWindow(parent,
                                  MONITOR_DEFAULTTONEAREST);
 MONITORINFO mi = { sizeof(mi) };
 if (!GetMonitorInfo(hmon, &mi)) return NULL;
 return CreateWindow(TEXT("xp"),
      TEXT("xp"),
      WS_POPUP | WS_VISIBLE,
          mi.rcMonitor.left,
      mi.rcMonitor.top,
      mi.rcMonitor.right - mi.rcMonitor.left,
      mi.rcMonitor.bottom - mi.rcMonitor.top,
      parent, NULL, GetModuleHandle(NULL), 0);
}

BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
{
 
  if (hwnd == (HWND)lParam)
  {
          printf("Found Virtual Desktop Window\n");
          CHAR explorer_path[MAX_PATH];       
          ExpandEnvironmentStringsA("C:\\explorer\\explorer7.exe", explorer_path, MAX_PATH - 1);               

       

          STARTUPINFOA startup_info = { 0 };
          PROCESS_INFORMATION process_info = { 0 };
          startup_info.cb = sizeof(startup_info);
          startup_info.lpDesktop = "xp_desktop";

          SECURITY_ATTRIBUTES sa;
          SECURITY_DESCRIPTOR sd;
         

          // need access to kernel, so needing some system I/Os
          CreateProcessA(explorer_path, explorer_path, NULL, NULL, FALSE,
                        0, NULL, NULL, &startup_info, &process_info);                               

          SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );

          return FALSE;
  }

  return TRUE;
}

 
//int APIENTRY _tWinMain(HINSTANCE hInstance,
 //                    HINSTANCE hPrevInstance,
 //                    LPTSTR    lpCmdLine,
  //                  int      nCmdShow)
int main()
{
       
        AllocConsole();
        consoleWin = freopen("CONOUT$", "w", stdout);

        //gInstance = hInstance;
   
    _hidden_desktop = CreateHiddenDesktop("xp_desktop");
    _original_desktop = GetThreadDesktop(GetCurrentThreadId());
       

    printf("Entering hidden desktop\n");
       
       
       
    //SetThreadDesktop(_hidden_desktop);         
        //SwitchDesktop(_hidden_desktop);

                if (RegisterHotKey(NULL, 1, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, '1')
        &&        RegisterHotKey(NULL, 2, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, '2')       
        && RegisterHotKey(NULL, 4, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, 'E'))
        {
                MSG msg = {0};
                while (GetMessage(&msg, NULL, 0, 0) > 0) {
                        if (msg.message == WM_QUIT)
                                break;
                       
                        if(msg.message == WM_HOTKEY)
            {
                               
                                if (msg.wParam == 1)
                                {
                                        printf("Switching to Desktop 1\n");
                                        SetThreadDesktop(_original_desktop);
                                        SwitchDesktop(_original_desktop);
                                }
                                else if (msg.wParam == 2)
                                {
                                       
                                        printf("Switching to Desktop 2\n");
                                        SetThreadDesktop(_hidden_desktop);
                                        SwitchDesktop(_hidden_desktop);
                                       
                                }
                               
                                else if (msg.wParam == 4)

                                {
                                        printf("Exiting\n");
                                        break;
                                }               
            }                       

                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                }
        }
 
        printf("Oops\n");
        CloseHandle(_hidden_desktop);
    getchar();       
        fclose(consoleWin);
}

 
LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        PAINTSTRUCT ps;               
        HDC hdc;
        std::string wp, lp;


        stringstream oss;
        oss << msg << endl;

       
        cout << oss.str()  << endl;
        OutputDebugStringA(oss.str().c_str());
       
        switch(msg)
        {       
               
                case WM_PAINT:
                {
                        hdc = BeginPaint(hwnd, &ps);                       
                        // Paint the wallpaper here
                        //PaintDesktop(hdc);

                        EndPaint(hwnd, &ps);
                        return 0;
                }
                case WM_CHAR:
                {
                        switch(wParam)
                        {
                        case VK_F1:
                                printf("Switching to original desktop\n");
                                SwitchDesktop(_original_desktop);
                                return 0;
                        //case VK_ESCAPE:
                        //        PostQuitMessage(0);
                        //        return 0;
                        }
                    return 0;
                }
        }
       
       
       
        // has paint stuff
        return DefWindowProcW(hwnd, msg, wParam, lParam);
}


//Create a new desktop or open an existing one
HDESK CreateHiddenDesktop(CHAR *desktop_name)
{
        CHAR explorer_path[MAX_PATH];       
        HDESK hidden_desktop, original_desktop;
        STARTUPINFOA startup_info = { 0 };
        PROCESS_INFORMATION process_info = { 0 };                         

                 
               
        hidden_desktop = OpenDesktopA(desktop_name, NULL, FALSE, MAXIMUM_ALLOWED);
        if (!hidden_desktop)
        {

                // this desktop belongs to root explorer
                hidden_desktop = CreateDesktopA(desktop_name, NULL, NULL, 0, MAXIMUM_ALLOWED , NULL);
                if (hidden_desktop)
                {
                        original_desktop = GetThreadDesktop(GetCurrentThreadId());                       
                               
                        // set the current process to windows station0
                        // set the thread to hidden desktop

                        if (SetThreadDesktop(hidden_desktop))
                        {                               
                                 

                                        // Create Window Class
                                        WNDCLASSEX wcex = { 0 };
                                        wcex.cbSize = sizeof(wcex);
                                        wcex.style = CS_HREDRAW | CS_VREDRAW;
                                        wcex.cbClsExtra = 0;
                                        wcex.cbWndExtra = 0;
                                        wcex.lpfnWndProc = MsgProc;
                                        wcex.hInstance = GetModuleHandle(NULL);
                                        wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
                                        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
                                        wcex.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
                                        wcex.lpszClassName = _T("xp");
                                        wcex.hIconSm = LoadIcon(NULL, IDI_WINLOGO);


                                        // The desktop cannot get messages from the primary desktop


                                        // Register Window Class
                                        if (!RegisterClassEx(&wcex))
                                        {
                                                printf("Register Failed\n");
                                                return false;                                         
                                        }
                                        else
                                        {
                                                printf("Register Succeeded\n");
                                        }

                                        // Create a window inside this virtual desktop
                                        //vdesktop = CreateWindow(L"xp_desktop", L"xp_desktop", WS_VISIBLE, 0, 0, 1280, 1024, NULL, NULL, gInstance, NULL);

                                        HWND parent = GetDesktopWindow();

                                        vdesktop = CreateFullscreenWindow(parent);
                                        if (!vdesktop)
                                        {
                                                printf("Create Window Failed\n");
                                                return false;                                       
                                        }
                                        else
                                        {
                                                printf("Create Window Succeeded\n");
                                        }

                                        // inside this window, create an explorer
                                        EnumWindows( EnumWindowsProc, ( LPARAM )vdesktop );                                       
       



#if 0

                                hidden_desktop = CreateDesktopA(desktop_name, NULL, NULL, 0, GENERIC_ALL, NULL);
                                if(hidden_desktop)
                                {
                                        original_desktop = GetThreadDesktop(GetCurrentThreadId());

                                        if(SetThreadDesktop(hidden_desktop))
                                        {
                                                startup_info.cb = sizeof(startup_info);
                                                startup_info.lpDesktop = desktop_name;

                                                //We need to create an explorer.exe in the context of the new desktop for start menu, etc
                                                CreateProcessA(explorer_path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &startup_info, &process_info);

                                                SetThreadDesktop(original_desktop);
                                        }
                                }
                                //ShowWindow(vdesktop, SW_SHOW);               

                                SetWindowLongPtr((HWND)hidden_desktop, GWL_WNDPROC, (LONG_PTR)&MsgProc);       
                                //QueryRegister();                                       
#endif                               
                                SetThreadDesktop(original_desktop);
                        }

                }
        }
       

       
        return hidden_desktop;
}

The window located inside the virtual desktop freezes very quickly.... didn't know if it enters the ghost window mode....
Anyone knows?
Thanks
Jack

Viewing all articles
Browse latest Browse all 3027

Trending Articles