我用vb调用c DLL的函数,DLL的两个出口函数如下:
bool _stdcall cleanuparray(char InBuffer[],int MaxSize)
{
int i;
memset(InBuffer,0,MaxSize);
for(i = 0; i < MaxSize; i++)
InBuffer[i] = 'a';
return false;}bool _stdcall mytest(char InBuffer[],int MaxSize)
{
return false;
}
vb的声明:
Declare Function cleanuparray Lib "mydll.dll" (InBuffer As Byte, ByVal MaxSize As Long) As Boolean
Declare Function mytest Lib "mydll.dll" (InBuffer As Byte, ByVal MaxSize As Long) As Booleanvb程序如下:
x = cleanuparray(ab(0), MaxSize)
If x Then
MsgBox "x is true"
Else
MsgBox "x is false"
End If
y = mytest(ab(0), MaxSize)
If y Then
MsgBox "y is true"
Else
MsgBox "y is false"
End If
很奇怪的问题就是,“x is false”“y is true”,从DLL函数可以看出,两个函数不管什么情况下都返回false的。
而改用这种表达方式:
x = cleanuparray(ab(0), MaxSize)
If x = True Then
MsgBox "x is true"
Else
MsgBox "x is false"
End If
y = mytest(ab(0), MaxSize)
If y = True Then
MsgBox "y is true"
Else
MsgBox "y is false"
End If
结果却正确了:"x is false","y is false"
if x then
跟if x=true then两者有这么大的区别吗?!更诡异的是
我把vb声明改为:
Declare Function cleanuparray Lib "mydll.dll" (InBuffer As Byte, ByVal MaxSize As Long) As Long
Declare Function mytest Lib "mydll.dll" (InBuffer As Byte, ByVal MaxSize As Long) As Long
的时候,
昨天晚上用if y then,if y=true then(包括x)判断都正确,
今天早上起来,同样的情况下,if y then 却判断失常了,y好像总是true(其实肯定返回false)。我不明白,为什么if x then总能判断正确,而if y then却会失常?高手指点,谢谢!!

解决方案 »

  1.   

    VC中的bool或BOOL函数,在VB中声明,般都是用Long的,VB返回非零即VC的true或TRUE,零为false或FALSE,所以你应如下:
    Declare Function cleanuparray Lib "mydll.dll" (InBuffer As Byte, ByVal MaxSize As Long) As Long
    Declare Function mytest Lib "mydll.dll" (InBuffer As Byte, ByVal MaxSize As Long) As Long
    x = cleanuparray(ab(0), MaxSize)
    If x <> 0 Then
    MsgBox "x is true"
    Else
    MsgBox "x is false"
    End If
    y = mytest(ab(0), MaxSize)
    If y <> 0 Then
    MsgBox "y is true"
    Else
    MsgBox "y is false"
    End If
      

  2.   

    对于这种调试,你应如下使用:
    x = cleanuparray(ab(0), MaxSize)
    MsgBox "x is " & xy = mytest(ab(0), MaxSize)
    MsgBox "y is " & y
      

  3.   

    另外,VB中的True其实为-1,当你的VB返回TRUE或true时,其值不一定是-1,可能其它非0的数,你可以在VB的立即窗口试试:?3=true  回车(这里的3可换成其它非-1的数测试)得到False
      

  4.   

    照你的第一个方法,y还是很奇怪的是true..
    照你的
    x = cleanuparray(ab(0), MaxSize)
    MsgBox "x is " & xy = mytest(ab(0), MaxSize)
    MsgBox "y is " & y
    改动后,
    总是y is true难道是我dll的函数总是返回true了?!但是我用If y = True Then的表达方式,
    y是false的怎么解释?