布尔值传递给不能用作expecetd的函数

IT *_*her 2 vb6 boolean function

下面的代码检查进程是否在WOW64下运行.但现在我的下面的代码iswow64过程中有boolean类型的参数.但即使变量sixtyfourbit 是,true或者falseif条件总是变为错误状态.即使我传递了具有正确数据类型的参数,为什么会发生这种情况.然后我将相同的参数(布尔值)作为字符串传递给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
Run Code Online (Sandbox Code Playgroud)

Mar*_*rkJ 5

出现这种情况是因为,不同的方式True,并False存储在幕后.

  • VB6 -1用于True0for False.
  • Windows API使用1for True0for False.它遵循C惯例.

当API返回时,VB6 执行任何转换Boolean.它相信你得到了Declare正确的,它只保留API返回的完全相同的位.

所以在这一行

If sixtyfourbit = True Then

你实际上在比较If 1 = -1 Then而且条件不正确.

最好像Integer在VB6中那样处理API值.像这样的东西(未经测试!)

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 
Run Code Online (Sandbox Code Playgroud)

PS我觉得你的字符串形式的作品,因为你已经转换的值转换为字符串,因为变量定义为Boolean已决定把1作为"True".所以比较是If "True" = "True" Then

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