CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2009
    Posts
    11

    Exiting programs

    Hello! Just having a little trouble making a program right now

    I've made a basic Win32 app so far, and i wan't it to close a program when i push a certain key. I made it a console app at first and tested that and it did work, but transitioning to the Win32 app is causing me trouble. I've put my code in the APIENTRY _tWinMain function which i think is the equivalent to the consoles init main function. Heres my code:

    Code:
    Torrent = FindWindow(NULL, _T("Vuze"));
    
    	
    	if (KEYDOWN(VK_HOME))
    	{
           if( Torrent = NULL )
    	   {
    		   ShellExecute(NULL, L"open", L"C:\\Documents and Settings\\Home\\Desktop\\Vuze\\Azureus.exe", NULL, L"C:\\", SW_SHOWNORMAL);
    	   }
    	   else
    	   {
    		   SendMessage(Torrent, WM_CLOSE, 0, 0);
    	   }
    	}
    and here is my little definition for the keydown if it helps thats in my stdafx.h file
    Code:
    #define KEYDOWN(vk_code)  ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    It seems to run and compile fine, but i never see my program close. I used that exact code (except the key part) for my console and it worked. I have my handle at the top if your wondering where that is for the name Torrent. Is there anything i'm doing wrong here?

    Lastly if your wondering why i am doing this, i have a phone that runs VOIP and my main PC with Media Center. I'm watching TV, the phone rings, and it takes me 30 seconds to turn off the torrents so i can hear what the people on the phone are saying. So i'm making life easier for myself

    Thank you for your help!

    -Dakiato

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Exiting programs

    Code:
    if( Torrent = NULL )
    it should be:
    Code:
    if( Torrent == NULL )
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    May 2009
    Posts
    11

    Re: Exiting programs

    Ah didn't see that. Thanks for your help cilu!

  4. #4
    Join Date
    May 2009
    Posts
    11

    Re: Exiting programs

    Still seem to be having a problem. It won't detect that the key is down. I added a breakpoint that outputs a message to the debug screen but it never gets past the if KEYDOWN part. I've seen on few other forums that the keydown doesn't get called if dialogs or buttons are on screen, which mine has. Is there any other way to detect a key input? Thanks for your help.

    -Dak

  5. #5
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Exiting programs

    Since Win32 programs are message driven unlike console applications, you could try and handle the WM_KEYDOWN message in your main window procedure.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  6. #6
    Join Date
    May 2009
    Posts
    11

    Re: Exiting programs

    Yup that was the problem! Thanks so much for helping a beginner

    As i always do to help other people out heres my source:

    NOTE: I placed this in the LRESULT CALLBACK WndProc function
    Code:
    case WM_KEYDOWN:
    		if (KEYDOWN(VK_HOME))
    	{
           if( Torrent == NULL )
    	   {
    		   ShellExecute(NULL, L"open", L"C:\\Documents and Settings\\Home\\Desktop\\Vuze\\Azureus.exe", NULL, L"C:\\", SW_SHOWNORMAL);
    	   }
    	   else
    	   {
    		   SendMessage(Torrent, WM_CLOSE, 0, 0);
    	   }
    	}
        break;
    EDIT: One last question, is there anyway to make this so this is checked even when it is not the active window? Once that is finished everything should be great.

    Once again thank you for your help!

    -Dak
    Last edited by dakiato; May 4th, 2009 at 08:36 PM.

  7. #7
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Exiting programs

    Key strokes is sent to the active window only.
    So could write a global keyboard hook to get keystokes in all windows.
    Check SetWindowsHookEx(WH_KEYBOARD, ....
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  8. #8
    Join Date
    May 2009
    Posts
    11

    Re: Exiting programs

    Had a look around at how to do that and it seems i need to make a DLL, which i can do, but not quite sure how to load it into the actual program. I had a look at some tutorials but most of them are really confusing and either don't explain it, or explain it really badly. Thanks again for your help!

  9. #9
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Exiting programs

    You need to load the dll using another exe.
    Once the dll is loaded it will automatically get loaded into all running processes.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  10. #10
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Exiting programs

    Explored RegisterHotkey ?

  11. #11
    Join Date
    May 2009
    Posts
    11

    Re: Exiting programs

    Ok, think i'm getting the hang of hooks, but one last question. I found this code on a Keyboard hook and just wondering, will it work since it isn't in a DLL?

    http://www.rohitab.com/discuss/lofiv...hp/t34071.html

    It seems to compile fine, but i'm not sure then how to check if a key is down using that function with the hook. I would've used the Keyboard Hook program that was a tutorial on CodeGuru but i have an Express edition which doesn't include some essential MFC header files, and the trial is HUGE for the pro VS2008. Could i put it in the Case VK_HOME: part as i use a similar one with the original key check?

  12. #12
    Join Date
    May 2009
    Posts
    11

    Re: Exiting programs

    Oh and thanks for that Kirant, it would be a lot easier, but i can't really find any examples that says how to implement it. I can find it on MSDN but it just states what it does, and not how i should call it. Still very new to this language, so i'm sorry if it seems like i can't learn.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured