Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows C program perform action on shutdown

Tags:

c

windows

I am making a program that sends "heartbeats" for a server to keep track of nodes. They are packets with the following payloads:

  • 'start' when it starts up

  • 'running' every 5 seconds

  • 'stopping' at shutdown

The first two are easy. The thread/loop can set the message on first and subsequent runs. How do I make the loop "catch" a shutdown so that it can send a last packet?

I use the minGW compiler for C in WinXP platform.

Edit: I added the relevant details I missed (thanks walkingTarget and Ferruccio)

  • It is an in-progress app that contains messy stuff in implementation :-)
  • It uses libCURL, the HTTP client library to send the packets
  • It is a console app, which I (much later) intend as a service
  • It needs to save a file and send a packet at shutdown
  • It needs to capture a system shutdown
like image 848
Jesvin Jose Avatar asked May 23 '26 12:05

Jesvin Jose


1 Answers

In your WindowProc() you can check for the message WM_QueryEndSession, which Windows sends to all open processes before shutting down. For more info on that message see the following link, but i understand that it is as trivial as checking for a WM_SIZE:

http://msdn.microsoft.com/en-us/library/aa376890%28VS.85%29.aspx

like image 120
Bernd Elkemann Avatar answered May 26 '26 03:05

Bernd Elkemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!