Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm in charge an application daemon which must handle
logoff event.

The sample script is:

use strict; use Win32::API; # sub catch { my $signame = shift; die "bye...\n"; } # my $func = "\&catch"; # my $Con_Event = new Win32::API('kernel32', 'SetConsoleCtrlHandler', 'P +I', 'N'); # if (!$Con_Event) { exit; } my $return = $Con_Event->Call($func, 1); if (!$return) { exit; } # loop for test only for (;;) { }

When in test the system crash and I must reboot.

What's wrong?

Thanks in Advance.

Comment on SetConsoleCtrlHandler Script
Download Code
Re: SetConsoleCtrlHandler Script
by CombatSquirrel (Hermit) on Mar 08, 2004 at 15:51 UTC
    I would guess that my $func = "\&catch"; should really read my $func = \&catch; as you probably want a reference to a function and not the function name as a string.
    This is just a quick thought, though.
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: SetConsoleCtrlHandler Script
by BrowserUk (Pope) on Mar 08, 2004 at 18:17 UTC

    As pointed out above, you are passing (the address off) an abritrary lump of string data to the OS kernel and asking it to call this as a function when your process receives a signal. Needless to say, when the OS tries to execute the machine code located at that location, the text "&catch" doesn't translate well into x86 machine code.

    Even if you correctly pass the address of your catch subroutine to the OS, it still isn't going to work. The code pointed at by &catch is perl bytecode, not x86 machinecode.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

      So,

      What can I do that?

      Thanks.

      Jes

        You could take a look at Win32::API::Callback. This is a part of dada's Win32-API-0.41 package. The documentation is minimalist, the best examples are in the samples directory.

        I have no idea whether this would work is conjunction with SetConsoleCtrlHandler, but it's possible. I think that you may well be the first to try to do this, which means you will be on your own for the most part.

        The alternative is to write your own callback wrapper in C or XS. You will need a compiler compatible with the build of Perl you are using. Again, you will be very much on your own getting this to work.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail