[fpc-pascal] Windows API SendMessage()

2017-08-09 Thread James Richters
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(1);

  Writeln('Turning On Display');

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

  Writeln('Display is On');

End.

 

James

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Windows API SendMessage()

2017-08-09 Thread 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(1);

   Writeln('Turning On Display');

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

   Writeln('Display is On');

End.

  


James




___
fpc-pascal maillist  -  fpc-pascal@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


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal