Thread: This code locks up explorer. Help?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    265

    This code locks up explorer. Help?

    Code:
    ShowWindow(GetTopWindow(NULL), SW_MAXIMIZE);
    Any particular reason? Im trying to maximize the current window on top. I have a keyhook setup to execute this statement whenever i press the "windows" key (key 91). I finally found a good use for that little bastard. As soon as some working code is posted on another thread, ill have to bind one to ctrl alt del too =P Any help would be greatly appreciated. Thanks for your time.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You really should have GetTopWindow pass to a hwnd so that you can do error checking.

    When troubleshooting you should really unnest your commands.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Im not a windows coder. I dont like the windows API. Please explain to somebody from that point of view what the problem is and how to fix it.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    HWND hTopWindow;
    int iError;
    TCHAR szBuffer[20];
    .......
    hTopWindow = GetTopWindow(NULL);
    
    if (hTopWindow == NULL)
      {
      wsprintf(szBuffer, TEXT("Error with GetTopWindow().  Error Number %i"), GetLastError() );
      MessageBox (NULL, szBuffer, TEXT("ERROR"), MB_ICONERROR);
      }
    
    else
      ShowWindow (hTopWindow, SW_MAXIMIZE);
    
    ......
    or something like that.

    edit: The problem could be many things. Which is why you need to un-nest (what I call it) your commands. Having GetTopWindow() seperate from ShowWindow() allows you to stop the code if there was an error. If there is no error then you have norrowed down the problem.
    Last edited by Thantos; 08-29-2003 at 07:03 PM.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Just tried that, the hwnd returned by gettopwindow is not null, however explorer still locks up when that showwindow command to maximize it executes.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Post your code please.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    My code is far too large to post, however the relivant function goes like this:

    Code:
    LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam )
    {
       HDC	       hDC;
       HWND        hwndTopWindow;
       int iError;
       TCHAR szBuffer[20];
    
       int x;
       int i = 0 ;
       x = wParam;
    
       if ( nCode >= 0 ) {
    
          if ( nCode == HC_NOREMOVE )
    		  strcpy(szType, "NOT Removed from Queue");
          else
    		 strcpy(szType, "REMOVED from Queue                                             ");
    
          wsprintf((LPSTR)szFilterLine[KEYBOARDINDEX],
    	       "KEYBOARD\tKey:%d\t%s",wParam,(LPSTR)szType);
    
    	if(x==commandkey&&!executing)
    	{
    		executing = 1;
    		executions++;
    		if(executions%2)
    		{
    			executing = 1;
    			
    			hwndTopWindow = GetTopWindow(NULL);
    			if(hwndTopWindow == NULL)
    			{
    				wsprintf(szBuffer, TEXT("Error with GetTopWindow().  Error Number %i"), GetLastError() );
    				MessageBox (NULL, szBuffer, TEXT("ERROR"), MB_ICONERROR);
    
    			}else{
    				ShowWindow(hwndTopWindow, SW_MAXIMIZE);
    			}
    			
    			executing = 0;
    			//We return here so that the command character 220 "\" isnt seen as pressed.
    			return 1;
    		}
    		executing = 0;
    	}
    
          hDC = GetDC(hwndMain);
          TabbedTextOut(hDC, 1, nLineHeight * KEYBOARDINDEX,
    	    (LPSTR)szFilterLine[KEYBOARDINDEX],
    	    strlen(szFilterLine[KEYBOARDINDEX]), 0, NULL, 1);
          ReleaseDC(hwndMain, hDC);
       }
    
       // If the message isnt for us, just peek at it and pass it on.
       //
       return( CallNextHookEx(hhookHooks[KEYBOARDINDEX], nCode, wParam, lParam));
    }
    Some of it is borrowed, other parts are poorly writen, and yes it uses global variables in parts. This code was for a personal utility and never meant to be seen by others so its not very nice. Thanks for your time.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    You shouldn't be doing all that code in your keyboard hook. Send yourself a message and do your code in there so you can pass the hook straight through with minimal disruption.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    As i stated earlyer, im not a windows coder. Im doing this the simplest way i know how. All i want to know, is why does ShowWindow(hwndTopWindow, SW_MAXIMIZE); fry my explorer process.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I looked at the code and tried it out. It didn't lock my explorer per say. Thought the response was screwy because it didn't maximize the only window I could see, it maximized another window handle. As such I think GetTopWindow (NULL) is not returning the window handle you want.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Any thoughts about how to get a better window handle? I think the probem is its using a window handle, but not a visible one. Like its trying to maximize the desktops handle or something. Explorer goes into an infinite loop eating 99% of the CPU.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I would suspect it's crashing because you are executing those calls in a system-wide hook procedure and not in the context of your own message loop.

    Try this:

    Code:
    LRESULT __stdcall LowLevelKeyboardProc (DWORD code, WPARAM wparam, LPARAM lparam)
    {
       if (code == HC_ACTION) /* A key was pressed */
       {
          KBDLLHOOKSTRUCT *hookstruct = (KBDLLHOOKSTRUCT *)lparam;
          /* Send key to our main program */
          PostMessage (hWnd, WM_U_HOOKMSG, (hookstruct->vkCode << 16) + hookstruct->scanCode, hookstruct->flags);
       }
       return (CallNextHookEx (hookHandle, code, wparam, lparam));
    }
    And then process WM_U_HOOKMSG in WndProc, rather than in the hook.

    WM_U_HOOKMSG is a userdefined message as follows:

    #define WM_U_HOOKMSG (WM_USER+0x1000)

    I know your not a Windows coder, but you should be able to get that working quite easily.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM