【问题标题】:Boolean value passing to a function not working as expecetd传递给函数的布尔值未按预期工作
【发布时间】:2015-02-25 09:50:42
【问题描述】:

下面的代码检查进程是否在 WOW64 下运行。但是现在在我的下面的代码iswow64 程序中具有布尔类型的参数。但即使变量sixtyfourbittruefalse,if 条件总是变为假条件。为什么即使我传递了具有正确数据类型的参数也会发生这种情况。然后我将相同的参数(布尔值)作为字符串传递给iswow64_string 过程,它工作正常。但是谁能告诉我将它作为布尔值传递有什么问题以及为什么它不起作用。

Private Declare Function GetProcAddress Lib "kernel32" _
    (ByVal hModule As Long, _
    ByVal lpProcName As String) As Long

Private Declare Function GetModuleHandle Lib "kernel32" _
    Alias "GetModuleHandleA" _
    (ByVal lpModuleName As String) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" _
    () As Long

Private Declare Function IsWow64Process Lib "kernel32" _
    (ByVal hProc As Long, _
    bWow64Process As Boolean) As Long
Private Sub Form_Load()
    sixtyfourbit = Is64bit
    iswow64 (sixtyfourbit)
    iswow64_string (sixtyfourbit)
End Sub
Public Function Is64bit() As Boolean
    Dim handle As Long, bolFunc As Boolean

    ' Assume initially that this is not a Wow64 process
    bolFunc = False

    ' Now check to see if IsWow64Process function exists
    handle = GetProcAddress(GetModuleHandle("kernel32"), _
                   "IsWow64Process")

    If handle > 0 Then ' IsWow64Process function exists
        ' Now use the function to determine if
        ' we are running under Wow64
        IsWow64Process GetCurrentProcess(), bolFunc
    End If

    Is64bit = bolFunc

End Function
Public Function iswow64(ByVal sixtyfourbit As Boolean)
    If sixtyfourbit = True Then
        MsgBox ("process running under wow64")
    Else
        MsgBox ("process not running under wow64")
    End If
End Function

Public Function iswow64_string(ByVal sixtyfourbit As String)
    If sixtyfourbit = True Then
        MsgBox ("process running under wow64")
    Else
        MsgBox ("process not running under wow64")
    End If
End Function

【问题讨论】:

    标签: function vb6 boolean


    【解决方案1】:

    这是因为TrueFalse 在后台存储的方式不同。

    • VB6 将-1 用于True,将0 用于False
    • Windows API 将1 用于True,将0 用于False。它遵循 C 约定。

    VB6 在 API 返回 Boolean 时执行任何转换。它相信您得到了正确的Declare,并且它只是保留了 API 返回的完全相同的位。

    所以在这一行

    If sixtyfourbit = True Then

    您实际上是在比较If 1 = -1 Then,但条件不成立。

    在 VB6 中将 API 值视为Integer 会更好。像这样的东西(未经测试!)

    Private Declare Function GetProcAddress Lib "kernel32" _
        (ByVal hModule As Long, _
        ByVal lpProcName As String) As Long
    
    Private Declare Function GetModuleHandle Lib "kernel32" _
        Alias "GetModuleHandleA" _
        (ByVal lpModuleName As String) As Long
    
    Private Declare Function GetCurrentProcess Lib "kernel32" _
        () As Long
    
    Private Declare Function IsWow64Process Lib "kernel32" _
        (ByVal hProc As Long, _
        bWow64Process As Integer) As Long 
    
    Private Sub Form_Load()
        sixtyfourbit = Is64bit
        iswow64 (sixtyfourbit)
        iswow64_string (sixtyfourbit)
    End Sub
    Public Function Is64bit() As Boolean
        Dim handle As Long, bolFunc As Integer
    
        ' Assume initially that this is not a Wow64 process
        bolFunc = False
    
        ' Now check to see if IsWow64Process function exists
        handle = GetProcAddress(GetModuleHandle("kernel32"), _
                       "IsWow64Process")
    
        If handle > 0 Then ' IsWow64Process function exists
            ' Now use the function to determine if
            ' we are running under Wow64
            IsWow64Process GetCurrentProcess(), bolFunc
        End If
    
        Is64bit = (bolFunc <> 0) 
    
    End Function
    Public Function iswow64(ByVal sixtyfourbit As Boolean)
        If sixtyfourbit = True Then
            MsgBox ("process running under wow64")
        Else
            MsgBox ("process not running under wow64")
        End If
    End Function
    
    Public Function iswow64_string(ByVal sixtyfourbit As String)
        If sixtyfourbit = True Then
            MsgBox ("process running under wow64")
        Else
            MsgBox ("process not running under wow64")
        End If
    End Function 
    

    PS 我认为您的字符串版本有效,因为您已将值转换为字符串,并且因为变量被定义为Boolean,所以它决定将1 视为"True"。所以对比的是If "True" = "True" Then

    PPS 使用Option Explicit 总是一个好主意。

    【讨论】:

    • 好的。请看这里vbforums.com/…Bonnie West 有一个很好的解释。
    • 邦妮在某些事情上是对的,而在其他事情上是错的。我可能必须加入 VB 论坛才能回复...
    • 好的,我在另一个论坛上回复了,我在上面进行了一些编辑以纠正 Bonnie 发现的错误,并更清楚地解释为什么(我仍然认为)我的主要观点是正确的,尽管她说了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2018-02-01
    相关资源
    最近更新 更多