Thread: Resetting Windows Desktop

  1. #1
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59

    Resetting Windows Desktop

    I have a legacy application that, on occasion, causes the windows fonts to screw up. I have found that, if I simply reset the desktop, it fixes the problem. I do not have the authority, knowledge, energy, nor desire to try and rewrite the legacy app.
    With that in mind, I have gotten the following code to launch the desktop manager to the point that I need it to be. I just need to send the enter key to the opened window and it should do what I need.
    Any hints on how I can get the desk.cpl to think the user has hit the enter key? AS you can see, I have tried the PostMessage function but that is not working.


    Code:
    void Reset( )
    {
     	HWND hwndDskTop;
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
    	char cmd[255];
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );
    			hwndDskTop = GetDesktopWindow(); 
    			SendMessage(hwndDskTop, WM_FONTCHANGE, 0, 0);
    			strcpy(cmd,"control.exe  desk.cpl desk,@Themes /Action:OpenTheme /File:");
    			strcat(cmd,"\"C:\\WINDOWS\\Resources\\Themes\\Windows Classic.theme\"");
    			if( !CreateProcess( NULL,				// No module name (use command line). 
    					cmd,	 
    					NULL,								// Process handle not inheritable. 
    					NULL,								// Thread handle not inheritable. 
    					FALSE,								// Set handle inheritance to FALSE. 
    					0,									// No creation flags. 
    					NULL,								// Use parent's environment block. 
    					NULL,								// Use parent's starting directory. 
    					&si,								// Pointer to STARTUPINFO structure.
    					&pi )								// Pointer to PROCESS_INFORMATION structure.
    				) 
    				{
    					MessageBox(NULL, "Reset has FAILED.","Error",MB_OK);
    				}
    			PostMessage (NULL, WM_KEYDOWN, VK_RETURN, 0);
        // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );
    
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I'd imagine that it probably isn't encouraged, but if the 'Display' window has keyboard focus, then keybd_event( VK_RETURN, MapVirtualKey( VK_RETURN, 0 ), NULL, NULL ) should work.

    Note that you'll also have to call it with the third argument as KEYEVENTF_KEYUP after you want it to stop being pressed.

  3. #3
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    That doesn't seem to work. I know the window has keyboard focus because it responds to both the escape and enter keys when I press them.
    Last edited by JustMax; 04-25-2009 at 07:09 AM.

  4. #4
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    DeadPlanet,
    I added this line between the CreateProcess and the keybd_event :
    Code:
    WaitForSingleObject( pi.hProcess, (DWORD)5000 );
    and it appears to be working. Thanks for the help.

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    This isn't going to be distributed to anyone but yourself is it?

  6. #6
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    It will placed in an application that calls the old app. It will reset the desktop when the old app closes and returns control to the new app.

    Do you see an issue I am missing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM