VB.NET中如何获取控制台console关闭事件??

swq504 2011-02-27 11:22:51
程序流程:

Allocconsle 打开一个控制台窗口
Call 调用DLL文件中的函数
Freeconsole 关闭控制台窗口

Call 命令调用DLL文件中函数后,函数运行时,如果用户关闭控制台,会弹出错误对话框。
本人想实现:当用户点击控制台关闭按钮后,弹出一个确认对话框,点确定,控制台关闭,并不会出现错误对话框;点取消,控制台继续运行。

本人新手,请回复时尽量详细些。多多指教!!
...全文
365 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2011-02-27
  • 打赏
  • 举报
回复
Try

If args.Length > 0 AndAlso args(0).ToLower() = "-c" Then
'通过命令行 xxxx.exe -c 参数启动,Console
'注意:不用 Main(string[] args)、System.Environment.GetCommandLineArgs(); 也可以取得命令行参数在任何地方

'启动
NativeMethods.AllocConsole()
Console.WriteLine("控制台以启动")
End If
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
Finally
'关闭
NativeMethods.FreeConsole()
End Try
xingyuebuyu 2011-02-27
  • 打赏
  • 举报
回复
Imports System.Runtime.InteropServices


Public Class Form1

Public Shared Sub inputHandler(ByVal consoleEvent As ConsoleCtrl.ConsoleEvent)
MsgBox(consoleEvent.ToString)
If consoleEvent = ConsoleCtrl.ConsoleEvent.CtrlC Then
Console.WriteLine("Stopping due to user input")
' Cleanup code here.
System.Environment.[Exit](-1)
End If
End Sub

Dim cc As ConsoleCtrl
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConsoleCtrl.AllocConsole()
cc = New ConsoleCtrl
AddHandler cc.ControlEvent, AddressOf inputHandler
End Sub
End Class


''' <summary>
''' Class to catch console control events (ie CTRL-C) in C#.
''' Calls SetConsoleCtrlHandler() in Win32 API
''' </summary>
Public Class ConsoleCtrl
Implements IDisposable
''' <summary>
''' The event that occurred.
''' </summary>
Public Enum ConsoleEvent
CtrlC = 0
CtrlBreak = 1
CtrlClose = 2
CtrlLogoff = 5
CtrlShutdown = 6
End Enum

''' <summary>
''' Handler to be called when a console event occurs.
''' </summary>
Public Delegate Sub ControlEventHandler(ByVal consoleEvent As ConsoleEvent)

''' <summary>
''' Event fired when a console event occurs
''' </summary>
Public Event ControlEvent As ControlEventHandler

Private eventHandler As ControlEventHandler

Public Sub New()
' save this to a private var so the GC doesn't collect it...
eventHandler = New ControlEventHandler(AddressOf Handler)
SetConsoleCtrlHandler(eventHandler, True)
End Sub

Protected Overrides Sub Finalize()
Try
Dispose(False)
Finally
MyBase.Finalize()
End Try
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub

Private Sub Dispose(ByVal disposing As Boolean)
If eventHandler IsNot Nothing Then
SetConsoleCtrlHandler(eventHandler, False)
eventHandler = Nothing
End If
End Sub

Private Sub Handler(ByVal consoleEvent As ConsoleEvent)
RaiseEvent ControlEvent(consoleEvent)
End Sub

<DllImport("kernel32.dll")> _
Public Shared Function SetConsoleCtrlHandler(ByVal e As ControlEventHandler, ByVal add As Boolean) As Boolean
End Function

<DllImport("kernel32")> _
Public Shared Function AllocConsole() As Boolean
End Function

<DllImport("kernel32")> _
Public Shared Function FreeConsole() As Boolean
End Function
End Class

http://www.hanselman.com/blog/CommentView.aspx?guid=269#commentstart

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧