looping within a wParam/notification

This is a discussion on looping within a wParam/notification within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to get the program to loop within a case, and stop if another case happens. So far it ...

  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,211
    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 09: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Also how can I stop the crashes/freezing?
    By checking the return values of the functions BEFORE you use invalid handle or null pointer?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 07:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 02:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21