【问题标题】:Return value from VB6 exe Main?从 VB6 exe Main 返回值?
【发布时间】:2012-07-16 20:50:32
【问题描述】:

我的印象是这是不可能的,但这就是我目前所得到的。

Sub Main()        
    On Error GoTo ErrorHandler        
    If Command$ = "" Then
        LogAction "Begin document lockdown"
        LockdownDocs
        LogAction "Lockdown complete"
    Else
        LogAction "Begin document enabling"
        EnableDocs
        LogAction "Documents have be enabled"
    End If
Exit Sub
ErrorHandler:
    LogAction "DocLock Error " & Err.Number & "::" & Err.Description
End Sub

我希望它看起来像这样:

Function Main() As Boolean
    On Error GoTo ErrorHandler        
    If Command$ = "" Then
        LogAction "Begin document lockdown"
        LockdownDocs
        LogAction "Lockdown complete"
    Else
        LogAction "Begin document enabling"
        EnableDocs
        LogAction "Documents have be enabled"
    End If
    Return True
Exit Function
ErrorHandler:
    LogAction "Error " & Err.Number & "::" & Err.Description
    Return False
End Function

我见过的最接近的是 Visual Studio 2005 中的 Function Main() As Integer,但我使用的是 VB6。

【问题讨论】:

    标签: vb6 main


    【解决方案1】:

    有一个可能的解决方案here,通过使用 Win32 API 调用。本质上:

    Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
    
    ' Exit with ErrorLevel set to 9
    ExitProcess 9
    

    请注意,这相当于 VB 运行时的 End,因此您必须在调用 ExitProcess 之前进行任何清理、关闭连接、文件、设备、表单等。

    【讨论】:

    • 美丽优雅的解决方案。非常感谢!
    • +1 但请确保您已完成所有可能的清理工作,关闭和释放所有对象,将所有内容设置为 Nothing 等,
    • 确实,@Deanna 是正确的——这个调用会立即终止我相信的进程,所以请确保所有需要的东西都被取消初始化。
    • @NickShaw:我在答案中添加了一行来说明这一点。
    • 我最近才发现这在 Windows Server 2000 上无法正常工作。它会抛出一条错误消息,说“0x######## 处的指令引用了 0x## 处的内存######。无法读取内存。” Microsoft 不支持 VB6,但解释了一个可能的但不完美的解决方法是在 ExitProcess 之前立即调用 CoUninitializesupport.microsoft.com/kb/288216
    猜你喜欢
    • 2011-12-19
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2013-02-17
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多