从网上下了个动态链接库(DLL)是用C#写的,里面很多参数都是用uint定义的,我想在VB下调用,可总是说类型无法转换。该怎么办呀?

解决方案 »

  1.   

    直接用Long好像还是不行。我在下载的C#源程序中把无符号型改掉,已经编译通过了。
      

  2.   

    呵呵,其实对于这种情况甚至可以采用Byte数组来接收,只须字节对齐即可,然后转换成double型即可。
    可以参考下面的代码,本来在VB当中的long型是有符号型,但是在FileLen采用的是无符号型数据,得到的超过2GB的文件的大小时对于long型,则变为一负值,由此可以做一个转换:
    -------------------------------------------------------
    Private Declare Sub CopyMemory Lib "kernel32" Alias _
                                       "RtlMoveMemory" (Destination As Any, _
                                                        Source As Any, _
                                                        ByVal Length As Long)'强制转换有符号长整型为无符号数
    Private Function LongAsULong2Double(ByVal Value As Long) As Double   Dim tmpByte(3) As Byte
       Dim dblValue As Double
       
       On Error GoTo ErrHandle
       
       If Value < 0 Then
          '若为负数,则强制转换为无符号型
          Call CopyMemory(tmpByte(0), Value, 4)
          dblValue = tmpByte(3) * 256# ^ 3# + _
                     tmpByte(2) * 256# ^ 2# + _
                     tmpByte(1) * 256# + _
                     tmpByte(0)
       Else
          '正整数直接返回
          dblValue = Value
       End If
       
       LongAsULong2Double = dblValue
       
       Exit Function
       
    ErrHandle:
       '错误时返回-1,表示调用失败
       LongAsULong2Double = -1
       
    End Function'获取小于4GB的文件大小,支持大于2GB的文件
    Public Function GetFileSize4GB(ByVal FileName As String) As Double
       
       Dim lLen As Long
       Dim dblSize As Double
       
       On Error GoTo ErrHandle
       
       '读取文件大小
       lLen = FileLen(FileName)
       
       '将有符号长整型数据转换为无符号长整型
       dblSize = LongAsULong2Double(lLen)
       
       GetFileSize4GB = dblSize
       
       Exit Function
       
    ErrHandle:
       '错误时返回-1,表示调用失败
       GetFileSize4GB = -1
    End Function
      

  3.   

    实际上这是一个串口通讯程序,从微软网站上下载的,题名
    “P/Invoking Serial APIs in the Compact Framework”,
    用C#写的。我觉得写的还可以,只是用起来老出错。
    ****************************************************
    如我在VB.NET中调用:Port.InputLen=1 ,出现这样的提示:
       “类型“Integer”的值无法转换为“System.UInt32”。”
    我查了一下,C#里InputLen的变量类型定义为uint。
    ****************************************************
    顺便说一下,我以前用VB的,VB.NET刚刚开始用,有些地方确实
    搞不清楚。
      

  4.   

    unsignedmeme(网络无名)老兄,我刚才试了一下,还是不行啊,麻烦你看还有没有其他原因。
      

  5.   

    这些转换函数是必备工具~~~~~~~~Microsoft技术支持已经帮我们做了。我们只需要收藏和利用即可~~~~~~~
    Private Const OFFSET_4 = 4294967296#
    Private Const MAXINT_4 = 2147483647
    Private Const OFFSET_2 = 65536
    Private Const MAXINT_2 = 32767Public Function UnsignedToLong(Value As Double) As Long
        '
        'The function takes a Double containing a value in the 
        'range of an unsigned Long and returns a Long that you 
        'can pass to an API that requires an unsigned Long
        '
        If Value < 0 Or Value >= OFFSET_4 Then Error 6 ' Overflow
        '
        If Value <= MAXINT_4 Then
            UnsignedToLong = Value
        Else
            UnsignedToLong = Value - OFFSET_4
        End If
        ' 
    End FunctionPublic Function LongToUnsigned(Value As Long) As Double
        '
        'The function takes an unsigned Long from an API and 
        'converts it to a Double for display or arithmetic purposes
        '
        If Value < 0 Then
            LongToUnsigned = Value + OFFSET_4
        Else
            LongToUnsigned = Value
        End If
        ' 
    End FunctionPublic Function UnsignedToInteger(Value As Long) As Integer
        '
        'The function takes a Long containing a value in the range 
        'of an unsigned Integer and returns an Integer that you 
        'can pass to an API that requires an unsigned Integer
        '
        If Value < 0 Or Value >= OFFSET_2 Then Error 6 ' Overflow
        '
        If Value <= MAXINT_2 Then
            UnsignedToInteger = Value
        Else
            UnsignedToInteger = Value - OFFSET_2
        End If
        ' 
    End FunctionPublic Function IntegerToUnsigned(Value As Integer) As Long
        '
        'The function takes an unsigned Integer from and API and 
        'converts it to a Long for display or arithmetic purposes
        '
        If Value < 0 Then
            IntegerToUnsigned = Value + OFFSET_2
        Else
            IntegerToUnsigned = Value
        End If
        ' 
    End Function 
      

  6.   

    实际上这是一个串口通讯程序,从微软网站上下载的,题名
    “P/Invoking Serial APIs in the Compact Framework”,
    用C#写的。我觉得写的还可以,只是用起来老出错。
    ****************************************************
    如我在VB.NET中调用:Port.InputLen=1 ,出现这样的提示:
       “类型“Integer”的值无法转换为“System.UInt32”。”
    我查了一下,C#里InputLen的变量类型定义为uint。
    ****************************************************
    顺便说一下,我以前用VB的,VB.NET刚刚开始用,有些地方确实
    搞不清楚。
    ===================================
    既然对方使用的是UInt32,那么也就相当于ULong,即VB当中的Long型看做无符号数传入即可。
    若是一个传回参数,倒还好,直接用long型,然后再做转换,否则就得次一个数转相当于ulong的数据传递给long型传递
    也就是要将上面的LongAsULong2Double做个逆向转换,先将数据拆成四个Byte,然后再CopyMemory到long型。Private Declare Sub CopyMemory Lib "kernel32" Alias _
                                       "RtlMoveMemory" (Destination As Any, _
                                                        Source As Any, _
                                                        ByVal Length As Long)Function DoubleAsULong2Long(ByVal Value As Double) as Long
       Dim tmpByte(3) As Byte
       Dim tmpValue1 As Double,tmpValue2 As Double
       tmpValue1=Value   tmpValue2=tmpValue1-Int(tmpValue1 / 256#) * 256#
       tmpByte(0)=tmpValue1-tmpValue2
       tmpValue1=tmpValue1-tmpValue2   tmpValue2=tmpValue1-Int(tmpValue1 / 256#^2#) * 256#^2#
       tmpByte(1)=(tmpValue1-tmpValue2)/256#
       tmpValue1=tmpValue1-tmpValue2   tmpValue2=tmpValue1-Int(tmpValue1 / 256#^3#) * 256#^3#
       tmpByte(2)=(tmpValue1-tmpValue2)/256#^2#
       tmpValue1=tmpValue1-tmpValue2   tmpValue2=tmpValue1-Int(tmpValue1 / 256#^4#) * 256#^4#
       tmpByte(3)=(tmpValue1-tmpValue2)/256#^3#
       Call CopyMemory(DoubleAsULong2Long,tmpByte(0),4)
    End Function
      

  7.   

    不好意思,上面的代码写错了一点^_^Private Declare Sub CopyMemory Lib "kernel32" Alias _
                                       "RtlMoveMemory" (Destination As Any, _
                                                        Source As Any, _
                                                        ByVal Length As Long)Private Function DoubleAsULong2Long(ByVal Value As Double) As Long
       Dim tmpByte(3) As Byte
       Dim tmpValue1 As Double, tmpValue2 As Double
       tmpValue1 = Value   tmpValue2 = tmpValue1 - Int(tmpValue1 / 256#) * 256#
       tmpByte(0) = tmpValue2
       tmpValue1 = tmpValue1 - tmpValue2   tmpValue2 = tmpValue1 - Int(tmpValue1 / 256# ^ 2#) * 256# ^ 2#
       tmpByte(1) = tmpValue2 / 256#
       tmpValue1 = tmpValue1 - tmpValue2   tmpValue2 = tmpValue1 - Int(tmpValue1 / 256# ^ 3#) * 256# ^ 3#
       tmpByte(2) = tmpValue2 / 256# ^ 2#
       tmpValue1 = tmpValue1 - tmpValue2   tmpValue2 = tmpValue1 - Int(tmpValue1 / 256# ^ 4#) * 256# ^ 4#
       tmpByte(3) = tmpValue2 / 256# ^ 3#
       Call CopyMemory(DoubleAsULong2Long, tmpByte(0), 4)
    End Function
      

  8.   

    用Long好像就行,VB会处理好……………………
      

  9.   

    对于32位系统应该说UInt即UInt32,也就一个32位数,那么用VB的Long做参数是不会有问题的。