找回密码
 加入
搜索
查看: 3194|回复: 6

[AU3基础] A文件的MD5值跟原来的不一样的时候,用B文件替换A文件的方法?[已解决]

[复制链接]
发表于 2014-5-9 09:48:46 | 显示全部楼层 |阅读模式
本帖最后由 kuailetiande 于 2014-5-12 15:10 编辑

有A文件的MD5值,当验证到A文件的MD5值跟原来的不一样的时候,用B文件替换A文件,是否有好的方法实现?
发表于 2014-5-9 10:28:59 | 显示全部楼层
#Include <Crypt.au3>
Local $ver=_MD5ForFile("D:\xxx\xxx.exe")


If $ver ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Then
     MsgBox(0,"","MD5相同:"&$ver)
    Else
     MsgBox(0,"","MD5不同:"&$ver)
EndIf
Func _MD5ForFile($sFile)

        Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _
                        "wstr", $sFile, _
                        "dword", 0x80000000, _ ; GENERIC_READ
                        "dword", 1, _ ; FILE_SHARE_READ
                        "ptr", 0, _
                        "dword", 3, _ ; OPEN_EXISTING
                        "dword", 0, _ ; SECURITY_ANONYMOUS
                        "ptr", 0)

        If @error Or $a_hCall[0] = -1 Then
                Return SetError(1, 0, "")
        EndIf

        Local $hFile = $a_hCall[0]

        $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", _
                        "hwnd", $hFile, _
                        "dword", 0, _ ; default security descriptor
                        "dword", 2, _ ; PAGE_READONLY
                        "dword", 0, _
                        "dword", 0, _
                        "ptr", 0)

        If @error Or Not $a_hCall[0] Then
                DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
                Return SetError(2, 0, "")
        EndIf

        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)

        Local $hFileMappingObject = $a_hCall[0]

        $a_hCall = DllCall("kernel32.dll", "ptr", "MapViewOfFile", _
                        "hwnd", $hFileMappingObject, _
                        "dword", 4, _ ; FILE_MAP_READ
                        "dword", 0, _
                        "dword", 0, _
                        "dword", 0)

        If @error Or Not $a_hCall[0] Then
                DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
                Return SetError(3, 0, "")
        EndIf

        Local $pFile = $a_hCall[0]
        Local $iBufferSize = FileGetSize($sFile)

        Local $tMD5_CTX = DllStructCreate("dword i[2];" & _
                        "dword buf[4];" & _
                        "ubyte in[64];" & _
                        "ubyte digest[16]")

        DllCall("advapi32.dll", "none", "MD5Init", "ptr", DllStructGetPtr($tMD5_CTX))

        If @error Then
                DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
                DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
                Return SetError(4, 0, "")
        EndIf

        DllCall("advapi32.dll", "none", "MD5Update", _
                        "ptr", DllStructGetPtr($tMD5_CTX), _
                        "ptr", $pFile, _
                        "dword", $iBufferSize)

        If @error Then
                DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
                DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
                Return SetError(5, 0, "")
        EndIf

        DllCall("advapi32.dll", "none", "MD5Final", "ptr", DllStructGetPtr($tMD5_CTX))

        If @error Then
                DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
                DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
                Return SetError(6, 0, "")
        EndIf

        DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)

        Local $sMD5 = Hex(DllStructGetData($tMD5_CTX, "digest"))

        Return SetError(0, 0, $sMD5)

EndFunc   ;==>_MD5ForFile
发表于 2014-5-9 11:13:43 | 显示全部楼层
a.txt、b.txt、demo.au3放置于同目录,a.txt、b.txt用来模拟你的文件A和B。
#include <Crypt.au3>

Opt('MustDeclareVars', 1)

_Main()
Exit

Func _Main()
        Local $FileA = @ScriptDir & '\a.txt'
        Local $FileB = @ScriptDir & '\b.txt'
        
        Local $MD5 = '0x202CB962AC59075B964B07152D234B70'
        
        Local $FileA_MD5 = _Crypt_HashFile($FileA, $CALG_MD5)
        If $FileA_MD5 <> $MD5 Then
                FileCopy($FileB, $FileA, 1)
                MsgBox(0, '', '文件已替换')
        EndIf
EndFunc   ;==>_Main

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2014-5-9 11:14:16 | 显示全部楼层
回复 2# fuldho


    多大点儿事搞这么复杂……
发表于 2014-5-9 12:13:57 | 显示全部楼层
回复 4# skyfree


  我忘了删除  #include <Crypt.au3>
带#include <Crypt.au3>编译会比我的代码少
发表于 2014-5-9 12:14:00 | 显示全部楼层
回复 4# skyfree


  我忘了删除  #include <Crypt.au3>
带#include <Crypt.au3>编译会比我的代码少
 楼主| 发表于 2014-5-12 15:09:50 | 显示全部楼层
回复 3# skyfree


    哈哈 多谢谢各位的帮助
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 04:17 , Processed in 0.196056 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表