Thread: Program not closing

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Program not closing

    I've got this tiny little program that's intended to do nothing but power off my monitor.

    Code:
    #include <Windows.h>
    
    int main() {         
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
    return 0;
    }
    It works, as in it compiles and when I double-click on the exe file, my monitor turns off but the computer stays on. The problem is, when I move my mouse or press a button to turn the monitor back on, there's a black console window in the middle of the screen that I have to manually close. What I would expect to happen is that the program runs, shutting off the monitor, and then exits. That doesn't seem to be happening.

    Any advice?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by MSDN
    Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.

    To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
    It could be that you don't have control back yet in your program for it to exit. Here is the MSDN link.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    I suppose it could also be visual studio's implicit inclusion of it's system("pause") like code. Is the console window blank or does it say something like "Press any key to continue"?
    You could also try adding some text output after the SendMessage call to see if your programs gets that far.
    And if you dont want the console window at all you can change your project type from console application to win32 application.
    All this assuming you are using visual studio..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcar keep closing my program
    By inmaterichard in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:15 PM
  2. Closing a program - what happens
    By C+/- in forum C++ Programming
    Replies: 9
    Last Post: 01-12-2007, 12:21 PM
  3. Closing program
    By DeepFyre in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 06:22 PM
  4. Closing a program -help
    By Unregistered in forum C Programming
    Replies: 20
    Last Post: 04-22-2002, 11:06 AM
  5. How to restart a program without closing it...
    By D4050 in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2001, 12:38 PM