[fpc-pascal] Windows API SendMessage()

Cyrax evvke at hotmail.com
Thu Aug 10 06:07:25 CEST 2017


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  -  fpc-pascal at lists.freepascal.org
> 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




More information about the fpc-pascal mailing list