从网上下了个动态链接库(DLL)是用C#写的,里面很多参数都是用uint定义的,我想在VB下调用,可总是说类型无法转换。该怎么办呀?
解决方案 »
- datagrid 右键选择属性出现property pages里面的columns只有 0和1怎么只有2个,我想多加几个(其实我就是想控制datagrid所显示的列和列的名
- 怎样筹划一个远程教育建设!!!!!!重分求救。领导马上要!!!!!!
- 鼠标单、双击ListView控件的空白处引起的烦恼
- WiseInstall和Installshield哪个打包软件好?
- 不知道大家在实际开发中遇到过这样的情况没?谢谢
- 求助!!!!
- 有关winsock下载文件,其大小不对的问题!急!!!散分...
- 告诉我!
- 关于VB.NET,急救
- treeview的问题
- 怎样调整视频大小
- vb里面如果把combo设置成dropdownlist 如何取得选中的值
可以参考下面的代码,本来在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
“P/Invoking Serial APIs in the Compact Framework”,
用C#写的。我觉得写的还可以,只是用起来老出错。
****************************************************
如我在VB.NET中调用:Port.InputLen=1 ,出现这样的提示:
“类型“Integer”的值无法转换为“System.UInt32”。”
我查了一下,C#里InputLen的变量类型定义为uint。
****************************************************
顺便说一下,我以前用VB的,VB.NET刚刚开始用,有些地方确实
搞不清楚。
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
“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
"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