Detecting Signal/System Messages in Windows and Linux

I'm writing a console app which will be run in both windows and linux environment. I have now run in to a slight problem.

When I close the application the destructors of some objects are not called. So alot of data is just left in the memory.

The console app looks something like this:

   dmMain := TdmMain.Create(nil);

The run method is just a dummy which contains a loop so that the application doesnt exit.

Would it be possible to listen for system signals to the program... and when the program recieves kill signals or the user closes the console window or presses control-c?

I would be greatful if someone had two code examples of this... one for windows and one for linux.

Thank you!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Console applications do not receive messages from Windows. They are not event driven apps and have no message loop.

dmMain.Free; is the correct statement. (what is dmMain btw?. It isn't a form, is it?)

Regards, Geo

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

As nicely stated above, console applications don't receive Windows
messages. BUT:

You can AllocateWnd so that *you* can receive messages <g>.

Try that out!


svennemanAuthor Commented:
dmMain is a DataModule...

AllocateWnd isnt a delphi function from what I can see... it doesnt show up in the delphi help...

can you give me a code example.

and I would need something that listen for signals on linux aswell
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

hello svenneman, , not sure what you are asking for, some have already said that a console app is NOT in the windows message comunications, and even if you create anothe window and get it in the message comunication, it will not get any event "Messages" that the system is using for the console window (like when the user ckicks the X caption "Close" button)
I will also say that this referes to the "Windows" system and I do NOT have any knowledge of the Linix API. .  
Since the .DPR Program code page does NOT have a finalization clause, I have had to use of a couple of of alternatives, first there is the


which you can set in the DPR file to have code processed before the process is terminated

PExitSave: Pointer;

procedure MyExit;
  ExitProc := PExitSave;  // restore old proc first
// do and memory freeing or object release here

begin //  *  *  *  *  *  *  * Main Program begin
PExitSave := ExitProc;
ExitProc := @MyExit;
//other code here

 - - - - - - - - - - - - - - - - - - - - - - -

if you have the SysUtils in your uses clause then you might try the

procedure AddTerminateProc(TermProc: TTerminateProc);

to add a Procedure to the list of procedures called at termination
svennemanAuthor Commented:
will try that
svennemanAuthor Commented:
none of the above suggestions worked in an console app I'm afraid =(
I tried some things, and it seems that with a console app, if the user clicks the  X  caption Close button, you will get that standard "Lose Information" Yes, No, Dialog box, and if the user clicks Yes button, the console process is Terminated, without anything  furthur being done, or any message or event or warning to the console process. . . Here is some code that I at least got a warning if the Ctrl-Break or Ctrl-C  keys were pressed, which closes a Console App , , with the
SetConsoleCtrlHandler(@HandlerFunc, True);

program EventCondsole;

uses Windows, SysUtils;

ConStr, CommandStr: String;
hStdOut, i: Integer;
ScreenBufInfo: TConsoleScreenBufferInfo;
Cord1: TCoord;

procedure FreeAndRelease;
// I do not have anything that needs to be Freed, so I do a MessageBeep

function HandlerFunc(CtrlType: Cardinal): Bool; stdcall;
{This function should be called for any of the "Events" listed
If you set the Result to "True" then it should NOT Close}
Result := False;
case CtrlType of
  CTRL_C_EVENT: begin
    // a True will keep it from closing on a Ctrl-C key press
    Result := True;
  CTRL_CLOSE_EVENT: FreeAndRelease;
    WriteLn('I''m Gone');
  CTRL_LOGOFF_EVENT: FreeAndRelease;

procedure CStrCount(Str1: String);
WriteLn('Character Count is '+IntToStr(Length(Str1)));

begin // / / MAIN BEGIN / /
hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTitle('The Command Console');
SetConsoleCtrlHandler(@HandlerFunc, True);
WriteLn('Hello to you');
WriteLn(' ');
WriteLn('Commands for this are "quit", "exit", "Clear", "Count" and "Repeat"');
WriteLn('Type in the command "quit"  or "exit" to exit this program.');
WriteLn('Type in the command "Clear" to Clear the text from this console');
WriteLn('Type in "Count" and then a space, and then the charaters you want to count');
WriteLn('Type in "Repeat" and then a space, and the charaters you want to be displayed');

while True do
  if (LowerCase(ConStr) = 'quit') or
    (LowerCase(ConStr) = 'exit') then Break;
  CommandStr := Copy(ConStr,1,6);
  CommandStr := LowerCase(CommandStr);
  if CommandStr = 'repeat' then
    CommandStr := Copy(ConStr,8,512);

  Delete(CommandStr,6, 1);
  if CommandStr = 'count' then
    CommandStr := Copy(ConStr,7,512);
    end else
  if CommandStr = 'clear' then
    GetConsoleScreenBufferInfo(hStdOut, ScreenBufInfo);
    for i := 0 to ScreenBufInfo.dwSize.Y do
    Cord1.X := 0;
    Cord1.Y := 0;
    SetConsoleCursorPosition (hStdOut, Cord1);
    WriteLn('Commands for this are "quit", "exit", "Clear", "Count" and "Repeat"');
    end else
    WriteLn('Command is NOT recognized');

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Like you said, even the ExitProc is NOT called if the X close button is used
you might consider a
while True do

to at least let your user have the option of exiting you program
I am not sure that console Apps were meant to have any memory objects that need to be freeded, like Fonts, brushes or bitmaps, so you may need to use a GUI interface
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.