Thread: looping within a wParam/notification

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    looping within a wParam/notification

    I'm trying to get the program to loop within a case, and stop if another case happens. So far it works ok for a couple seconds, then it freezes and crashes. It also freezes (not responding) if I click away from notepad, and try to focus my program again. All it does for now is alternate between typing '2' and '3' in notepad. I don't how to go about fixing it so the program does not "not respond" when i try to focus it while it's typing in notepad. Using Devcpp compiler

    Code:
    case FILE_START:
    {
         HWND Handle = FindWindow(NULL, "Untitled - Notepad");
         if(SetForegroundWindow(Handle)){
              SetFocus(Handle);
                        		
              Sleep(1000);
              INPUT inputf1, inputf2;
    
              ZeroMemory(&inputf1, sizeof(INPUT));
              inputf1.type = INPUT_KEYBOARD;
              inputf1.ki.wVk = VK_NUMPAD2;
    							
              ZeroMemory(&inputf2,sizeof(INPUT));
              inputf2.type = INPUT_KEYBOARD;
              inputf2.ki.wVk = VK_NUMPAD3;
    							
              while(wParam != FILE_STOP){
                   Sleep(500);
                   SendInput(1, &inputf1, sizeof(INPUT));
                   Sleep(500);
                   SendInput(1, &inputf2, sizeof(INPUT));
              }							
         }
    }
    break;

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You don't even know what you're doing with wParam or the callback function you're in.

    Consider the following code:

    Code:
    #include <stdio.h>
    
    void dosomething(int);
    
    int main(void)
    {
    	dosomething(5);
    	dosomething(10);
    
    	return 0;
    }
    
    void dosomething(int x)
    {
    	while(x != 10)
    	{
    		/* Wait until x is 10. */
    	}
    }
    If the above program makes sense to you and you think it should work, you need to review basic C (or C++). If you understand that it's totally absurd, then look at your own code and realize you're doing the same thing.

    Edit: What are you even doing with FindWindow()? *sigh*
    Last edited by MacGyver; 06-12-2007 at 08:59 PM.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I see where I went wrong, but how can I make the program loop the SendInput()'s until a FILE_STOP is called? Also how can I stop the crashes/freezing?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Also how can I stop the crashes/freezing?
    By checking the return values of the functions BEFORE you use invalid handle or null pointer?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while(wParam != FILE_STOP)
    Maybe
    if(wParam != FILE_STOP)

    Where are these messages coming from?


    If you want a loop like that, then you need to implement a state machine inside that code
    case FILE_START:
    - find notepad window
    - set focus
    - set 1 second timer
    case WM_TIMER:
    - is notepad still focussed?
    - send key1 then key2
    case FILE_STOP:
    - destroy timer

    Notice that none of these states make use of a while loop or sleep.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Salem View Post
    > while(wParam != FILE_STOP)
    If you want a loop like that, then you need to implement a state machine inside that code
    case FILE_START:
    - find notepad window
    - set focus
    - set 1 second timer
    case WM_TIMER:
    - is notepad still focussed?
    - send key1 then key2
    case FILE_STOP:
    - destroy timer
    Thank you, that's exactly what I needed to know. I got it working correctly now.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Another question so I don't have to start a new thread;

    The SendInput() works for sending a simulated keystroke to notepad, or the calculator, or anything else that has text for that matter, but what if i want to call a hotkey. Say in some program F9 is a hotkey for Help on the menu, and I want to call that from another program. SendInput doesn't give the same value as actually pressing the key. Is there a different function (I searched MSDN) that will do that, or is the keyboard press always going to return a different value then simulating it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM