Leaderboard
Popular Content
Showing content with the highest reputation on 03/08/2013 in all areas
-
MX records for a specific domain
argumentum reacted to trancexx for a topic
Being new to AutoIt, I feel rush with every new script I make, do you? Retrieving MX records for a specific domain is something that you will probably never need to do. Or would you? In case you would... There is few tools on the net that will do it for you, or you can use 'nslookup.exe' that ships with your windows. There is few scripts here on forum that is automating nslookup (chiefly unsuccessful scripts). But that is not what I was thinking. AutoIt is very powerful scripting language, and almost ideal for this kind of tasks. MX records are retrieved after doing so called MX look up (this is smart, right?), and that is done by sending query, request to some DNS server. This communication is done on port 53, binary protocol is used by sending and receiving UDP packages. It is very hard to find some documentation on this (written for average human being to understand). So, this script (what scrip?) is written mostly relying on RFC1034 and RFC1035. It will not check for online status so if you run it off line it will just take 10-15 sec of your precious time. Domain name should be in form of ...domain name lol (example.com). It is ease to 'upgrade' it to check for online status, or to query MX records for IP-adress, or to query for example SOA records or ANAME or... Main function is made of three (sub)functions, to keep thingh as transparent as possible. Each of them can be replaced with better ones. First one will connect to the DNS server, ask the question and provide us with answer, second one will extract interesting parts of that answer and last one will sort things up. Commented only the interesting parts. Script will not use any external program or whatever to get us MX records! Question was what script. Well, this one: Opt("MustDeclareVars", 1) Dim $domain = "autoit.com" ; change it to domain of your interest Dim $mx = MXRecords($domain) If IsArray($mx) Then Local $au For $j = 1 To $mx[0][0] $au &= $mx[0][$j] & " - " & $mx[1][$j] & @CRLF Next MsgBox(0, "MX records for " & $domain, $au) Else MsgBox(0, "MX records for " & $domain, "No Records") EndIf Func MXRecords($domain) Local $binary_data = MXQueryServer($domain) If $binary_data = -1 Then Return -1 Local $output = ExtractMXServerData($binary_data) SortArray($output) Return $output EndFunc ;==>MXRecords Func MXQueryServer($domain) Local $domain_array $domain_array = StringSplit($domain, ".", 1) Local $binarydom For $el = 1 To $domain_array[0] $binarydom &= Hex(BinaryLen($domain_array[$el]), 2) & StringTrimLeft(StringToBinary($domain_array[$el]), 2) Next $binarydom &= "00" ; for example, 'gmail.com' will be '05676D61696C03636F6D00' and 'autoit.com' will be '066175746F697403636F6D00' Local $identifier = Hex(Random(0, 1000, 1), 2) ; random hex number serving as a handle for the data that will be received Local $server_bin = "0x00" & $identifier & "01000001000000000000" & $binarydom & "000F0001" ; this is our query Local $num_time, $data For $num_time = 1 To 10 Local $query_server ; ten(10) DNS servers, we'll start with one that is our's default, if no response or local one switch to public free servers Switch $num_time Case 1 Local $loc_serv = StringSplit(RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "DhcpNameServer"), " ", 1) If $loc_serv[0] > 0 Then If StringLeft($loc_serv[1], 3) <> "192" Then ; this kind of server is not what we want $query_server = $loc_serv[1] EndIf EndIf Case 2 $query_server = "4.2.2.1" Case 3 $query_server = "67.138.54.100" Case 4 $query_server = "208.67.222.222" Case 5 $query_server = "4.2.2.2" Case 6 $query_server = "4.2.2.3" Case 7 $query_server = "208.67.220.220" Case 8 $query_server = "4.2.2.4" Case 9 $query_server = "4.2.2.5" Case 10 $query_server = "4.2.2.6" Case 11 Return -1 EndSwitch If $query_server <> "" Then UDPStartup() Local $sock $sock = UDPOpen($query_server, 53) If $sock = -1 Then ; ok, that happens UDPCloseSocket($sock) UDPShutdown() ContinueLoop ; change server and try again EndIf UDPSend($sock, $server_bin) ; sending query Local $tik = 0 Do $data = UDPRecv($sock, 512) $tik += 1 Sleep(100) Until $data <> "" Or $tik = 8 ; waiting reasonable time for the response UDPShutdown() ; stopping service If $data <> "" And StringRight(BinaryMid($data, 2, 1), 2) = $identifier Then Return $data ; if there is data for us, return EndIf EndIf Next Return -1 EndFunc ;==>MXQueryServer Func ExtractMXServerData($binary_data) Local $num_answ = Dec(StringMid($binary_data, 15, 4)) ; representing number of answers provided by the server Local $arr = StringSplit($binary_data, "C00C000F0001", 1) ; splitting input; "C00C000F0001" - translated to human: "this is the answer for your MX query" If $num_answ <> $arr[0] - 1 Or $num_answ = 0 Then Return -1 ; dealing with possible options Local $pref[$arr[0]] ; preference number(s) Local $server[$arr[0]] ; server name(s) Local $output[2][$arr[0]] ; this goes out containing both server names and coresponding preference numbers $output[0][0] = $arr[0] - 1 Local $offset = 10 ; initial offset For $i = 2 To $arr[0] $arr[$i] = "0x" & $arr[$i] ; well, it is binary data $pref[$i - 1] = Dec(StringRight(BinaryMid($arr[$i], 7, 2), 4)) $offset += BinaryLen($arr[$i - 1]) + 6 ; adding lenght of every past part plus lenght of that "C00C000F0001" used for splitting Local $array = ReadBinary($binary_data, $offset) ; extraction of server names starts here While $array[1] = 192 ; dealing with special case $array = ReadBinary($binary_data, $array[6] + 2) WEnd $server[$i - 1] &= $array[2] & "." While $array[3] <> 0 ; the end will obviously be at $array[3] = 0 If $array[3] = 192 Then $array = ReadBinary($array[0], $array[4] + 2) If $array[3] = 0 Then $server[$i - 1] &= $array[2] ExitLoop Else $server[$i - 1] &= $array[2] & "." EndIf Else $array = ReadBinary($array[0], $array[5]) If $array[3] = 0 Then $server[$i - 1] &= $array[2] ExitLoop Else $server[$i - 1] &= $array[2] & "." EndIf EndIf WEnd $output[0][$i - 1] = $server[$i - 1] ; assigning $output[1][$i - 1] = $pref[$i - 1] ; part Next Return $output ; two-dimensional array EndFunc ;==>ExtractMXServerData Func ReadBinary($binary_data, $offset) Local $len = Dec(StringRight(BinaryMid($binary_data, $offset - 1, 1), 2)) Local $data_bin = BinaryMid($binary_data, $offset, $len) Local $checker = Dec(StringRight(BinaryMid($data_bin, 1, 1), 2)) Local $data = BinaryToString($data_bin) Local $triger = Dec(StringRight(BinaryMid($binary_data, $offset + $len, 1), 2)) Local $new_offset = Dec(StringRight(BinaryMid($binary_data, $offset + $len + 1, 1), 2)) Local $another_offset = $offset + $len + 1 Local $array[7] = [$binary_data, $len, $data, $triger, $new_offset, $another_offset, $checker] ; bit of this and bit of that Return $array EndFunc ;==>ReadBinary Func SortArray(ByRef $array) ; this will sort our $array by preferance numbers (in case of identical preferance, lower in order goes first - that's cool) If IsArray($array) Then ReDim $array[2][2 * $array[0][0] + 1] For $ij = $array[0][0] + 1 To 2 * $array[0][0] $array[1][$ij] = 1 Next For $bf = 1 To $array[0][0] For $cf = 1 To $array[0][0] If $array[1][$cf] - $array[1][$bf] > 0 Then $array[1][$cf + $array[0][0]] += 1 Next Next For $df = $array[0][0] + 1 To 2 * $array[0][0] For $ef = $array[0][0] + 1 To 2 * $array[0][0] If $ef = $df Then ContinueLoop If $array[1][$ef] - $array[1][$df] = 0 Then $array[1][$ef] += 1 Next Next Dim $aux_array[2][$array[0][0] + 1] $aux_array[0][0] = $array[0][0] For $ff = 1 To $array[0][0] $aux_array[1][$array[1][$ff + $array[0][0]]] = $array[1][$ff] $aux_array[0][$array[1][$ff + $array[0][0]]] = $array[0][$ff] Next $array = $aux_array Return $aux_array EndIf Return -1 EndFunc ;==>SortArray Please comment if you see any flaws.1 point -
When processing files, especially large ones, you will always lose battle if riding AutoIt. Not to mention RAM suffocation. So, what if I leave the whole process on a level that is few steps below me and just collect the cream? Script includes functions and small example: Opt("MustDeclareVars", 1) Global $sFile = FileOpenDialog("Choose file", "", "All files (*)") If @error Then Exit Global $hTimer, $iTimer, $sData ;------------------------------------------------------------------------ ; CRC32: $hTimer = TimerInit() $sData = _CRC32ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! CRC32 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; MD4: $hTimer = TimerInit() $sData = _MD4ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! MD4 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; MD5: $hTimer = TimerInit() $sData = _MD5ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! MD5 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; SHA1: $hTimer = TimerInit() $sData = _SHA1ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! SHA1 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; Functions... ; #FUNCTION# ;=============================================================================== ; ; Name...........: _CRC32ForFile ; Description ...: Calculates CRC32 value for the specific file. ; Syntax.........: _CRC32ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns CRC32 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - RtlComputeCrc32 function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _CRC32ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "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 $a_iCall = DllCall("ntdll.dll", "dword", "RtlComputeCrc32", _ "dword", 0, _ "ptr", $pFile, _ "int", $iBufferSize) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(4, 0, "") EndIf DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Local $iCRC32 = $a_iCall[0] Return SetError(0, 0, Hex($iCRC32)) EndFunc ;==>_CRC32ForFile ; #FUNCTION# ;=============================================================================== ; ; Name...........: _MD4ForFile ; Description ...: Calculates MD4 value for the specific file. ; Syntax.........: _MD4ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns MD4 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - MD4Init function or call to it failed. ; |5 - MD4Update function or call to it failed. ; |6 - MD4Final function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _MD4ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "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 $tMD4_CTX = DllStructCreate("dword i[2];" & _ "dword buf[4];" & _ "ubyte in[64];" & _ "ubyte digest[16]") DllCall("advapi32.dll", "none", "MD4Init", "ptr", DllStructGetPtr($tMD4_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", "MD4Update", _ "ptr", DllStructGetPtr($tMD4_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", "MD4Final", "ptr", DllStructGetPtr($tMD4_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 $sMD4 = Hex(DllStructGetData($tMD4_CTX, "digest")) Return SetError(0, 0, $sMD4) EndFunc ;==>_MD4ForFile ; #FUNCTION# ;=============================================================================== ; ; Name...........: _MD5ForFile ; Description ...: Calculates MD5 value for the specific file. ; Syntax.........: _MD5ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns MD5 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - MD5Init function or call to it failed. ; |5 - MD5Update function or call to it failed. ; |6 - MD5Final function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _MD5ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "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 ; #FUNCTION# ;=============================================================================== ; ; Name...........: _SHA1ForFile ; Description ...: Calculates SHA1 value for the specific file. ; Syntax.........: _SHA1ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns SHA1 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - CryptAcquireContext function or call to it failed. ; |5 - CryptCreateHash function or call to it failed. ; |6 - CryptHashData function or call to it failed. ; |7 - CryptGetHashParam function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _SHA1ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "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 $a_iCall = DllCall("advapi32.dll", "int", "CryptAcquireContext", _ "ptr*", 0, _ "ptr", 0, _ "ptr", 0, _ "dword", 1, _ ; PROV_RSA_FULL "dword", 0xF0000000) ; CRYPT_VERIFYCONTEXT If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(4, 0, "") EndIf Local $hContext = $a_iCall[1] $a_iCall = DllCall("advapi32.dll", "int", "CryptCreateHash", _ "ptr", $hContext, _ "dword", 0x00008004, _ ; CALG_SHA1 "ptr", 0, _ ; nonkeyed "dword", 0, _ "ptr*", 0) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(5, 0, "") EndIf Local $hHashSHA1 = $a_iCall[5] $a_iCall = DllCall("advapi32.dll", "int", "CryptHashData", _ "ptr", $hHashSHA1, _ "ptr", $pFile, _ "dword", $iBufferSize, _ "dword", 0) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptDestroyHash", "ptr", $hHashSHA1) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(6, 0, "") EndIf Local $tOutSHA1 = DllStructCreate("byte[20]") $a_iCall = DllCall("advapi32.dll", "int", "CryptGetHashParam", _ "ptr", $hHashSHA1, _ "dword", 2, _ ; HP_HASHVAL "ptr", DllStructGetPtr($tOutSHA1), _ "dword*", 20, _ "dword", 0) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptDestroyHash", "ptr", $hHashSHA1) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(7, 0, "") EndIf DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptDestroyHash", "ptr", $hHashSHA1) Local $sSHA1 = Hex(DllStructGetData($tOutSHA1, 1)) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(0, 0, $sSHA1) EndFunc ;==>_SHA1ForFile Results are in form of hex strings, but that is easily changed to fit your needs. First time is the hardest - you will see what I mean if you run it (file mapping related). Try it on something big.1 point
-
Make sure you run au3check on your scripts since there is clearly an error there. Install the full SciTE4AutoIt3 to help you with that. "...test\test.au3"(82,10) : error: syntax error Return () ~~~~~~~~^ Other that that:Topic locked for obvious reasons. Read the forum rules.1 point
-
C DLL Question
matwachich reacted to funkey for a topic
I cannot see what's wrong without code, but once I had the same problem with an app not a DLL ritten in C. Problem was that the command line was different when starting the app from windows than starting the app from console. I parsed the app for first command line parameter (the path) and on console it was only 'app.exe' and in windows it was 'C:app.exe'. Something like that.1 point -
Attention that sample code has a great tendency to crash >_< when it comes to call DllCall("imagehlp.dll","UnMapAndLoad"... )Well one quick and dirty workaround would be to just drop/delete all UnMapAndLoad calls since we just read data. Nothing is needed to write back. But well that's somehow unsatisfying and so we've learn nothing from that. Well it took me some time to find out the reason for the crash that somehow occurs inside imagehlp!UnMapAndLoad();FreeModuleName during the KERNEL32.HeapFree call. First i though there is something wrong with some unproper use of "ptr" or DllStructGetPtr() well but this seem to be fine. However to crashes here: typedef struct _LOADED_IMAGE { PSTR ModuleName; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ... PUSH [DWORD EBP+8] ; /pMemory <-PSTR ModuleName; PUSH 0 ; |Flags = 0 PUSH [DWORD 6CC851F4] ; |hHeap = 01420000 CALL [<&KERNEL32.HeapFree>] when it tries to free the memory MapAndLoad allocated for the path of the ModuleName. And finally I found it - the is problem is that UnMapAndLoad uses some other hHeap address than MapAndLoad used.So why does that value changes ? ->Well because Autoit loads and unload imagehlp.dll on each call when you use the dll parameter as a String. So instead doing it like this DllCall("imagehlp.dll","MapAndLoad"... ) DllCall("imagehlp.dll","UnMapAndLoad"... )Do it like this: $himagehlp_dll = DllOpen("imagehlp.dll") DllCall(himagehlp_dll, "int", "MapAndLoad"...) DllCall(himagehlp_dll, "int", "UnMapAndLoad"...) and everything will be alright. Well base on the example i made my own code that is targeting the Dll-Flag in the PE-Characteristics Func Main() ; Get TargetFile Local $PE_FileName = GetPEFile() If $PE_FileName = False Then Exit ; verify TargetFile If OpenPEFile($PE_FileName) = False Then Exit PEFile_UnMap() ; Copy *.* to *.dll and open it Local $Dll_FileName = $PE_FileName ChangeFileExt ($Dll_FileName,"dll") FileCopy ($PE_FileName, $Dll_FileName, 1) OpenPEFile($Dll_FileName) ; Set Dll Flag in PE_Header/Characteristics [via BitOR] NT_hdr_set("Characteristics", BitOR(NT_hdr_get("Characteristics"), _ $Characteristics_IMAGE_FILE_DLL) ) ; Save Changes PEFile_UnMap() Logtxt($Dll_FileName & " created.") EndFunc ;==>Main^-that's just the roadmap - DL ExeToDll.au3 for real use....and why convert a Exe to an DLL?Na well - just for fun. ... or to inject it into another Exe - to get some code (or au3-Script ) in, to extract some 'virtual'/'bundled' files or data from that Process. ExeToDll.au31 point
-
Dizzy, That is because I moved it there - originally you had posted it in "Examples" just as JLogan3o13 said. M231 point
-
Negro, A search would have saved you from wearing out your typing fingers. The same request from only 2 days ago suffered the same fate - as will all other threads asking this question which has been answered so many times over the years. M231 point
-
FileCountLines and Read Counted Lines :_: ?
Keniger reacted to johnmcloud for a topic
You have right, i have write "In alternative you can use _FileReadToArray", but that's not the alternative, it's the main way. Sorry for that.1 point -
Why not use _FileReadToArray?1 point
-
Perhaps you would need @ScriptDir for the folder path, and for the file path you'd use @ScriptFullPath which gives the path of the folder and the script name.1 point
-
Stopping a While 1 loop?
jgq85 reacted to DicatoroftheUSA for a topic
Or you can click the tray item and choose exit. If you do not wish it to exit entirely, have the hotkey function toggle a global variable that through an if then statement will trigger the exitloop function.1 point -
You can set an hotkey: HotKeySet("{ESC}", "Terminate") _AssetURLStrLoop() Func _AssetURLStrLoop() While 1 Sleep(300) WinActivate("Microsoft Word", "") Sleep(300) Send("^c") Sleep(300) Send("^k") ;~ Send("{TAB}{TAB}{TAB}{TAB}") Sleep(300) Send("www.google.com" & "^v") Sleep(300) Send("{Enter}") Send("{RIGHT}") Send("{DOWN}") Send("+{HOME}") WEnd EndFunc ;==>_AssetURLStrLoop Func Terminate() Exit EndFunc When you think that everything is fine just press the esc key Hi!1 point
-
jdelaney, Well picked up - I had forgotten that StringReplace was case-insensitive as a default. And my commiserations for your having had to work your way through that abomination to change the necessary parts! M231 point
-
When you replace D to 'e ', the e is then repaced to 'f ', then the f is replaaced to 'f ' Conceptually, this function will not work. NOTE: you are not setting the option to do case sensitive replaces, so f = F ConsoleWrite( "Non case sensitive replace...should be E: " & StringReplace("f", "F", "E") & @CRLF ) ConsoleWrite( "case sensitive replace...should remain f: " & StringReplace("f", "F", "E", 0, 1) & @CRLF ) output: Non case sensitive replace...should be E: E case sensitive replace...should remain f: f I agree, that the array route is better, but this corrects what is needed: case sensitive searches on the needed ones...rest are fine Func _HEX($text) Local $hex1, $hex2, $hex3, $hex4, $hex5 $hex1 = StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($text, " ", "0 "), "A", "6 "), "B", "e "), "C", "6 "), "D", "e "), "E", "f ", 0, 1), "F", "f ", 0, 1), "G", "6 "), "H", "9 "), "I", "6 "), "J", "f "), "K", "9 "), "L", "8 "), "M", "9 "), "N", "d "), "O", "6 "), "P", "e "), "Q", "6 "), "R", "e "), "S", "6 "), "T", "f "), "U", "9 "), "V", "9 "), "W", "9 "), "X", "9 "), "Y", "9 "), "Z", "f "), " ", " "), "!", "6 ") $hex2 = StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($text, " ", "0 "), "A", "9 "), "B", "9 "), "C", "8 "), "D", "9 "), "E", "8 "), "F", "8 "), "G", "8 "), "H", "9 "), "I", "6 "), "J", "1 "), "K", "9 "), "L", "8 "), "M", "f "), "N", "d "), "O", "9 "), "P", "9 "), "Q", "9 "), "R", "9 "), "S", "8 "), "T", "6 "), "U", "9 "), "V", "9 "), "W", "9 "), "X", "9 "), "Y", "9 "), "Z", "1 "), " ", " "), "!", "6 ") $hex3 = StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($text, " ", "0 "), "A", "f "), "B", "e "), "C", "8 "), "D", "9 "), "E", "f ", 0, 1), "F", "f ", 0, 1), "G", "8 "), "H", "f "), "I", "6 "), "J", "1 "), "K", "e "), "L", "8 "), "M", "9 "), "N", "f "), "O", "9 "), "P", "e "), "Q", "9 "), "R", "e "), "S", "6 "), "T", "6 "), "U", "9 "), "V", "9 "), "W", "9 "), "X", "6 "), "Y", "6 "), "Z", "6 "), " ", " "), "!", "6 ") $hex4 = StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($text, " ", "0 "), "A", "9 "), "B", "9 "), "C", "8 "), "D", "9 "), "E", "8 "), "F", "8 "), "G", "9 "), "H", "9 "), "I", "6 "), "J", "1 "), "K", "9 "), "L", "8 "), "M", "9 "), "N", "b "), "O", "9 "), "P", "8 "), "Q", "b "), "R", "9 "), "S", "1 "), "T", "6 "), "U", "9 "), "V", "6 "), "W", "f "), "X", "9 "), "Y", "6 "), "Z", "8 "), " ", " "), "!", "0 ") $hex5 = StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($text, " ", "0 "), "A", "9 "), "B", "e "), "C", "6 "), "D", "e "), "E", "f ", 0, 1), "F", "8 ", 0, 1), "G", "7 "), "H", "9 "), "I", "6 "), "J", "6 "), "K", "9 "), "L", "f "), "M", "9 "), "N", "b "), "O", "6 "), "P", "8 "), "Q", "7 "), "R", "9 "), "S", "6 "), "T", "6 "), "U", "6 "), "V", "6 "), "W", "9 "), "X", "9 "), "Y", "6 "), "Z", "f "), " ", " "), "!", "6 ") Return StringReplace(StringReplace($hex1&"!"&@CRLF&$hex2&"!"&@CRLF&$hex3&"!"&@CRLF&$hex4&"!"&@CRLF&$hex5&"!", " ", " "), " !", "") EndFunc ;==>_HEX1 point
-
Cupidkidd, I suggest you add some errorchecking to the script to see if it actually runs as you think it should once compiled. Some MsgBoxes at critical points giving you the return values from recently called functions would be a good place to start - it will also allow you to check that the script actually runs and does not hang at some point. Otherwise, I see you are trying to write to the root of the C drive - not something I would reccomend given the manner in which Vista+ protects certain areas. I would suggest changing that patht to something less likely to cause problems - but if you must use it then you might want to try adding #requireadmin to the top of the script to allow the necessary permissions. Any use? M231 point
-
Clark, Happens to me almost every day - and long may it continue. M231 point
-
But even MVPs can learn something new every day. Happened to me today1 point
-
Hi joseLB You can do like this for ask if it finished to talk. If $ospeech.Status.RunningState = $SRSEIsSpeaking Then ; is talking Else ; is not talking EndIf And If i remember well there is already several Udf for sapi voice !1 point