Receiving a notification after resuming from sleep state

This is a discussion on Receiving a notification after resuming from sleep state within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm looking for a way of launching a small executable every time a computer resumes from sleep state (this ...

  1. #1
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404

    Receiving a notification after resuming from sleep state

    Hi,

    I'm looking for a way of launching a small executable every time a computer resumes from sleep state (this is for a laptop, i want to do some stuff every time the lid is open and every time the computer is started). I know how to add a program for it to be run every time windows start, but i'm completely lost on if there's a way for a program to be notified when the computer resumes from the sleep state or some things like that.

    I did some search on the internet, but it was unsuccessful.

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Check out WM_POWERBROADCAST on MSDN. Which means you'll need to have a app running to recieve the message in the first place.

    gg

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Thanks. I got something working.

    I have another question. I really don't know much about Windows programming in general, and this being said, the only way i managed to make my program work is by creating a window (that i never show) so it can handle the WM_POWERBROADCAST message (else, i wasn't able to receive the message (or maybe i just did something wrong )). But this is rather ugly, and i would prefer not creating a window (even if we don't see it). So... is there something easy to do about this ?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Nope. That's just how Windows works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Well - you don't need a window to have a message queue. And even though the documentation says "A window receives this message through its WindowProc function" - the message may just show up in a message queue not associated with any windows. It's worth a quick try anyways...
    Code:
        MSG msg;
        BOOL bRet;
        for (;;)
        {
            bRet = GetMessage(&msg, 0, 0, 0);
            if ((bRet == 0) || (bRet == -1))
                break;
    
            // check message here
    
        }//while

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    It might just work that way since you would get it before it's send to the window proc...
    The actual message should be in the msg variable, in the MSG struct.
    It doesn't hurt to try.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Indeed, in my first try, i wasn't creating any windows and i wasn't either receiving the message. Maybe i did some errors while writing the code. But it was looking like

    Code:
    // ...
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
       if (msg.message == WM_POWERBROADCAST)
       {
          if (msg.wParam == PBT_APMRESUMESUSPEND)
          {
             MessageBox(...);
          }
       }
    }
    Never received the notification after re-opening the lid of my laptop. The other version where i create a window does work.

    But i might retry eventually. I'll give some feedback about it.

    And i read this on MSDN: "Because the system directs messages to individual windows in an application, a thread must create at least one window before starting its message loop.". Don't know what we can conclude from it.

    Anyway. Thanks for the help.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    From what I understand, Windows creates and sends messages to windows. So if you don't have a window, no message is created and sent to your application.
    If you do create a window, however, it will automatically begin sending notifications to your window. You could actually intercept them in your message loop after creating the window if you want, but you could just as well use the actual window proc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input/switch statement issues
    By peanut in forum C Programming
    Replies: 5
    Last Post: 10-27-2006, 02:58 PM
  2. receiving notification when user runs a program
    By hanhao in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2006, 10:23 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  4. Client not receiving FD_CLOSE notification message?
    By dp_76 in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-22-2005, 11:08 PM
  5. Function pointer question
    By sbayeta in forum C Programming
    Replies: 9
    Last Post: 08-06-2004, 08:15 AM

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