Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 11/01/2016 in all areas

  1. Uhm... what?? You have over 2.2k posts here, someone could actually take your advices seriously.
    2 points
  2. j0kky

    STUN UDF

    Hi guysssss, have you ever heard of STUN?! No? When you are under a NAT and you try to connect to an online server, you make the request using an internal port and your local IP. But when your request arrives to the router, it uses its own port list and it enstablishes the connection to the final server using a different port (which has a different port number, but it is associated to your internal port) and the public (external) IP. So, the server responds to your request using the couple external IP\port assigned by the router. Well, STUN is a protocol that permits you to know your external IP but, more interesting, your external port associated to your internal port. But, which is the point? Client/Server connections don't require to know these informations, but what about P2P connection? STUN UDF is the second step (the first was my winsock UDF, which is used in this script) to UDP Hole Punching technique: Here you are the function: ; #FUNCTION# ======================================================================================================================== ; Name...........: _STUN ; Description ...: Makes a STUN request and processes the response ; Syntax.........: _STUN($iLocalPort, $iStunServer = "", $iStunPort = "") ; Parameters ....: $iLocalPort - The local port to be tested ; |1 : 65535 ; $iStunServer - name of the STUN server [Default = ""] ; $iStunPort - STUN server port, used only if $iStunServer <> "" ; |1 : 65535 - [Default = 3478] ; Return values .: On success it returns an array: ; |[0] - The external IP address ; |[1] - The external port ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - Unable to connect to internet ; |-4 - $iLocalPort is not a valid port ; |-5 - can't bind to $iLocalPort, try a different port ; |-6 - problem with servers ; Remarks .......: This function is used by a client to know which external port/IP correspond to the indicated internal port. ; By default it is used a STUN server list composed by: ; stun.l.google.com:19302, stun1.l.google.com:19302, stun.ekiga.net:3478, stun.ideasip.com:3478, stun.schlund.de:3478 ; If $iStunServer is indicated, the first element of the list is replaced by $iStunServer:$iStunPort ; Author ........: j0kky ; Modified ......: 1.0.0 ; Links .........: STUN RFC 5389: https://tools.ietf.org/html/rfc5389 ; ==================================================================================================================================== #include "winsock.au3" Func _STUN($iLocalPort, $iStunServer = "", $iStunPort = "") $iLocalPort = Int($iLocalPort) If ($iLocalPort < 1) Or ($iLocalPort > 65535) Then Return SetError(-4, 0, -1) $iExtended = 0 If $iStunServer = Default Then $iStunServer = "" $iStunServer = String($iStunServer) If $iStunServer Then $iStunServer = StringRegExpReplace($iStunServer, "^http.?://", "") If StringRight($iStunServer, 1) = "/" Then $iStunServer = StringTrimRight($iStunServer, 1) If Not StringRegExp($iStunServer, ".+\..{2,}") Then $iStunServer = "" ; invalid parameter $iExtended = -1 EndIf EndIf $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, $iExtended, -1) $tHeader = DllStructCreate("ushort MessageType; ushort MessageLenght; ulong MagicCookie; byte TransactionID[12]") $aRet = DllCall($hWs2, "ushort", "htons", "ushort", 0x0001) If @error Then DllClose($hWs2) Return SetError(-1, $iExtended, -1) Else DllStructSetData($tHeader, "MessageType", $aRet[0]) ;MessageType EndIf DllStructSetData($tHeader, "MessageLenght", 0x0000) ;MessageLenght $aRet = DllCall($hWs2, "ulong", "htonl", "ulong", 0x2112A442) If @error Then DllClose($hWs2) Return SetError(-1, $iExtended, -1) Else DllStructSetData($tHeader, "MagicCookie", $aRet[0]) ;MagicCookie EndIf For $i = 1 To 12 DllStructSetData($tHeader, "TransactionID", Random(0x00, 0xff, 1), $i) ;TransactionID Next _UDPStartup() If @error Then DllClose($hWs2) Return SetError(-1, $iExtended, -1) EndIf $sLocalIP = _GetIPs() If @error Then _UDPShutdown() DllClose($hWs2) Return SetError(-3, $iExtended, -1) EndIf $sLocalIP = $sLocalIP[0] If $iStunServer Then $sFirstServer = $iStunServer If $iStunPort Then $nFirstPort = $iStunPort Else $nFirstPort = 3478 EndIf Else $sFirstServer = "stun.l.google.com" $nFirstPort = 19302 EndIf Local $aStunServer[5][2] = [[$sFirstServer, $nFirstPort], ["stun1.l.google.com", 19302], ["stun.ekiga.net", 3478], ["stun.ideasip.com", 3478], ["stun.schlund.de", 3478]] $nBindSocket = _UDPBind($sLocalIP, $iLocalPort) If @error Then Return SetError(-5, $iExtended, -1) For $j = 0 To 4 $iError = 0 $sServerName = $aStunServer[$j][0] $sServerIP = _TCPNameToIP($sServerName) If @error Then $iError = -6 If Not $iError Then ;RFC indicates that $nRcMaxValue should be equal to 7 and $nRm should be equal to 16, ;but, following those rules, if the server doesn't respond quickly, the script is blocked for more than 1 minute $tByteHeader = DllStructCreate("byte[" & DllStructGetSize($tHeader) & "]", DllStructGetPtr($tHeader)) Local $nRTO = 500, $hTime = TimerInit(), $nRTOTotal = 0, $nRc = 0, $nRcMaxValue = 2, $aRecv = "", $nRm = 2 Do If ($nRc < $nRcMaxValue) And (TimerDiff($hTime) >= $nRTOTotal) Then _UDPSendTo($sServerIP, $aStunServer[$j][1], DllStructGetData($tByteHeader, 1), $nBindSocket) If @error Then $iError = -1 $nRTOTotal += $nRTO * (2 ^ $nRc) $nRc += 1 If $nRc = $nRcMaxValue Then $hTime = TimerInit() EndIf $aRecv = _UDPRecvFrom($nBindSocket, 2048, 1) If @error Then $iError = -1 Sleep(100) Until ($aRecv <> -1) Or (($nRc = $nRcMaxValue) And (TimerDiff($hTime) >= $nRTO * $nRm)) If Not IsArray($aRecv) Then $iError = -1 EndIf If Not $iError Then $dResponse = $aRecv[0] $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", BinaryMid($dResponse, 3, 2)) ;MessageLenght If @error Then $iError = -1 Else $nMessageLenght = $aRet[0] EndIf If Not ($nMessageLenght > 0) Then $iError = -6 EndIf If Not $iError Then $dResponse = BinaryMid($dResponse, 1, $nMessageLenght + 20) ;message lenght plus header size $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", BinaryMid($dResponse, 1, 2)) ;MessageType If @error Then $iError = -1 Else $nMessageType = $aRet[0] If Not (($nMessageType = 0x0101) Or ($nMessageType = 0x0111)) Then $iError = -6 EndIf EndIf If Not $iError Then $nByteNumber = 1 If $nMessageType = 0x0101 Then ;Binding success response Do $iError = 0 $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", BinaryMid($dResponse, 20 + $nByteNumber, 2)) ;Attribute Type If @error Then $iError = -1 Else $nAttributeType = $aRet[0] EndIf If Not $iError And (($nAttributeType = 0x0001) Or ($nAttributeType = 0x0020)) Then ;MAPPED-ADDRESS or XOR-MAPPED-ADDRESS $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", BinaryMid($dResponse, 24 + $nByteNumber, 2)) ;Family If @error Then $iError = -1 Else $nFamily = $aRet[0] EndIf EndIf If Not $iError Then Local $aResult[2] If $nAttributeType = 0x0020 Then ;XOR-MAPPED-ADDRESS $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", BinaryMid($dResponse, 26 + $nByteNumber, 2)) ;Port If @error Then $iError = -1 Else $aResult[1] = BitXOR($aRet[0], 0x2112) ;Xored with the most significant 16 bit of Magic Cookie EndIf If Not $iError Then If $nFamily = 0x0001 Then ;IPv4 $dNBOIP = BinaryMid($dResponse, 28 + $nByteNumber, 4) $dNBOCookie = Binary("0x" & Hex(0x2112A442)) $tNBOXoredIP = DllStructCreate("byte[4]") For $i = 1 To 4 DllStructSetData($tNBOXoredIP, 1, BitXOR(BinaryMid($dNBOIP, $i, 1), BinaryMid($dNBOCookie, $i, 1)), $i) Next $aRet = DllCall($hWs2, "ptr", "inet_ntoa", "ulong", Int(DllStructGetData($tNBOXoredIP, 1))) If @error Then $iError = -1 ElseIf $aRet[0] = Null Then $iError = -6 Else $aResult[0] = DllStructGetData(DllStructCreate("char[15]", $aRet[0]), 1) ;IP address xored with Magic Cookie EndIf Else ;IPv6 $dNBOIP = BinaryMid($dResponse, 28 + $nByteNumber, 16) $dNBOConcatenation = BinaryMid($dResponse, 5, 16) $tNBOXoredIP = DllStructCreate("byte[16]") For $i = 1 To 16 DllStructSetData($tNBOXoredIP, 1, BitXOR(BinaryMid($dNBOIP, $i, 1), BinaryMid($dNBOConcatenation, $i, 1)), $i) Next $tIP = DllStructCreate("char[46]") $aRet = DllCall($hWs2, "ptr", "inet_ntop", _ "int", 23, "ptr", DllStructGetPtr($tNBOXoredIP), "ptr", DllStructGetPtr($tIP), "ULONG_PTR", DllStructGetSize($tIP)) If @error Then $iError = -1 ElseIf $aRet[0] = Null Then $iError = -6 Else $aResult[0] = DllStructGetData($tIP, 1) ;IP address EndIf EndIf EndIf ExitLoop 2 ElseIf $nAttributeType = 0x0001 Then ;MAPPED-ADDRESS $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", BinaryMid($dResponse, 26 + $nByteNumber, 2)) ;Port If @error Then $iError = -1 Else $aResult[1] = $aRet[0] EndIf If Not $iError Then If $nFamily = 0x0001 Then ;IPv4 $aRet = DllCall($hWs2, "ptr", "inet_ntoa", "ulong", BinaryMid($dResponse, 28 + $nByteNumber, 4)) If @error Then $iError = -1 ElseIf $aRet[0] = Null Then $iError = -6 Else $aResult[0] = DllStructGetData(DllStructCreate("char[15]", $aRet[0]), 1) ;IP address EndIf Else ;IPv6 $tNBOIP = DllStructCreate("byte[16]") DllStructSetData($tNBOIP, 1, BinaryMid($dResponse, 28 + $nByteNumber, 16)) $tIP = DllStructCreate("char[46]") $aRet = DllCall($hWs2, "ptr", "inet_ntop", _ "int", 23, "ptr", DllStructGetPtr($tNBOIP), "ptr", DllStructGetPtr($tIP), "ULONG_PTR", DllStructGetSize($tIP)) If @error Then $iError = -1 ElseIf $aRet[0] = Null Then $iError = -6 Else $aResult[0] = DllStructGetData($tIP, 1) ;IP address EndIf EndIf EndIf ExitLoop 2 Else $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", BinaryMid($dResponse, 22 + $nByteNumber, 2)) ;Attribute Lenght If @error Then $iError = -1 Else $nAttributeLenght = $aRet[0] If Not (Mod($nAttributeLenght, 4) = 0) Then $nAttributeLenght += 4 - Mod($nAttributeLenght, 4) ;padding $nByteNumber += 4 + $nAttributeLenght ;TLV EndIf EndIf EndIf Until $nByteNumber > $nMessageLenght Else ;0x0111 = Binding error response $iError = -6 EndIf EndIf Next _UDPCloseSocket($nBindSocket) _UDPShutdown() DllClose($hWs2) If $iError Then $aResult = -1 If $iStunServer And ($iStunServer <> $sServerName) Then $iExtended = -2 Return SetError($iError, $iExtended, $aResult) EndFunc ;==>_STUN Please test it (especially if you have an IPv6 interface) and report every bug you can try STUN.zip
    1 point
  3. j0kky

    Winsock UDF

    Hi guys, some time ago I tried to deal with client\server application using native winsock functions, but I encountered some difficult because their limitations, so I decided to write a new UDF based on winsock library. Some functions are simply wrappers of native function, but some other are completely renewed and there are a lot of improvements. Check it out! Each function has a detailed description and I attached 5 simple example script ; #INDEX# ======================================================================================================================= ; Title .........: Winsock ; AutoIt Version : 3.3.14.2 ; Language ......: English ; Description ...: Functions that assist with Winsock library management. ; Author(s) .....: j0kky ; =============================================================================================================================== ; #CONSTANTS# =================================================================================================================== Global Const $TCP_DATA_EOT = 2 ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ; _TCPStartup ; _TCPListen ; _TCPAccept ; _TCPRecv ; _TCPConnect ; _TCPSend ; _TCPNameToIP ; _TCPCloseSocket ; _TCPShutdown ; _GetIps ; _UDPStartup ; _UDPBind ; _UDPSendTo ; _UDPRecvFrom ; _UDPCloseSocket ; _UDPShutdown ; =============================================================================================================================== ; #WRAPPER# ===================================================================================================================== Func _TCPStartup() $iResult = TCPStartup() Return SetError(@error, 0, $iResult) EndFunc ;==>_TCPStartup ; #WRAPPER# ===================================================================================================================== Func _TCPListen($iIPAddr, $iPort, $iMaxPendingConnection = 0) If Not $iMaxPendingConnection Then $iResult = TCPListen($iIPAddr, $iPort) Else $iResult = TCPListen($iIPAddr, $iPort, $iMaxPendingConnection) EndIf Return SetError(@error, 0, $iResult) EndFunc ;==>_TCPListen ; #FUNCTION# ==================================================================================================================== ; Name...........: _TCPAccept ; Description ...: Permits an incoming connection attempt on a socket. ; Syntax.........: _TCPAccept($iMainsocket) ; Parameters ....: $iMainsocket - The main socket identifier (SocketID) as returned by _TCPListen function. ; Return values .: On success it returns an array: ; |[0] - The connected socket identifier. ; |[1] - The external address of the client ; |[2] - The external port which the client are communicating on ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter (not used in this function) ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; Author ........: j0kky ; Modified ......: 1.0.0 ; Links .........: accept: https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _TCPAccept($iMainsocket) $iMainsocket = Number($iMainsocket) If $iMainsocket < 0 Then Return SetError(-4, 0, -1) ; invalid parameter Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0, $hSock = 0 Local $tagSockAddr = "short sin_family; ushort sin_port; " & _ "STRUCT; ulong S_addr; ENDSTRUCT; " & _ ;sin_addr "char sin_zero[8]" If Not $bError Then $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $iMainsocket, "long", 0x8004667e, "ulong*", 1) ;FIONBIO If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf EndIf If Not $bError Then $tSockAddr = DllStructCreate($tagSockAddr) $aRet = DllCall($hWs2, "uint", "accept", "uint", $iMainsocket, "ptr", DllStructGetPtr($tSockAddr), "int*", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf ($aRet[0] = 4294967295) Or ($aRet[0] = -1) Then ;INVALID_SOCKET $bError = 1 $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $bError = -1 ElseIf ($aRet[0] = 0) Or ($aRet[0] = 10035) Then ;WSAEWOULDBLOCK $nCode = -10 ;internal function value, it means no error EndIf Else $hSock = $aRet[0] $aRet = DllCall($hWs2, "ptr", "inet_ntoa", "ulong", DllStructGetData($tSockAddr, "S_addr")) If @error Then $bError = -1 ElseIf $aRet[0] = Null Then $bError = 1 Else $sIPAddr = DllStructGetData(DllStructCreate("char[15]", $aRet[0]), 1) $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", DllStructGetData($tSockAddr, "sin_port")) If @error Then $bError = -1 Else $nPort = $aRet[0] Local $aResult[3] = [$hSock, $sIPAddr, $nPort] EndIf EndIf EndIf EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = -1 ;failure If $hSock Then TCPCloseSocket($hSock) ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf If $nCode = -10 Then $nCode = 0 $nReturn = -1 If $hSock Then TCPCloseSocket($hSock) Else $nReturn = $aResult EndIf DllClose($hWs2) Return SetError($nCode, 0, $nReturn) EndFunc ;==>_TCPAccept ; #FUNCTION# ==================================================================================================================== ; Name...........: _TCPRecv ; Description ...: Receives data from a connected socket. ; Syntax.........: _TCPRecv($iMainsocket, $iMaxLen, $iFlag = 0) ; Parameters ....: $iMainsocket - The array as returned by _TCPAccept ; or the connected socket identifier (SocketID) as returned by _TCPConnect. ; $iMaxLen - max # of characters to receive (usually 2048). ; $iFlag - values can be added together ; |$TCP_DATA_DEFAULT (0) - Text data. [Default] ; |$TCP_DATA_BINARY (1) - Binary data. ; |$TCP_DATA_EOT (2) - Returns data received and ; set @error to -6 when it reaches the End of Text ASCII character (Chr(3)) ; Return values .: On success it returns the binary/string sent by the connected socket. ; On failure it returns "" and sets the @error or @extended flag to non-zero: ; @error values: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; @extended values: ; |1 - connection closed ; |2 - End of Text reached ; Author ........: j0kky ; Modified ......: 1.0.0 ; Remarks .......: If Unicode strings need to be transmitted they must be encoded/decoded with StringToBinary()/BinaryToString(). ; $iFlag = 2 must be set in couple with _TCPSend ; You must check for both @error and @extended, @extended could be set with @error set to zero ; Links .........: recv: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _TCPRecv($iMainsocket, $iMaxLen, $iFlag = 0) If IsArray($iMainsocket) And (UBound($iMainsocket, 0) = 1) And (UBound($iMainsocket) > 0) Then $iMainsocket = $iMainsocket[0] If $iFlag = Default Then $iFlag = 0 $iMainsocket = Number($iMainsocket) $iMaxLen = Number($iMaxLen) $iFlag = Number($iFlag) If $iMainsocket < 0 Or _ $iMaxLen < 1 Or _ Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(-4, 0, -1) ; invalid parameter Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0, $nExtended = 0 If Not $bError Then $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $iMainsocket, "long", 0x8004667e, "ulong*", 1) ;FIONBIO If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf EndIf Local $tBuf If $iFlag Then $tBuf = DllStructCreate("byte[" & $iMaxLen & "]") Else $tBuf = DllStructCreate("char[" & $iMaxLen & "]") EndIf $aRet = DllCall($hWs2, "int", "recv", "uint", $iMainsocket, "ptr", DllStructGetPtr($tBuf), "int", $iMaxLen, "int", 0) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Then ;SOCKET_ERROR $bError = 1 $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $bError = -1 ElseIf $aRet[0] = 0 Or $aRet[0] = 10035 Then ;WSAEWOULDBLOCK $nCode = -10 ;internal function value, it means no error EndIf ElseIf $aRet[0] = 0 Then $bError = 1 $nCode = -10 $nExtended = 1 ;connection closed Else Local $sResult = DllStructGetData($tBuf, 1) ;data If BitAND($iFlag, 2) = 2 Then ;EOT If StringRight($sResult, 1) = Chr(3) Then $sResult = StringTrimRight($sResult, 1) $nExtended = 2 ;End of Text reached EndIf EndIf EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = "" ;failure ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf If $nCode = -10 Then $nCode = 0 $nReturn = "" Else $nReturn = $sResult EndIf DllClose($hWs2) Return SetError($nCode, $nExtended, $nReturn) EndFunc ;==>_TCPRecv ; #FUNCTION# ==================================================================================================================== ; Name...........: _TCPConnect ; Description ...: Create a socket connected to an existing server. ; Syntax.........: _TCPConnect($sIPAddr, $iDestPort, $sSourceAddr = "", $iSourcePort = 0, $iTimeOut = 0) ; Parameters ....: $sIPAddr - Destination IP. ; |Internet Protocol dotted address(IpV4) as "192.162.1.1". ; $iDestPort - Destination port. ; |1 : 65534 - port on which the created socket will be connected. ; $sSourceAddr - Source IP ; |Internet Protocol dotted address(IpV4) as "192.162.1.1". [Default = ""] ; $iSourcePort - Source port. ; |1 : 65534 - port on which the created socket will be bind (on the local PC). [Default = 0] ; $iTimeOut - The maximum time in milliseconds for _TCPConnect to wait for connection. ; |Any value > 0 [Default = 0 and it will be equal to Opt("TCPTimeout")]. ; Return values .: On success it returns the main socket identifier. ; |Any value > 0 ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter ; |-5 - not connected ; |-6 - timed out ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; Author ........: j0kky ; Modified ......: 1.0.0 ; Remarks .......: This function is used by a client to communicate with the server and it allows to choose a source IP, ; a source port and to set a timeout for the connection. ; Links .........: bind: https://msdn.microsoft.com/en-us/library/windows/desktop/ms737550(v=vs.85).aspx ; connect: https://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx ; select: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _TCPConnect($sIPAddr, $iDestPort, $sSourceAddr = "", $iSourcePort = 0, $iTimeOut = 0) If $sSourceAddr = Default Then $sSourceAddr = "" If $iSourcePort = Default Then $iSourcePort = 0 If $iTimeOut = Default Then $iTimeOut = 0 $sIPAddr = String($sIPAddr) $iDestPort = Number($iDestPort) $sSourceAddr = String($sSourceAddr) $iSourcePort = Number($iSourcePort) $iTimeOut = Number($iTimeOut) If Not ($iDestPort > 0 And $iDestPort < 65535) Or _ Not ($iSourcePort >= 0 And $iSourcePort < 65535) Or _ Not ($iTimeOut >= 0) Then Return SetError(-4, 0, -1) ; invalid parameter StringRegExp($sIPAddr, "((?:\d{1,3}\.){3}\d{1,3})", 3) ;$STR_REGEXPARRAYGLOBALMATCH If @error Then Return SetError(-4, 0, -1) If $sSourceAddr <> "" Then StringRegExp($sSourceAddr, "((?:\d{1,3}\.){3}\d{1,3})", 3) ;$STR_REGEXPARRAYGLOBALMATCH If @error Then Return SetError(-4, 0, -1) EndIf Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0 Local $tagSockAddr = "short sin_family; ushort sin_port; " & _ "STRUCT; ulong S_addr; ENDSTRUCT; " & _ ;sin_addr "char sin_zero[8]" Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 1, "int", 6); AF_INET, SOCK_STREAM, IPPROTO_TCP If @error Then $bError = -1 ElseIf ($hSock[0] = 4294967295) Or ($hSock[0] = -1) Then ;INVALID_SOCKET $bError = 1 Else $hSock = $hSock[0] EndIf If Not $bError Then $aRet = DllCall($hWs2, "ulong", "inet_addr", "str", $sIPAddr) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Or ($aRet[0] = 0) Then ;INADDR_NONE or INADDR_ANY $bError = 1 Else $sIPAddr = $aRet[0] EndIf EndIf If Not $bError Then $aRet = DllCall($hWs2, "ushort", "htons", "ushort", $iDestPort) If @error Then $bError = -1 Else $iDestPort = $aRet[0] EndIf EndIf If (Not $bError) And ($sSourceAddr <> "") Then $aRet = DllCall($hWs2, "ulong", "inet_addr", "str", $sSourceAddr) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Or ($aRet[0] = 0) Then ;INADDR_NONE or INADDR_ANY $bError = 1 Else $sSourceAddr = $aRet[0] EndIf EndIf If (Not $bError) And $iSourcePort Then $aRet = DllCall($hWs2, "ushort", "htons", "ushort", $iSourcePort) If @error Then $bError = -1 Else $iSourcePort = $aRet[0] EndIf EndIf If (Not $bError) And ($sSourceAddr Or $iSourcePort) Then $tSockAddr = DllStructCreate($tagSockAddr) DllStructSetData($tSockAddr, "sin_family", 2) ;AF_INET If $iSourcePort Then DllStructSetData($tSockAddr, "sin_port", $iSourcePort) Else DllStructSetData($tSockAddr, "sin_port", 0) EndIf If $sSourceAddr Then DllStructSetData($tSockAddr, "S_addr", $sSourceAddr) Else DllStructSetData($tSockAddr, "S_addr", 0x00000000) ;INADDR_ANY EndIf $aRet = DllCall($hWs2, "int", "bind", "uint", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf $tSockAddr = 0 EndIf If Not $bError Then $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $hSock, "long", 0x8004667e, "ulong*", 1) ;FIONBIO If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf EndIf If Not $bError Then $tSockAddr = DllStructCreate($tagSockAddr) DllStructSetData($tSockAddr, "sin_family", 2) ;AF_INET DllStructSetData($tSockAddr, "sin_port", $iDestPort) DllStructSetData($tSockAddr, "S_addr", $sIPAddr) $aRet = DllCall($hWs2, "int", "connect", "uint", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR -> functional with connect() on non-blocking sockets $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $bError = -1 ElseIf ($aRet[0] <> 0) And ($aRet[0] <> 10035) Then ;WSAEWOULDBLOCK $bError = 1 EndIf EndIf $tSockAddr = 0 EndIf If Not $bError Then If $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout") If $iTimeOut < 1 Then $iTimeOut = 100 Local $tagFd_set = "uint fd_count; uint fd_array[64]" Local $tFd_set_writefds = DllStructCreate($tagFd_set) DllStructSetData($tFd_set_writefds, "fd_count", 1) DllStructSetData($tFd_set_writefds, "fd_array", $hSock, 1) Local $tFd_set_exceptfds = DllStructCreate($tagFd_set) DllStructSetData($tFd_set_exceptfds, "fd_count", 1) DllStructSetData($tFd_set_exceptfds, "fd_array", $hSock, 1) Local $tTimeval = DllStructCreate("long tv_sec; long tv_usec") DllStructSetData($tTimeval, "tv_sec", Floor($iTimeOut / 1000)) DllStructSetData($tTimeval, "tv_usec", Round(Mod($iTimeOut, 1000) * 1000)) $aRet = DllCall($hWs2, "int", "select", _ "int", $hSock, "ptr", 0, "ptr", DllStructGetPtr($tFd_set_writefds), "ptr", DllStructGetPtr($tFd_set_exceptfds), "ptr", DllStructGetPtr($tTimeval)) If @error Then $bError = -1 ElseIf $aRet[0] = 0 Then ;time expired $bError = 1 $nCode = -6 ;timed out, similar to WSAETIMEDOUT ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Then ;SOCKET_ERROR $bError = 1 Else If Not (DllStructGetData($tFd_set_writefds, "fd_count") = 1) Then $bError = 1 If DllStructGetData($tFd_set_exceptfds, "fd_count") = 1 Then $tBuf = DllStructCreate("int") $aRet = DllCall("Ws2_32.dll", "int", "getsockopt", _ "uint", $hSock, "int", 0xffff, "int", 0x1007, "ptr", DllStructGetPtr($tBuf), "int*", DllStructGetSize($tBuf)) ;SO_ERROR If @error Then $bError = -1 ElseIf $aRet[0] = 0 Then $nCode = DllStructGetData($tBuf, 1) EndIf Else $nCode = -5 ;NOT_CONNECTED EndIf EndIf EndIf EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = -1 ;failure If $hSock Then TCPCloseSocket($hSock) ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf $nReturn = -1 If $hSock Then TCPCloseSocket($hSock) Else $nReturn = $hSock EndIf DllClose($hWs2) Return SetError($nCode, 0, $nReturn) EndFunc ;==>_TCPConnect ; #MODIFIED WRAPPER# ============================================================================================================ ; Name...........: _TCPSend ; Description ...: Wrapper of _TCPSend function, see the guide. ; Syntax.........: _TCPSend($iMainsocket, $iData, $iFlag = 0) ; Parameters ....: $iMainsocket - The array as returned by _TCPAccept ; or the connected socket identifier (SocketID) as returned by _TCPConnect. ; $iData - Data sent. ; |Text or binary data ; $iFlag - values can be added together ; |$TCP_DATA_DEFAULT (0) - It doesn't add anything at the end of the string [Default] ; |$TCP_DATA_EOT (2) - It adds an End of Text ASCII character (Chr(3)) at the end of the string ; Return values .: see the guide. ; Author ........: j0kky ; Modified ......: 1.0.0 ; Remarks .......: If Unicode strings need to be transmitted they must be encoded/decoded with StringToBinary()/BinaryToString(). ; $iFlag = $TCP_DATA_EOT must be set in couple with _TCPRecv, so don't use it for external application ; Links .........: send: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740149(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _TCPSend($iMainsocket, $iData, $iFlag = 0) If IsArray($iMainsocket) And (UBound($iMainsocket, 0) = 1) And (UBound($iMainsocket) > 0) Then $iMainsocket = $iMainsocket[0] If BitAND($iFlag, 2) = 2 Then $iData = String($iData) & Chr(3) $iResult = TCPSend($iMainsocket, $iData) Return SetError(@error, 0, $iResult) EndFunc ;==>_TCPSend ; #WRAPPER# ===================================================================================================================== Func _TCPNameToIP($sName) $iResult = TCPNameToIP($sName) Return SetError(@error, 0, $iResult) EndFunc ;==>_TCPNameToIP ; #FUNCTION# ==================================================================================================================== ; Name...........: _GetIps ; Description ...: Get local and public IP address of a network/computer ; Syntax.........: _GetIps($iServerName = "") ; Parameters ....: $iServerName - The server name which can retrieve your public IP ; |For example: "www.something.com/somethingelse" [Default = ""] ; Return values .: On success it returns an array: ; |[0] - Local IP (on LAN) of the adapter used to connect to Internet. ; |[1] - Public IP. ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter (not used) ; |-5 - servers are offline ; |-6 - IP retrieving failed ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; Author ........: j0kky ; Modified ......: 1.0.0 ; Remarks .......: It works with Winsock library, so it must be initialized with _TCPStartup(). ; Local and public IPs are both retrieved through internet. ; Links .........: error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _GetIps($iServerName = "") If $iServerName = Default Then $iServerName = "" $iServerName = String($iServerName) $iServerName = StringRegExpReplace($iServerName, "^http.?://", "") If StringInStr($iServerName, "/") Then If StringRegExp($iServerName, "/(.*)", 1)[0] Then If StringRight($iServerName, 1) = "/" Then $iServerName = StringTrimRight($iServerName, 1) EndIf Else $iServerName &= "/" EndIf If Not StringRegExp($iServerName, ".+\..{2,}") Then $iServerName = "" ; invalid parameter Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0, $sLocalIP = "", $sPublicIP = "" Local $tagSockAddr = "short sin_family; ushort sin_port; " & _ "STRUCT; ulong S_addr; ENDSTRUCT; " & _ ;sin_addr "char sin_zero[8]" Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 1, "int", 6); AF_INET, SOCK_STREAM, IPPROTO_TCP If @error Then $bError = -1 ElseIf ($hSock[0] = 4294967295) Or ($hSock[0] = -1) Then ;INVALID_SOCKET $bError = 1 Else $hSock = $hSock[0] EndIf If Not $bError Then If $iServerName Then Local $aGetIPURL[][2] = [["www.myexternalip.com/raw"], ["checkip.dyndns.org/"], ["bot.whatismyipaddress.com/"], [$iServerName]], $nElements = 0 Else Local $aGetIPURL[][2] = [["www.myexternalip.com/raw"], ["checkip.dyndns.org/"], ["bot.whatismyipaddress.com/"]], $nElements = 0 EndIf For $i = 0 To (UBound($aGetIPURL) - 1) $sServerIp = TCPNameToIP(StringRegExp($aGetIPURL[$i][0], "(.*?)/", 1)[0]) If $sServerIp <> "" Then $aGetIPURL[$nElements][0] = $aGetIPURL[$i][0] $aGetIPURL[$nElements][1] = $sServerIp $nElements += 1 EndIf Next If $nElements Then ReDim $aGetIPURL[$nElements][2] Else $bError = 1 $nCode = -5 ;there is no valid server for checking IP EndIf EndIf If Not $bError Then $aRet = DllCall($hWs2, "ushort", "htons", "ushort", 80) If @error Then $bError = -1 Else $iDestPort = $aRet[0] EndIf EndIf If Not $bError Then For $i = 0 To (UBound($aGetIPURL) - 1) $sServerIp = $aGetIPURL[$i][1] If Not $bError Then $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $hSock, "long", 0x8004667e, "ulong*", 1) ;FIONBIO If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf EndIf If Not $bError Then $aRet = DllCall($hWs2, "ulong", "inet_addr", "str", $sServerIp) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Or ($aRet[0] = 0) Then ;INADDR_NONE or INADDR_ANY $bError = 1 Else $sServerIp = $aRet[0] EndIf EndIf If Not $bError Then $tSockAddr = DllStructCreate($tagSockAddr) DllStructSetData($tSockAddr, "sin_family", 2) ;AF_INET DllStructSetData($tSockAddr, "sin_port", $iDestPort) DllStructSetData($tSockAddr, "S_addr", $sServerIp) $aRet = DllCall($hWs2, "int", "connect", "uint", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR -> functional with connect() on non-blocking sockets $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $bError = -1 ElseIf ($aRet[0] <> 0) And ($aRet[0] <> 10035) Then ;WSAEWOULDBLOCK $bError = 1 EndIf EndIf $tSockAddr = 0 EndIf If Not $bError Then Local $tagFd_set = "uint fd_count; uint fd_array[64]" Local $tFd_set_writefds = DllStructCreate($tagFd_set) DllStructSetData($tFd_set_writefds, "fd_count", 1) DllStructSetData($tFd_set_writefds, "fd_array", $hSock, 1) Local $tFd_set_exceptfds = DllStructCreate($tagFd_set) DllStructSetData($tFd_set_exceptfds, "fd_count", 1) DllStructSetData($tFd_set_exceptfds, "fd_array", $hSock, 1) Local $tTimeval = DllStructCreate("long tv_sec; long tv_usec") DllStructSetData($tTimeval, "tv_sec", Floor(500 / 1000)) DllStructSetData($tTimeval, "tv_usec", Round(Mod(500, 1000) * 1000)) $aRet = DllCall($hWs2, "int", "select", _ "int", $hSock, "ptr", 0, "ptr", DllStructGetPtr($tFd_set_writefds), "ptr", DllStructGetPtr($tFd_set_exceptfds), "ptr", DllStructGetPtr($tTimeval)) If @error Then $bError = -1 ElseIf ($aRet[0] = 0) Or ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Then $bError = 1 Else If Not (DllStructGetData($tFd_set_writefds, "fd_count") = 1) Then $bError = 1 EndIf EndIf If Not $bError And Not $sLocalIP Then $tSockAddr = DllStructCreate($tagSockAddr) $aRet = DllCall($hWs2, "int", "getsockname", "uint", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int*", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 Else $aRet = DllCall($hWs2, "ptr", "inet_ntoa", "ulong", DllStructGetData($tSockAddr, "S_addr")) If @error Then $bError = -1 ElseIf $aRet[0] = Null Then $bError = 1 Else $sLocalIP = DllStructGetData(DllStructCreate("char[15]", $aRet[0]), 1) ;IP address EndIf EndIf $tSockAddr = 0 EndIf If Not $bError Then $sRequest = "GET /" & StringRegExp($aGetIPURL[$i][0], "/(.*)", 1)[0] & " HTTP/1.1" & @CRLF & _ "Host: " & StringRegExp($aGetIPURL[$i][0], "(.*?)/", 1)[0] & @CRLF & _ "Connection: close" & @CRLF & @CRLF TCPSend($hSock, $sRequest) Local $sRecv = "", $hTimer = TimerInit() While 1 $sRecv &= _TCPRecv($hSock, 2048) If @error Or @extended Then If @error Then $bError = 1 ExitLoop EndIf If TimerDiff($hTimer) > 2500 Then ExitLoop Sleep(10) WEnd EndIf If Not $bError Then $aPublicIP = StringRegExp($sRecv, "((?:\d{1,3}\.){3}\d{1,3})", 3) ;STR_REGEXPARRAYGLOBALMATCH If Not @error Then $sPublicIP = $aPublicIP[0] ExitLoop Else $bError = 1 EndIf EndIf If $bError And $i = (UBound($aGetIPURL) - 1) Then $nCode = -6 ExitLoop Else $bError = 0 $nCode = 0 TCPCloseSocket($hSock) EndIf Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 1, "int", 6); AF_INET, SOCK_STREAM, IPPROTO_TCP If @error Then $bError = -1 ElseIf ($hSock[0] = 4294967295) Or ($hSock[0] = -1) Then ;INVALID_SOCKET $bError = 1 Else $hSock = $hSock[0] EndIf Next EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = -1 ;failure ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf $nReturn = -1 Else Local $nReturn[2] = [$sLocalIP, $sPublicIP] EndIf If $hSock Then TCPCloseSocket($hSock) DllClose($hWs2) Return SetError($nCode, 0, $nReturn) EndFunc ;==>_GetIps ; #MODIFIED WRAPPER# ============================================================================================================ ; Name...........: _TCPCloseSocket ; Description ...: Wrapper of TCPCloseSocket function, see the guide. ; Syntax.........: _TCPCloseSocket($iMainsocket) ; Parameters ....: $iMainsocket - The array as returned by _TCPAccept ; or the connected socket identifier (SocketID) as returned by _TCPListen and _TCPConnect. ; Return values .: see the guide. ; Author ........: j0kky ; Modified ......: 1.0.0 ; Links .........: recv: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _TCPCloseSocket($iMainsocket) If IsArray($iMainsocket) And (UBound($iMainsocket, 0) = 1) And (UBound($iMainsocket) > 0) Then $iMainsocket = $iMainsocket[0] $iResult = TCPCloseSocket($iMainsocket) Return SetError(@error, 0, $iResult) EndFunc ;==>_TCPCloseSocket ; #WRAPPER# ===================================================================================================================== Func _TCPShutdown() $iResult = TCPShutdown() Return SetError(@error, 0, $iResult) EndFunc ;==>_TCPShutdown ; #WRAPPER# ===================================================================================================================== Func _UDPStartup() $iResult = UDPStartup() Return SetError(@error, 0, $iResult) EndFunc ;==>_UDPStartup ; #FUNCTION# ==================================================================================================================== ; Name...........: _UDPBind ; Description ...: Creates and bind a socket to a local IP and a local port. ; Syntax.........: _UDPBind($sSourceAddr = "", $iSourcePort = 0) ; Parameters ....: $sSourceAddr - Source IP ; |Internet Protocol dotted address(IpV4) as "192.162.1.1". [Default = ""] ; $iSourcePort - Source port. ; |1 : 65534 - port on which the created socket will be bind (on the local PC). [Default = 0] ; Return values .: On success it returns the main socket identifier. ; |Any value > 0 ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; Author ........: j0kky ; Modified ......: 1.0.0 ; Remarks .......: It could be used before both of _UDPSendTo (client app) and _UDPRecvFrom (server app) functions ; Links .........: bind: https://msdn.microsoft.com/en-us/library/windows/desktop/ms737550(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _UDPBind($sSourceAddr = "", $iSourcePort = 0) If $sSourceAddr = Default Then $sSourceAddr = "" If $iSourcePort = Default Then $iSourcePort = 0 $sSourceAddr = String($sSourceAddr) $iSourcePort = Number($iSourcePort) If Not ($iSourcePort >= 0 And $iSourcePort < 65535) Then Return SetError(-4, 0, -1) ; invalid parameter If $sSourceAddr <> "" Then StringRegExp($sSourceAddr, "((?:\d{1,3}\.){3}\d{1,3})", 3) ;$STR_REGEXPARRAYGLOBALMATCH If @error Then Return SetError(-4, 0, -1) EndIf Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0 Local $tagSockAddr = "short sin_family; ushort sin_port; " & _ "STRUCT; ulong S_addr; ENDSTRUCT; " & _ ;sin_addr "char sin_zero[8]" Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 2, "int", 17); AF_INET, SOCK_DGRAM, IPPROTO_UDP If @error Then $bError = -1 ElseIf ($hSock[0] = 4294967295) Or ($hSock[0] = -1) Then ;INVALID_SOCKET $bError = 1 Else $hSock = $hSock[0] EndIf If (Not $bError) And ($sSourceAddr <> "") Then $aRet = DllCall($hWs2, "ulong", "inet_addr", "str", $sSourceAddr) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Or ($aRet[0] = 0) Then ;INADDR_NONE or INADDR_ANY $bError = 1 Else $sSourceAddr = $aRet[0] EndIf EndIf If (Not $bError) And $iSourcePort Then $aRet = DllCall($hWs2, "ushort", "htons", "ushort", $iSourcePort) If @error Then $bError = -1 Else $iSourcePort = $aRet[0] EndIf EndIf If Not $bError Then $tSockAddr = DllStructCreate($tagSockAddr) DllStructSetData($tSockAddr, "sin_family", 2) ;AF_INET If $iSourcePort Then DllStructSetData($tSockAddr, "sin_port", $iSourcePort) Else DllStructSetData($tSockAddr, "sin_port", 0) EndIf If $sSourceAddr Then DllStructSetData($tSockAddr, "S_addr", $sSourceAddr) Else DllStructSetData($tSockAddr, "S_addr", 0x00000000) ;INADDR_ANY EndIf $aRet = DllCall($hWs2, "int", "bind", "uint", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf $tSockAddr = 0 EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = -1 ;failure If $hSock Then UDPCloseSocket($hSock) ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf $nReturn = -1 If $hSock Then UDPCloseSocket($hSock) Else $nReturn = $hSock EndIf DllClose($hWs2) Return SetError($nCode, 0, $nReturn) EndFunc ;==>_UDPBind ; #FUNCTION# ==================================================================================================================== ; Name...........: _UDPSendTo ; Description ...: Sends data to an address/port, it uses a socket created by _UDPBind or it creates a socket itself ; Syntax.........: _UDPSendTo($sIPAddr, $iDestPort, $iData, $iMainsocket = 0) ; Parameters ....: $sIPAddr - Destination IP. ; |Internet Protocol dotted address(IpV4) as "192.162.1.1". ; $iDestPort - Destination port. ; |1 : 65534 - port which the created socket will connect on. ; $iData - Data sent. ; |Text or binary data ; $iMainsocket - Main socket identifier returned by _UDPBind. ; |Any value > 0 [Default = 0] ; Return values .: On success it returns an array: ; |[0] - The number of bytes sent. ; |[1] - The main socket identifier used. ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; Author ........: j0kky ; Modified ......: 1.0.0 ; Links .........: sendto: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740148(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _UDPSendTo($sIPAddr, $iDestPort, $iData, $iMainsocket = 0) If $iMainsocket = Default Then $iMainsocket = 0 $iMainsocket = Number($iMainsocket) $sIPAddr = String($sIPAddr) $iDestPort = Number($iDestPort) If Not IsBinary($iData) Then $iData = String($iData) If Not ($iDestPort > 0 And $iDestPort < 65535) Or _ $iMainsocket < 0 Then Return SetError(-4, 0, -1) ; invalid parameter StringRegExp($sIPAddr, "((?:\d{1,3}\.){3}\d{1,3})", 3) ;$STR_REGEXPARRAYGLOBALMATCH If @error Then Return SetError(-4, 0, -1) Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0 Local $tagSockAddr = "short sin_family; ushort sin_port; " & _ "STRUCT; ulong S_addr; ENDSTRUCT; " & _ ;sin_addr "char sin_zero[8]" If Not $iMainsocket Then Local $aRet = DllCall($hWs2, "uint", "socket", "int", 2, "int", 2, "int", 17); AF_INET, SOCK_DGRAM, IPPROTO_UDP If @error Then $bError = -1 ElseIf ($aRet[0] = 4294967295) Or ($aRet[0] = -1) Then ;INVALID_SOCKET $bError = 1 Else $iMainsocket = $aRet[0] EndIf EndIf If Not $bError Then $aRet = DllCall($hWs2, "ulong", "inet_addr", "str", $sIPAddr) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Or ($aRet[0] = 0) Then ;INADDR_NONE or INADDR_ANY $bError = 1 Else $sIPAddr = $aRet[0] EndIf EndIf If Not $bError Then $aRet = DllCall($hWs2, "ushort", "htons", "ushort", $iDestPort) If @error Then $bError = -1 Else $iDestPort = $aRet[0] EndIf EndIf If Not $bError Then $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $iMainsocket, "long", 0x8004667e, "ulong*", 1) ;FIONBIO If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf EndIf If Not $bError Then Local $tSockAddr = DllStructCreate($tagSockAddr) DllStructSetData($tSockAddr, "sin_family", 2) ;AF_INET DllStructSetData($tSockAddr, "sin_port", $iDestPort) DllStructSetData($tSockAddr, "S_addr", $sIPAddr) Local $nLenght, $tBuf If IsBinary($iData) Then $nLenght = BinaryLen($iData) $tBuf = DllStructCreate("byte[" & $nLenght & "]") DllStructSetData($tBuf, 1, $iData) Else $nLenght = StringLen($iData) $tBuf = DllStructCreate("char[" & $nLenght & "]") DllStructSetData($tBuf, 1, $iData) EndIf $aRet = DllCall($hWs2, "int", "sendto", _ "uint", $iMainsocket, "ptr", DllStructGetPtr($tBuf), "int", $nLenght, "int", 0, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Then ;SOCKET_ERROR $bError = 1 Else Local $aReturn[2] = [$aRet[0], $iMainsocket] EndIf EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = -1 ;failure ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf $nReturn = -1 Else $nReturn = $aReturn EndIf DllClose($hWs2) Return SetError($nCode, 0, $nReturn) EndFunc ;==>_UDPSendTo ; #FUNCTION# ==================================================================================================================== ; Name...........: _UDPRecvFrom ; Description ...: Receives data from a bound socket on the local PC. ; Syntax.........: _UDPRecvFrom($iMainsocket, $iMaxLen, $iFlag = 0) ; Parameters ....: $iMainsocket - Main socket identifier. ; |Any value > 0 ; $iMaxLen - Max # of characters to receive (usually 2048). ; $iFlag ; |0 - Text data. [Default] ; |1 - Binary data. ; Return values .: On success it returns an array: ; |[0] - The data received. ; |[1] - The IP address of the sender. ; |[2] - The port used by the sender for connection. ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; Author ........: j0kky ; Modified ......: 1.0.0 ; Links .........: recvfrom: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740120(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _UDPRecvFrom($iMainsocket, $iMaxLen, $iFlag = 0) If $iFlag = Default Then $iFlag = 0 $iMainsocket = Number($iMainsocket) $iMaxLen = Number($iMaxLen) $iFlag = Number($iFlag) If $iMainsocket < 0 Or _ $iMaxLen < 1 Or _ Not ($iFlag = 0 Or $iFlag = 1) Then Return SetError(-4, 0, -1) ; invalid parameter Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0 Local $tagSockAddr = "short sin_family; ushort sin_port; " & _ "STRUCT; ulong S_addr; ENDSTRUCT; " & _ ;sin_addr "char sin_zero[8]" If Not $bError Then $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $iMainsocket, "long", 0x8004667e, "ulong*", 1) ;FIONBIO If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf EndIf Local $tSockAddr = DllStructCreate($tagSockAddr) Local $tBuf If $iFlag Then $tBuf = DllStructCreate("byte[" & $iMaxLen & "]") Else $tBuf = DllStructCreate("char[" & $iMaxLen & "]") EndIf $aRet = DllCall($hWs2, "int", "recvfrom", _ "uint", $iMainsocket, "ptr", DllStructGetPtr($tBuf), "int", $iMaxLen, "int", 0, "ptr", DllStructGetPtr($tSockAddr), "int*", DllStructGetSize($tSockAddr)) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Then ;SOCKET_ERROR $bError = 1 $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $bError = -1 ElseIf $aRet[0] = 0 Or $aRet[0] = 10035 Then ;WSAEWOULDBLOCK $nCode = -10 ;internal function value, it means no error EndIf Else Local $aResult[3] = [DllStructGetData($tBuf, 1)] ;data $aRet = DllCall($hWs2, "ptr", "inet_ntoa", "ulong", DllStructGetData($tSockAddr, "S_addr")) If @error Then $bError = -1 ElseIf $aRet[0] = Null Then $bError = 1 Else $aResult[1] = DllStructGetData(DllStructCreate("char[15]", $aRet[0]), 1) ;IP address $aRet = DllCall($hWs2, "ushort", "ntohs", "ushort", DllStructGetData($tSockAddr, "sin_port")) If @error Then $bError = -1 Else $aResult[2] = $aRet[0] ;port EndIf EndIf EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = -1 ;failure ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf If $nCode = -10 Then $nCode = 0 $nReturn = -1 Else $nReturn = $aResult EndIf DllClose($hWs2) Return SetError($nCode, 0, $nReturn) EndFunc ;==>_UDPRecvFrom ; #FUNCTION# ==================================================================================================================== ; Name...........: _UDPCloseSocket ; Description ...: Close a UDP socket. ; Syntax.........: _UDPCloseSocket($iMainsocket) ; Parameters ....: $iMainsocket - Main socket identifier returned by _UDPBind or the array returned by _UDPSendTo. ; |Any value > 0 ; Return values .: On success it returns 1. ; On failure it returns -1 and sets @error to non zero: ; |-1 - internal error ; |-2 - missing DLL (Ws2_32.dll) ; |-3 - undefined error ; |-4 - invalid parameter ; |Any Windows Socket Error Code retrieved by WSAGetLastError ; Author ........: j0kky ; Modified ......: 1.0.0 ; Links .........: closesocket: https://msdn.microsoft.com/en-us/library/windows/desktop/ms737582(v=vs.85).aspx ; error codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx ; =============================================================================================================================== Func _UDPCloseSocket($iMainsocket) If IsArray($iMainsocket) Then If Not ((UBound($iMainsocket, 0) = 1) And (UBound($iMainsocket) = 2)) Then Return SetError(-1, 0, -4) $iMainsocket = $iMainsocket[1] Else If $iMainsocket < 1 Then Return SetError(-1, 0, -4) EndIf Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0 $aRet = DllCall($hWs2, "int", "closesocket", "uint", $iMainsocket) If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = -1 ;failure ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf $nReturn = -1 Else $nReturn = 1 EndIf DllClose($hWs2) Return SetError($nCode, 0, $nReturn) EndFunc ;==>_UDPCloseSocket ; #WRAPPER# ===================================================================================================================== Func _UDPShutdown() $iResult = UDPShutdown() Return SetError(@error, 0, $iResult) EndFunc ;==>_UDPShutdown Obviously there can be a lot of errors, so please report here any problem\suggestion! Updated to: 11/01/16 winsock.zip
    1 point
  4. Hey all. So a month ago I helped some fellow scripters who needed a way to, from a pre-configured list of folders (in ini file), select a folder, list the contents in that folder, and then be able to launch a particular executable in that folder from an autoit GUI. Having done something very similar already, I modified my code somewhat and provided him the following, and after updating it to get rid of the errors some encountered, I would like to share it here. What this will allow you to do is, from the directory you have selected with the executables (can handle exe, zip, rar, 7z, and msi) you double click a selection, or select one or multiple files you want to install (or in the case of archive file extract), and if the file is an executable, will attempt to silently install the applications for you. Msi files can easily be installed silently, but the exe files will be scanned with a signature detection app, determine the installer that the application uses, then if the installer has known silent installation parameters, will use them to silently install the executable, or just regularly launch it if not. Here is the code and attached are the misc files you need. Have a blast. ; *** Start added by AutoIt3Wrapper *** #include <FileConstants.au3> #include <ListBoxConstants.au3> #include <MsgBoxConstants.au3> #include <StringConstants.au3> ; *** End added by AutoIt3Wrapper *** #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Add_Constants=n #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.15.0 (Beta) Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <GUIConstantsEx.au3> #include <GuiComboBox.au3> #include <GUIListBox.au3> #include <StaticConstants.au3> #include <File.au3> #include <Zip.au3> #include <WindowsConstants.au3> #include <Constants.au3> #Region ### START Koda GUI section ### Form=c:\users\whiggs\onedrive\always script\form\openjv3.kxf $Form1 = GUICreate("Install Launcher", 500, 593, 192, 124, BitOR($GUI_SS_DEFAULT_GUI,$DS_SETFOREGROUND), BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE)) $MenuItem1 = GUICtrlCreateMenu("Add") $MenuItem2 = GUICtrlCreateMenuItem("Folder", $MenuItem1) $Label1 = GUICtrlCreateLabel("Pick your poison", 176, 16, 147, 29) GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif") $dumm = GUICtrlCreateDummy() $Combo1 = GUICtrlCreateCombo("", 40, 56, 409, 25, BitOR($GUI_SS_DEFAULT_COMBO,$CBS_SIMPLE,$CBS_SORT,$WS_HSCROLL)) GUICtrlSetTip(-1, "Select the path you wish to view....") $List1 = GUICtrlCreateList("", 24, 96, 449, 422, BitOR($LBS_NOTIFY, $LBS_MULTIPLESEL, $LBS_HASSTRINGS, $WS_VSCROLL, $WS_BORDER)) $Button1 = GUICtrlCreateButton("Run", 48, 536, 89, 33, $BS_NOTIFY) GUICtrlSetState(-1, $GUI_DISABLE) GUICtrlSetCursor (-1, 0) $Button2 = GUICtrlCreateButton("Exit", 360, 536, 81, 33, $BS_NOTIFY) GUICtrlSetCursor (-1, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### If Not FileExists ( @ScriptDir & "\settings.ini" ) Then addPath() Else $array2 = IniReadSection ( @ScriptDir & "\settings.ini", "settings" ) Local $store2[$array2[0][0]] $here2 = 0 For $i = 1 To $array2[0][0] Step 1 GUICtrlSetData ( $Combo1, $array2[$i][1] ) ; $here2 += 1 Next ;GUICtrlSetData ( $Combo1, $store2 ) EndIf GUIRegisterMsg($WM_COMMAND, "doubleClick") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $MenuItem2 addPath() Case $Combo1 _GUICtrlListBox_ResetContent ( $List1 ) $use = GUICtrlRead ( $Combo1 ) If Not FileExists ( $use ) Then #Region --- CodeWizard generated code Start --- ;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=None, Miscellaneous=Top-most attribute MsgBox($MB_OK + 262144,"invalid path","the path you selected does not exist or is inaccessible on local device/using current user.") #EndRegion --- CodeWizard generated code End --- Else GUICtrlSetState ( $Button1, 128 ) GUICtrlSetStyle ($List1, BitOR($LBS_NOTIFY, $LBS_MULTIPLESEL, $LBS_HASSTRINGS, $WS_VSCROLL, $WS_BORDER)) $thelist = _FileListToArrayRec ( $use, "*", Default, Default, $FLTAR_SORT, Default ) For $i = 1 To $thelist[0] Step 1 GUICtrlSetData ( $List1, $thelist[$i] ) Next EndIf Case $List1 $listselect = _GUICtrlListBox_GetSelItemsText ( $List1 ) If $listselect[0] > 0 Then GUICtrlSetState ( $Button1, 64 ) Else GUICtrlSetState ( $Button1, 128 ) EndIf Case $Button1 $listsel = _GUICtrlListBox_GetSelItemsText ( $List1 ) For $f = 1 To $listsel[0] Step 1 apps ($listsel[$f]) Next Case $Button2 Exit EndSwitch WEnd Func addPath () If Not FileExists ( @ScriptDir & "\settings.ini" ) Then #Region --- CodeWizard generated code Start --- ;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Info MsgBox($MB_OK + $MB_ICONASTERISK,"Select paths","You need to select some file share paths so program knows where to look") #EndRegion --- CodeWizard generated code End --- $hold = 0 Else $hold = IniReadSection ( @ScriptDir & "\settings.ini", "settings" ) EndIf Do $init = FileSelectFolder ( "Select path to add", "" ) If $init <> "" Then If IsArray ( $hold ) Then $hold[0][0] += 1 IniWrite ( @ScriptDir & "\settings.ini", "settings", "path " & $hold[0][0], $init ) Else $hold += 1 IniWrite ( @ScriptDir & "\settings.ini", "settings", "path " & $hold, $init ) EndIf EndIf #Region --- CodeWizard generated code Start --- ;MsgBox features: Title=Yes, Text=Yes, Buttons=Yes and No, Icon=None If Not IsDeclared("continue") Then Local $continue $continue = MsgBox($MB_YESNO,"More?","Are there any more you would like to enter?") Until $continue = $IDNO $array = IniReadSection ( @ScriptDir & "\settings.ini", "settings" ) Local $store[$array[0][0]] $here = 0 For $i = 1 To $array[0][0] Step 1 GUICtrlSetData ( $Combo1, $array[$i][1] ) ;$here += 1 Next ;GUICtrlSetData ( $Combo1, $store ) EndFunc #EndRegion --- CodeWizard generated code End --- Func doubleClick($hWnd, $Msg, $wParam, $lParam) $nNotifyCode = BitShift($wParam, 16) $nID = BitAND($wParam, 0x0000FFFF) $hCtrl = $lParam Select Case $nID = $List1 Switch $nNotifyCode Case $LBN_DBLCLK $one = GUICtrlRead($List1) If StringRight ( $one, 4 ) == ".exe" Or StringRight ( $one, 4 ) == ".msi" Or StringRight ( $one, 4 ) == ".zip" Or StringRight ( $one, 4 ) == ".rar" Or StringRight ( $one, 3 ) == ".7z" Then apps ($one) EndIf EndSwitch EndSelect EndFunc Func apps ($file) $filepath = GUICtrlRead ( $Combo1 ) $setfile = $filepath & "\" & $file Select Case StringRight($setfile, 3) == "msi" RunWait(@ComSpec & " /c " & 'msiexec.exe /i ' & '"' & $setfile & '"' & ' /qb', "", @SW_HIDE) Case StringRight($setfile, 3) == "zip" Global $stringSPL = StringSplit($setfile, ".", $STR_NOCOUNT) _Zip_UnzipAll($setfile, @DesktopDir & "\short\" & $stringSPL[0], 532) Case StringRight($setfile, 3) == "rar" Local $rarsplit = StringSplit($setfile, ".", $STR_NOCOUNT) RunWait(@ComSpec & " /c " & 'unrar e "' & $setfile & '" "' & @DesktopDir & '\short\' & $rarsplit[0] & '\"', "C:\Program Files\WinRAR") Case StringRight($setfile, 2) == "7z" Local $7split = StringSplit($setfile, ".", $STR_NOCOUNT) RunWait(@ComSpec & " /c " & '7z x "' & $setfile & '" -o"' & @DesktopDir & '\short\' & $7split[0] & '\"', "C:\Program Files\7-Zip") Case StringRight($setfile, 11) == "application" ShellExecuteWait($setfile, "", $filepath) Case StringRight($setfile, 3) == "msu" RunWait(@ComSpec & " /c " & 'wusa.exe ' & $setfile & ' /quiet /norestart', "" ) Case StringRight($setfile, 3) == "exe" If Not FileExists (@ScriptDir & "\peid.exe") Then FileInstall("C:\Users\whiggs\OneDrive\always script\mdt\PEiD.exe", @ScriptDir & "\peid.exe") EndIf Run ("peid.exe -hard " & '"' & $setfile & '"', @ScriptDir, @SW_HIDE ) WinWait ( "PEiD v0.95" ) $IDString= ControlGetText("PEiD v0.95", "", "[CLASS:Edit; INSTANCE:2]") Do Sleep (100) $IDString= ControlGetText("PEiD v0.95", "", "Edit2") Until ($IDString <>"Scanning...") And ($IDString <> "" ) WinClose ("PEiD v0.95") $foundsomething = "n" If StringInStr ($IDString, "Inno Setup") Then RunWait ( @ComSpec & " /c " & '"' & $setfile & '"' & " /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-", "", @SW_HIDE ) $foundsomething = "y" ElseIf StringInStr ( $IDString, "Delphi" ) Then RunWait ( @ComSpec & " /c " & '"' & $setfile & '"' & " /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-", "", @SW_HIDE ) $foundsomething = "y" ElseIf StringInStr ($IDString, "Wise") Then RunWait ( @ComSpec & " /c " & '"' & $setfile & '"' & " /s", "", @SW_HIDE ) ElseIf StringInStr ($IDString, "Nullsoft") Then RunWait ( @ComSpec & " /c " & '"' & $setfile & '"' & " /S", "", @SW_HIDE ) ElseIf StringInStr ($IDString, "Installshield 2003") Then ;ShellExecuteWait ( $setfile, "/s /v /qb", $filepath ) RunWait ( @ComSpec & ' /c "' & $setfile & ' /s /v"/qb"', "", @SW_HIDE ) Else RunWait ( @ComSpec & " /c " & '"' & $setfile & '"', "", @SW_HIDE ) EndIf Case Else Sleep (100) EndSelect EndFunc Zip.au3 peid.exe
    1 point
  5. jchd

    AutoItify-C PRE_Alpha no RC

    Points raised in same sequence. You'll need a variant datatype compatible with AutoIt own variant implementation, including Keyword, Function, UserFunction, Map (beta), Hwnd, Bool, Int32, Int64, Array, Binary, Ptr, Object, String, Double, DllStruct, at least as per current beta version. UTF8 is fundamentally different from UCS2. Google them to see why. "A" + 2 this never give you "C" since AutoIt + operators forces conversion to numeric datatypes. Number("A") gives 0, so "A" + 2 evaluates to 2. Not at all, see above. Try to work out by yourself the result of the following code: ConsoleWrite(("z" + 16) & @LF) ConsoleWrite(("€" + 10) & @LF) ConsoleWrite(("AutoIt" + 10) & @LF) ConsoleWrite(Chr(Int(StringToBinary("z")) + 16) & @LF) ConsoleWrite(Chr(Int(StringToBinary("€")) + 10) & @LF) ConsoleWrite(Chr(Int(StringToBinary("AutoIt")) + 10) & @LF) You're still seem to think that one character = one byte, but this is untrue. Again, in $i = 10 & "12.123" the & operator forces conversion of its arguments to strings, hence it means $i = "10" & "12.123" yielding $i = "1012.123" as a string. Be warned that if you mess with floats (actually doubles) as you intend, you'll hit approximation errors.
    1 point
  6. Things aren't that simple. Look at what gives a naive approach: Local $n = 4276803 ConsoleWrite(BinaryToString($n)) ConsoleWrite(@LF) Local $s = "ABCdef" ConsoleWrite(StringToBinary($s) & @LF) Windows platforms are little-endian, which means that integers (for instance) are stored with low-order bytes first. That's why your 4276803 value gets interpreted as "CBA". See the difference here: Local $n = 4276803 ConsoleWrite(Hex($n) & @LF) ConsoleWrite(Binary($n) & @LF) If you intend to thusly process random strings then you'll have to process them in a loop, character by character. StringToASCIIArray and StringFromASCIIArray will help then.
    1 point
  7. Jos

    Autoit Crashing periodicly

    Why start a new thread without finishing your previous one? There is still a question there. Jos
    1 point
  8. What I believe would be a better approach is a script that loops until a key is pressed, then calls a function to do your outlook thing. While 1 Sleep(10) If _IsPressed($key) Then FuncOutlookStuff() EndIf WEnd Func FuncOutlookStuff() ; stuff EndFunc ;==>FuncOutlookStuff This is how I would have approached it, building in safeguards and other exits as appropriate.
    1 point
  9. AutoBert

    Set my icons from memory

    Make a runable script using tempfiles for the icons. After i am sure somebody (mybe @UEZ himself) will help you to create the icons direct without the tempfiles
    1 point
  10. This part leaves me completely speechless.
    1 point
  11. ChrisL

    Transparent controls

    I started messing around with this when someone asked about transparent controls before. I made a function for creating transparent controls but martin below had a better way of doing it. (Thanks for the help martin!) The syntax is $hWnd = _GuiCtrlMakeTrans($ControlID as returned by GuiCtrlCreate....,[Transparency level (Optional can be set later by the returned hWnd )]) Returns a handle to the new Child gui There are a couple of functions which I can't get to work properly, one is UP/Down controls and the other is Radio Buttons This is the new function Func _GuiCtrlMakeTrans($iCtrlID,$iTrans=255) Local $pHwnd, $nHwnd, $aPos, $a $hWnd = GUICtrlGetHandle($iCtrlID);Get the control handle If $hWnd = 0 then Return SetError(1,1,0) $pHwnd = DllCall("User32.dll", "hwnd", "GetParent", "hwnd", $hWnd);Get the parent Gui Handle If $pHwnd[0] = 0 then Return SetError(1,2,0) $aPos = ControlGetPos($pHwnd[0],"",$hWnd);Get the current pos of the control If @error then Return SetError(1,3,0) $nHwnd = GUICreate("", $aPos[2], $aPos[3], $aPos[0], $aPos[1], 0x80000000, 0x00080000 + 0x00000040, $pHwnd[0]);greate a gui in the position of the control If $nHwnd = 0 then Return SetError(1,4,0) $a = DllCall("User32.dll", "hwnd", "SetParent", "hwnd", $hWnd, "hwnd", $nHwnd);change the parent of the control to the new gui If $a[0] = 0 then Return SetError(1,5,0) If NOT ControlMove($nHwnd,'',$hWnd,0,0) then Return SetError(1,6,-1);Move the control to 0,0 of the newly created child gui GUISetState(@SW_Show,$nHwnd);show the new child gui WinSetTrans($nHwnd,"",$iTrans);set the transparency If @error then Return SetError(1,7,0) GUISwitch($pHwnd[0]);switch back to the parent Gui Return $nHwnd;Return the handle for the new Child gui EndFunc Here is an example of the Help file Gui Example #include <GuiConstantsEx.au3> #include <AVIConstants.au3> #include <TreeViewConstants.au3> #include <WindowsConstants.au3> ; GUI GuiCreate("Sample GUI", 400, 400) GuiSetIcon(@SystemDir & "\mspaint.exe", 0) GUICtrlCreatePic(@WindowsDir & "\River Sumida.bmp",0,0,400,400,$WS_CLIPSIBLINGS) ; MENU GuiCtrlCreateMenu("Menu&One") GuiCtrlCreateMenu("Menu&Two") GuiCtrlCreateMenu("MenuTh&ree") GuiCtrlCreateMenu("Menu&Four") ; CONTEXT MENU $contextMenu = GuiCtrlCreateContextMenu() GuiCtrlCreateMenuItem("Context Menu", $contextMenu) GuiCtrlCreateMenuItem("", $contextMenu);separator GuiCtrlCreateMenuItem("&Properties", $contextMenu) ; PIC Local $sFile = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\logo4.gif" GuiCtrlCreatePic($sFile,0,20, 169,68) $pt = _GuiCtrlMakeTrans(-1); we'll set the transparecy later for this one GuiCtrlCreateLabel("Sample pic", 75, 21, 53, 15) GuiCtrlSetColor(-1,0xffffff) _GuiCtrlMakeTrans(-1,100) ; AVI Local $sFile = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\sampleAVI.avi" GuiCtrlCreateAvi($sFile,0, 180, 30, 32, 32, $ACS_AUTOPLAY) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateLabel("Sample avi", 170, 70) _GuiCtrlMakeTrans(-1,100) ; TAB GuiCtrlCreateTab(240, 20, 150, 70) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateTabItem("One") GuiCtrlCreateLabel("Sample Tab with tabItems", 250, 40) GuiCtrlCreateTabItem("Two") GuiCtrlCreateTabItem("Three") GuiCtrlCreateTabItem("") ; COMBO GuiCtrlCreatecombo("Sample Combo", 250, 100, 120, 100) _GuiCtrlMakeTrans(-1,100) ; PROGRESS GuiCtrlCreateProgress(60, 100, 150, 20) GuiCtrlSetData(-1, 60) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateLabel("Progress:", 5, 102) _GuiCtrlMakeTrans(-1,100) ; EDIT GuiCtrlCreateEdit(@CRLF & " Sample Edit Control", 10, 130, 150, 70) _GuiCtrlMakeTrans(-1,100) ; LIST GuiCtrlCreateList("", 5, 210, 100, 90) _GuiCtrlMakeTrans(-1,100) GuiCtrlSetData(-1, "a.Sample|b.List|c.Control|d.Here", "b.List") ; ICON GuiCtrlCreateIcon("shell32.dll", 1, 175, 140) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateLabel("Icon", 180, 180, 50, 20) _GuiCtrlMakeTrans(-1,100) ; LIST VIEW $listView = GuiCtrlCreateListView("Sample|ListView|", 110, 210, 110, 80) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateListViewItem("A|One", $listView) GuiCtrlCreateListViewItem("B|Two", $listView) GuiCtrlCreateListViewItem("C|Three", $listView) ; UPDOWN GuiCtrlCreateLabel("UpDown", 350, 135) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateInput("42", 350, 140, 40, 20) GuiCtrlCreateUpDown(-1) ; GROUP WITH RADIO BUTTONS GuiCtrlCreateGroup("Sample Group", 230, 120,110,80) GuiCtrlCreateRadio("Radio One", 250, 140, 80) GuiCtrlSetState(-1, $GUI_CHECKED) GuiCtrlCreateRadio("Radio Two", 250, 165, 80) GUICtrlCreateGroup ("",-99,-99,1,1) ;close group ; LABEL GuiCtrlCreateLabel("Green" & @CRLF & "Label", 350, 185, 40, 40) GuiCtrlSetBkColor(-1, 0x00FF00) _GuiCtrlMakeTrans(-1,100) ; SLIDER GuiCtrlCreateLabel("Slider:", 235, 235) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateSlider(270, 230, 120, 30) _GuiCtrlMakeTrans(-1,100) GuiCtrlSetData(-1, 30) ; INPUT GuiCtrlCreateInput("Sample Input Box", 235, 275, 130, 20) _GuiCtrlMakeTrans(-1,100) ; DATE GuiCtrlCreateDate("", 5, 300, 200, 20) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateLabel("(Date control expands into a calendar)", 10, 325, 200, 20) _GuiCtrlMakeTrans(-1,100) ; BUTTON $Button = GuiCtrlCreateButton("Sample Button", 10, 350, 100, 30) _GuiCtrlMakeTrans(-1,100) ; CHECKBOX GuiCtrlCreateCheckbox("Checkbox", 130, 355, 80, 20) GuiCtrlSetState(-1, $GUI_CHECKED) _GuiCtrlMakeTrans(-1,100) ; TREEVIEW ONE $treeOne = GuiCtrlCreateTreeView(210, 310, 80, 80) _GuiCtrlMakeTrans(-1,100) $treeItem = GuiCtrlCreateTreeViewItem("TreeView", $treeOne) GuiCtrlCreateTreeViewItem("Item1", $treeItem) GuiCtrlCreateTreeViewItem("Item2", $treeItem) GuiCtrlCreateTreeViewItem("Foo", -1) GuiCtrlSetState($treeItem, $GUI_EXPAND) ; TREEVIEW TWO $treeTwo = GuiCtrlCreateTreeView(295, 310, 103, 80, $TVS_CHECKBOXES) _GuiCtrlMakeTrans(-1,100) GuiCtrlCreateTreeViewItem("TreeView", $treeTwo) GuiCtrlCreateTreeViewItem("With", $treeTwo) GuiCtrlCreateTreeViewItem("tvs_checkboxes", $treeTwo) GuiCtrlSetState(-1, $GUI_CHECKED) GuiCtrlCreateTreeViewItem("Style", $treeTwo) ; GUI MESSAGE LOOP GuiSetState() ;change the transparency of the picture For $i = 255 to 0 step -1 WinSetTrans($pt,"",$i) Sleep(1) Next For $i = 0 to 100 WinSetTrans($pt,"",$i) Sleep(1) Next While 1 Switch GuiGetMsg() Case $GUI_EVENT_CLOSE Exit Case $Button Msgbox(0,"","Button still works!") EndSwitch WEnd Func _GuiCtrlMakeTrans($iCtrlID,$iTrans=255) Local $pHwnd, $nHwnd, $aPos, $a $hWnd = GUICtrlGetHandle($iCtrlID);Get the control handle If $hWnd = 0 then Return SetError(1,1,0) $pHwnd = DllCall("User32.dll", "hwnd", "GetParent", "hwnd", $hWnd);Get the parent Gui Handle If $pHwnd[0] = 0 then Return SetError(1,2,0) $aPos = ControlGetPos($pHwnd[0],"",$hWnd);Get the current pos of the control If @error then Return SetError(1,3,0) $nHwnd = GUICreate("", $aPos[2], $aPos[3], $aPos[0], $aPos[1], 0x80000000, 0x00080000 + 0x00000040, $pHwnd[0]);greate a gui in the position of the control If $nHwnd = 0 then Return SetError(1,4,0) $a = DllCall("User32.dll", "hwnd", "SetParent", "hwnd", $hWnd, "hwnd", $nHwnd);change the parent of the control to the new gui If $a[0] = 0 then Return SetError(1,5,0) If NOT ControlMove($nHwnd,'',$hWnd,0,0) then Return SetError(1,6,-1);Move the control to 0,0 of the newly created child gui GUISetState(@SW_Show,$nHwnd);show the new child gui WinSetTrans($nHwnd,"",$iTrans);set the transparency If @error then Return SetError(1,7,0) GUISwitch($pHwnd[0]);switch back to the parent Gui Return $nHwnd;Return the handle for the new Child gui EndFunc
    1 point
  12. OK, I got a solution: #include <IE.au3> $oIE = _IECreate() $hIE = _IEPropertyGet($oIE, "hwnd") ControlSend($hIE, "", "", "{F12}") Sleep(1000) ControlSend("[Class:F12FrameWindow]", "", "", "^{F7}{TAB 3}{down}{TAB 3}{up 9}{down}") _IENavigate($oIE, "http://www.google.com/#num=100&q=autoit+jon") WinActivate($hIE) MsgBox(262144, Default, "click 'OK' to close IE Test window", 0) _IEQuit($oIE)
    1 point
  13. It's called scientific notation.
    1 point
  14. @kctvt, you need to reformat the output, like this: $b = 1.09549 $a = 1.09548 $c = $b - $a MsgBox(0, '', StringFormat('%.5f', $c)) EDIT: to clarify, this is only a visual representation. the actual numeric value is correct, 1e-005 = 0.00001
    1 point
  15. jchd

    AutoItify-C PRE_Alpha no RC

    Im' afraid you need to start at a higher level. Start with an AutoIt grammar parser, else you'll get stuck pretty soon. Note that AutoIt strings are UCS2-encoded (Unicode restricted to plane 0). Also AutoIt variables are not fixed type: Local $a = 3 & ".1415926" ConsoleWrite(-$a + '3' & @TAB & $a ^ 2 & @LF) ; also _CubeMe($a) ConsoleWrite($a & @LF) Func _CubeMe(ByRef $n) $n = $n ^ 3 EndFunc ; also Local $aTricky[Random(2, 5, 1][Floor($a)] ... and more pitfalls. Then you'll have to rewrite all internal functions and deal with non-native C datatypes and much more.
    1 point
  16. Sure you can, just open up a script and click Tools -> Scite Config (or Ctrl + 1) Here's my properties, preview in the pic
    1 point
  17. Just some initial feedback. In Func _ProcessListFiles... For $n = 1 To $aFiles[0] ; <-- Possible type mismatch? Typically this is how to enumerate through an array. The author is expecting that the array $aFiles was populated in a manner which set the first array element is the count of array indecies (Ubound). AFAIK, an array in AutoIt can have any data type, so even if you expect strings, the first element could (and in this case should) be an integer. If $sPath Then ; <-- $sPath is a string and certainly not a logical True/False. What is the result of this test? Doesn't look like the most useful condition eval, I agree. In this case I assume the author is just checking to see that the variable was set to something that is <> 0 and <> "". Here's how I understand the conditional. If $var = 0 OrAnd $var = "" Then Return False If $var <> 0 AndOr $var <> "" Then Return True edit: wrong logic ^ In Func __IsFileObject... There are several bald "Return" statements which do not specify True or False or any other return value, even though the calling code treats the return code as a logical value. Another assumption...thinking the author just took the shortcut to not specify a return value that is not false. Per the help file, when using, "... the Return keyword to exit the function...user-defined functions return 0 unless another return value is specified."
    1 point
×
×
  • Create New...