#!perl use strict; use Win32::API; use Win32::API::Callback; use constant CTRL_C_EVENT => 0; use constant CTRL_BREAK_EVENT => 1; use constant CTRL_CLOSE_EVENT => 2; use constant CTRL_LOGOFF_EVENT => 5; use constant CTRL_SHUTDOWN_EVENT => 6; ####################################################################### # BOOL WINAPI HandlerRoutine( __in DWORD dwCtrlType ); my $cbfn = Win32::API::Callback->new( sub { my $type = shift; return 0; }, 'L', 'L' ); ####################################################################### # BOOL WINAPI SetConsoleCtrlHandler( __in_opt PHANDLER_ROUTINE HandlerRoutine, __in BOOL Add); my $fn1 = new Win32::API('kernel32', 'SetConsoleCtrlHandler', 'KL', 'L'); die "Can't get function handle" unless ($fn1); # The following line of code complains that PHANDLER_ROUTINE is an unknown type. Boo. Hiss. #my $fn = new Win32::API('kernel32', 'BOOL SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine, BOOL Add)'); #die "Can't get function handle" unless ($fn); my $fn2 = new Win32::API('kernel32', 'SetConsoleCtrlHandler', 'LL', 'L'); die "Can't get function handle" unless ($fn2); ####################################################################### # BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent, DWORD dwProcessGroupId); my $fn3 = new Win32::API('kernel32', 'BOOL GenerateConsoleCtrlEvent(DWORD dwCtrlEvent, DWORD dwProcessGroupId)'); die "Can't get function handle" unless ($fn3); print "Registering the callback function...\n"; die "Bad function call return value" unless $fn1->Call($cbfn, 1); # Generate a CTRL-C event that can be captured print "Generating a CTRL-C event...\n"; $fn3->Call(CTRL_C_EVENT, 0); print "Resetting the callback handler...\n"; my $return = $fn2->Call(0, 0); print "Done!\n";