|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
我用论坛上的代码计算文件的MD5值,但是一直提示类型不匹配
我的电脑系统是Win11,Excel是2021版的
1、在Declare 语句中添加PtrSafe
2、然后把long数据类型,改为LongPtr
3、然后就出现如图错误,这个网上也搜不到,所以处理不了了,请高人指点一下
代码如下:
Option Explicit
Option Base 0
Public Type MD5_CTX
i(1) As LongPtr
buf(3) As LongPtr
inc(63) As Byte
digest(15) As Byte
End Type
Public Declare PtrSafe Sub MD5Init Lib "Cryptdll.dll" (ByVal pContex As LongPtr)
Public Declare PtrSafe Sub MD5Final Lib "Cryptdll.dll" (ByVal pContex As LongPtr)
Public Declare PtrSafe Sub MD5Update Lib "Cryptdll.dll" (ByVal pContex As LongPtr, ByVal lPtr As LongPtr, ByVal nSize As LongPtr)
Public Function ConvBytesToBinaryString(bytesIn() As Byte) As String
Dim i As LongPtr
Dim nSize As LongPtr
Dim strRet As String
nSize = UBound(bytesIn)
For i = 0 To nSize
strRet = strRet & Right$("0" & Hex(bytesIn(i)), 2)
Next
ConvBytesToBinaryString = strRet
End Function
Public Function GetMD5Hash(bytesIn() As Byte) As Byte()
Dim ctx As MD5_CTX
Dim nSize As LongPtr
nSize = UBound(bytesIn) + 1
MD5Init VarPtr(ctx)
MD5Update ByVal VarPtr(ctx), ByVal VarPtr(bytesIn(0)), nSize
MD5Final VarPtr(ctx)
GetMD5Hash = ctx.digest
End Function
Public Function GetMD5Hash_Bytes(bytesIn() As Byte) As String
GetMD5Hash_Bytes = ConvBytesToBinaryString(GetMD5Hash(bytesIn))
End Function
Public Function GetMD5Hash_String(ByVal strIn As String) As String
GetMD5Hash_String = GetMD5Hash_Bytes(StrConv(strIn, vbFromUnicode))
End Function
Public Function GetMD5Hash_File(ByVal strFile As String) As String
Dim lFile As LongPtr
Dim bytes() As Byte
Dim lSize As LongPtr
lSize = FileLen(strFile)
If (lSize) Then
lFile = FreeFile
ReDim bytes(lSize - 1)
Open strFile For Binary As lFile
Get lFile, , bytes
Close lFile
GetMD5Hash_File = GetMD5Hash_Bytes(bytes)
End If
End Function
|
|