Thread: Beginning threading

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Beginning threading

    Sorry for the uninformative title, but here's my problem I want to do (the example I'm providing is very similar to John the Ripper cracking utility, where you press the spacebar and it prints some data). I made a little picture of what I think my program should look like (graphic representation) here: http://img.photobucket.com/albums/v2...o1/threads.jpg

    So, the problem is that I need this data generator thread to be spinning away somewhere, while another (thread? just the parent?) is waiting for an event like keyboard input to do something else. So, I don't even know where to start on this one. Critical sections sound interesting, mutexes also, but all in all I don't know where to look for a solution to my problem. MSDN is an infinite library of information but I don't know how I would go about applying the things given. With my limited knowledge, I was able to create this very simple simple thread:
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    using namespace std;
    
    
    string s = "aaaa";
    DWORD WINAPI Generator(void *Params) {
         // This is just an example section
         // to spit out some data to query
         // or something
         int i = 0;                
         int length = s.length(); 
         
    
         while(s[i]++)
         {           
               while(s[i] == 'z' + 1) {
                     s[i] = 'a';
                     s[++i]++;
               }
               if(i == length) { break; }
               i = 0;
         }
         return 0;
    }
    int main(int argc, char* argv[]) {
        HANDLE hThread;
        DWORD dwThread;
    
        hThread = CreateThread(NULL, NULL, 
                Generator,  // Function pointer
                (void*) 0,  // Arguments 
                NULL, &dwThread);
          
    
        if (hThread) { return WaitForSingleObject(hThread,INFINITE); } 
        else { return 1; }
    }
    This just runs the function in a thread and then let's the WaitForSingleObject run infinitely until it returns. Naturally instead of this WaitForSingleObject, I want to be able to interact with this thread or something, or set up another thread which does stuff while this other thread is spinning about, and I have really, no direction for doing something like that. Any tips? It's all greatly appreciated.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Do I type something here?

    Thank you for the link! However, I think that I have fallen onto a Windows thread hook instead. This seems to me, the best way to simulate event driven programming for win32 C. But, unfortunently, I really am not very good with Windows hooks! Here is the code that I currently have, problems I encountered below:
    Code:
    #define _WIN32_WINNT 0x0400
    
    #include <windows.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    HHOOK     hKeywait; // The keyboard hook var
    string s = "aaa";   // "Bruteforce" string
    
    
    LRESULT CALLBACK FilterFunction(int nCode, WORD wParam, DWORD lParam) {
    	cout << s << endl;
    	// This is a test and it appears
    	// this proc is never actually being
    	// called. Shouldn't the hook be
    	// doing something to get us here??
    
    	if  (wParam == WM_KEYDOWN)      
    	{
    		
    	}
    	return CallNextHookEx(hKeywait, nCode, wParam, lParam);
    }
    
    
    
    DWORD WINAPI Generator(void *Params) {
    	// Function runs in a thread, creating
    	// all combo's of a string a-z for length
    	// of the global "bruteforce" variable
    	
    	int i = 0;                
    	int length = s.length();
       
    
    	while(s[i]++)
    	{          
    		while(s[i] == 'z' + 1) {
    			s[i] = 'a';
    			s[++i]++;
    		}
    		if(i == length) { break; }
    		i = 0;
    	}
    	return 0;
    }
    
    
    void MsgLoop()
    {
    	// This code spins about spitting messages
    	// everywhere. Will the WM_KEYDOWN message
    	// ever get to the FilterProc? I dunno
    	MSG message;
    	while (GetMessage(&message,NULL,0,0)) {
    		TranslateMessage( &message );
    		DispatchMessage( &message );
    	}
    }
    
    
    
    int main(int argc, char* argv[]) {
    	DWORD     dwThread;
    	HANDLE    hThread;
    	
    
    	hThread = CreateThread
    		(NULL, NULL, Generator, 
    		(void*) 0, NULL, &dwThread);
    
    
    
    
    	hKeywait = SetWindowsHookEx (
    		WH_KEYBOARD,                // Local process keyboard thread hook
    		(HOOKPROC) FilterFunction,  // "Hook proc", which confuses me
    		(HINSTANCE) NULL,           // Null, hook proc isn't in DLL mod
    		(DWORD) hThread );          // Thee local thread to hook from
    
    
    	MsgLoop();
    	UnhookWindowsHookEx(hKeywait);
    	return 0;
    }
    Well, this code might look familiar to what is described in a NewOrder article about a keylogger here: http://neworder.box.sk/newsread.php?newsid=10952 because a lot of the topics seem fairly relevent (even though my goal is hardly a keylogger here). So, my problem here, is that it does not seem we ever actually get to my filter procedure, however from what I was lead to believe (I probably read the diagram wrong) from this image: http://msdn.microsoft.com/library/en.../hooks32_1.gif is that the hook add's the FilterProc to the chain, and starts being executed and such.

    I'm probably wrong in my interpretation here, and I don't really know what I'm doing. Any help is much appreciated.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I would forget about threads and hooks - you don't need either for "even driven programming".

    gg

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I don't know, sorry, I've fallen back to the WaitForStuff series. I think that for this, I am going to have to have a (worker thread?) generating data in one place, while something else is checking the console for keypresses or "events". I think I want to use a WaitForMultipleObjects, the two objects being a handle to STDIN and another to the thread, but I do not know how to implement this. In fact, I do not even know how to do a WaitForSingleObject(hStdin, INFINITE). Like:
    Code:
    int main() {
        CreateAllTheThreadsEtc();
        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    
    
        while(WaitForSingleObject(hStdin, INFINITE) == WAIT_OBJECT_0) {
            cout << somedata << endl;
        }
        return 0;
    }
    Just continuously cout's stuff, without waiting. Maybe I do not understand the concept of something being signaled? If I banged my head on the keyboard would it be signaled? While I was looking around, I found this (win32 getch() thing) http://faq.cprogramming.com/cgi-bin/...&id=1043284392 but I think I kind of failed at adopting the techniques to here.

    At this point what I need to try to do, is

    1) Do "stuff" when the program finds that the console has recieved input
    2) (Less important) find a way to stop doing step 1 when hThread finishes

    Thanks again

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> but I think I kind of failed at adopting the techniques to here.
    Code:
    #include <iostream>
    using namespace std;
    
    #include <conio.h> // for getch(), kbhit()
    #include <windows.h> // for Sleep()
    
    void key_pressed()
    {
        int c = getch();
        cout << "Key pressed: " << char(c) << endl;
    }//key_pressed
    
    void do_other_stuff()
    {
        Sleep(0);
    }//do_other_stuff
    
    int main()
    {
        while (1)
        {
            if (kbhit())
                key_pressed();
            else
                do_other_stuff();
        }//while
    
        return 0;
    }//main
    For learning windows programming (including coverage of multi-threading): http://www.microsoft.com/mspress/books/toc/2344.asp

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 05-13-2006, 09:28 PM
  2. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. Problem with threading
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2004, 12:41 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM