Windows API SendMessage()

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Windows API SendMessage()

Zaaphod

I’m trying to use SenMessage() to turn the display on or off under program control of a console application.    I got the function to work, and it turns off the display, but then the program just hangs. 

Does anyone know what I am doing wrong?

 

Here’s my sample program

Program Displaycontrol;

Uses Windows;

Begin

  Writeln('Turning Off Display');

  SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);      //This seems to work, turning off my display

  Writeln('Display is Off');                      // This Writeln never happens.    I need to force terminate the program.

  sleep(10000);

  Writeln('Turning On Display');

  SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);

  Writeln('Display is On');

End.

 

James


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Windows API SendMessage()

Cyrax
On 10/08/17 04:27, James Richters wrote:

> I'm trying to use SenMessage() to turn the display on or off under program
> control of a console application.    I got the function to work, and it
> turns off the display, but then the program just hangs.
>
> Does anyone know what I am doing wrong?
>
>  
>
> Here's my sample program
>
> Program Displaycontrol;
>
> Uses Windows;
>
> Begin
>
>    Writeln('Turning Off Display');
>
>    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
> //This seems to work, turning off my display
>
>    Writeln('Display is Off');                      // This Writeln never
> happens.    I need to force terminate the program.
>
>    sleep(10000);
>
>    Writeln('Turning On Display');
>
>    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
>
>    Writeln('Display is On');
>
> End.
>
>  
>
> James
>
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

You need to use PostMessage if you want to your program to work
correctly. SendMessage waits reply and you don't have a message pump in
your console application, thus a hang happens.

SendMessage :
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Windows API SendMessage()

Zaaphod
>You need to use PostMessage if you want to your program to work correctly. SendMessage waits reply and you don't have a message pump in your console application, thus a hang happens.

Thank you... PostMessage() works to turn off the display, but for some strange reason it can't turn it back on.   I set a 20 second delay, and precisely at 20 seconds my  monitor light turns green, but then the monitor goes back to sleep before the screen can be displayed.  I'm guessing the signal is being sent to turn on the monitor, but not to get windows out of power save mode...  I also  see using -1 to turn the display back on is an un-documented feature.    I can get the display back on with a keystroke or moving the mouse, but I'm wanting the program to turn the display back on without user input.  Can anyone think of any other way I could make my console application to turn the display back on without user input?  

I've tried

progname := FindWindow('displaycontrol', nil);
PostMessage(progname, WM_KEYDOWN, VK_F5, 0);

And while the key sent to the program,  it does not wake the display up.   I see other examples of simulating keyboard input, but I haven't been able to get them to work in a freepascal console application.

James


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Windows API SendMessage()

Santiago A.
El 10/08/17 a las 13:11, James Richters escribió:
>> You need to use PostMessage if you want to your program to work correctly. SendMessage waits reply and you don't have a message pump in your console application, thus a hang happens.
> Thank you... PostMessage() works to turn off the display, but for some strange reason it can't turn it back on.   I set a 20 second delay, and precisely at 20 seconds my  monitor light turns green, but then the monitor goes back to sleep before the screen can be displayed.  I'm guessing the signal is being sent to turn on the monitor, but not to get windows out of power save mode...  I also  see using -1 to turn the display back on is an un-documented feature.    I can get the display back on with a keystroke or moving the mouse, but I'm wanting the program to turn the display back on without user input.  Can anyone think of any other way I could make my console

I have found this:

https://stackoverflow.com/questions/25011141/turn-off-on-monitor-cant-turn-on

It's in c++, but it looks that since w.8, you must simulate a mouse move.

--
--------
Saludos
Santiago A.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Windows API SendMessage()

Zaaphod
>I have found this:
>
>https://stackoverflow.com/questions/25011141/turn-off-on-monitor-cant-turn-on
>
>It's in c++, but it looks that since w.8, you must simulate a mouse move.

Thanks for the info.  It got me on the right track.  It seems to depend on which method is used to simulate mouse or keyboard.
Here is what I found out:
Moving the mouse with SetCursorPos(x,y);   does not wake up the display
Moving the mouse with   Mouse_Event(MouseEventF_Move,100,100,0,0);     does wake up the display
Simulating the Keyboard with PostMessage(progname, WM_KEYDOWN, VK_F5, 0);  does not wake the display
Simulating the Keyboard with Keybd_Event(VK_Shift,1,0,0);  does wake up the display

James



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...