传递给函数的布尔值无法按预期工作

时间:2014-12-29 07:23:38

标签: function vb6 boolean

下面的代码检查进程是否在WOW64下运行。但是现在我的下面的代码iswow64过程的参数类型为boolean。但即使变量sixtyfourbittruefalse,if条件也始终为false条件。即使我传递了具有正确数据类型的参数,为什么会发生这种情况。然后我将相同的参数(布尔值)作为字符串传递给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

1 个答案:

答案 0 :(得分:5)

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

  • VB6对-1使用True0使用False
  • Windows API对1使用True0使用False。它遵循C惯例。
当API返回Boolean时,

VB6 不会执行任何转换。它相信你得到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始终是个好主意。

相关问题