NullReferenceException with no stack trace when hooking SetConsoleCtrlHandler

后端 未结 2 1949
甜味超标
甜味超标 2020-12-20 18:58

Using code to hook the console close event from this thread, I sometimes get a NullReferenceException with no stacktrace (most of the times I don\'t). It happen

2条回答
  •  星月不相逢
    2020-12-20 19:19

    For the vb.net bods who are struggling with this, my code is ...

    'declaration
    Private Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal handlerRoutine As ConsoleEventDelegate, ByVal add As Boolean) As Boolean
    Public Delegate Function ConsoleEventDelegate(ByVal [event] As ConsoleEvent) As Boolean
    
    'The close function...
    Public Function Application_ConsoleEvent(ByVal [event] As ConsoleEvent) As Boolean
        Console.WriteLine("We're closing it all down: ")
        Return False
    End Function
    
    'creating the handler. 
    If Not SetConsoleCtrlHandler(New ConsoleEventDelegate(AddressOf Application_ConsoleEvent), True) Then
        Console.WriteLine("Unable to install console event handler.")
        Exit Sub
    End If
    

提交回复
热议问题