Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 06/23/2014 in all areas

  1. If you ever tried to download some remote file from your script using InetGet(), InetRead() or other download functions, you probably noticed that the download speed was never as good as when you try to download the same file using a download manager. But that's over, you won't lack the high speed download capability of download managers in your AutoIt scripts, not anymore. Enjoy: #include-once #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <WinHttp.au3> ;http://www.autoitscript.com/forum/topic/84133-winhttp-functions/ #include <FileConstants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: UrlDownloadEx ; Description ...: Download a single file by splitting it in segments and using several simultaneous connections to download these segments from a single server. ; Syntax ........: UrlDownloadEx($sUrl [, $sFileName = Default [, $iNumberOfConnections = Default [, $CallbackFunction = Default [, $vParam = 0]]]]) ; Parameters ....: $sUrl - Url of the file to download. ; $sFileName - [optional] Local filename to download to. Default is "". ; $iNumberOfConnections - [optional] Number of simultaneous connections. Default is 8. ; $CallbackFunction - [optional] An application-defined callback function to call when progress is made. Default is Null. ; $vParam - [optional] An application-defined value to be passed to the callback function. Default is 0. ; Return values .: Success - Returns a binary string if file name is not supplied. ; - Returns 1 if file name is supplied ; - @extended receives the number of received bytes. ; Failure - Returns 0 and sets @error: ; |1 - Invalid number of connections ; |2 - Invalid callback function ; |3 - Invalid url ; |4 - _WinHttpOpen failed ; |5 - _WinHttpConnect failed ; |6 - _WinHttpOpenRequest failed ; |7 - _WinHttpSendRequest failed ; |8 - _WinHttpReceiveResponse failed ; |9 - _WinHttpQueryHeaders failed ; |10 - _WinHttpOpenRequest failed, while trying to create multiple request ; |11 - _WinHttpQueryHeaders failed, while trying to prepare multiple request ; |12 - Not enough memory to allocate buffer ; |13 - _WinHttpQueryDataAvailable failed ; |14 - Download aborted, callback function returned False ; |15 - FileOpen failed ; |16 - FileWrite failed ; Author ........: FaridAgl ; Remarks .......: The Url parameter should be in the form "http://www.somesite.com/path/file.html", just like an address you would type into your web browser. ; + Please use a high number of connections with caution. Some servers may have specific limitations for a number of connections from one client, when the number of connections is high, the servers can put your computer to a black list. ; + For some connection types or for low performance computers or routers, the download speed may decrease when you increase the number of connections. ; + The callback function is called with the following parameters: $iReceivedBytes, $iTotalReceivedBytes, $iDownloadSize, $vParam ; + To continue downloading, the callback function must return True; to stop downloading, it must return False. ; Example .......: Yes ; =============================================================================================================================== Func UrlDownloadEx($sUrl, $sFileName = Default, $iNumberOfConnections = Default, $CallbackFunction = Default, $vParam = 0) If ($sFileName = Default Or $sFileName = -1) Then $sFileName = "" If ($iNumberOfConnections = Default Or $iNumberOfConnections = -1) Then $iNumberOfConnections = 8 ElseIf (IsInt($iNumberOfConnections) = 0 Or $iNumberOfConnections < 1) Then Return SetError(1, 0, 0) EndIf If ($CallbackFunction = Default Or $CallbackFunction = -1) Then $CallbackFunction = Null Else If (IsFunc($CallbackFunction) = 0) Then Return SetError(2, 0, 0) EndIf Local $avUrlComponents = _WinHttpCrackUrl($sUrl, $ICU_DECODE) If ($avUrlComponents = 0) Then Return SetError(3, 0, 0) Local Const $SERVER_NAME = $avUrlComponents[2] Local Const $OBJECT_NAME = $avUrlComponents[6] Local Const $SERVER_PORT = $avUrlComponents[3] Local Const $FLAGS = ($avUrlComponents[1] = $INTERNET_SCHEME_HTTPS) ? ($WINHTTP_FLAG_SECURE) : (0) Local $hSession = _WinHttpOpen("Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))", $WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, $WINHTTP_NO_PROXY_NAME, $WINHTTP_NO_PROXY_BYPASS, 0) If ($hSession = 0) Then Return SetError(4, 0, 0) Local $hConnect = _WinHttpConnect($hSession, $SERVER_NAME, $SERVER_PORT) If ($hConnect = 0) Then _WinHttpCloseHandle($hSession) Return SetError(5, 0, 0) EndIf Local $hRequest = _WinHttpOpenRequest($hConnect, "GET", $OBJECT_NAME, 0, $WINHTTP_NO_REFERER, $WINHTTP_DEFAULT_ACCEPT_TYPES, $FLAGS) If ($hRequest = 0) Then _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(6, 0, 0) EndIf If (_WinHttpSendRequest($hRequest, "Range: bytes=0-0", $WINHTTP_NO_REQUEST_DATA, 0, 0) = 0) Then _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(7, 0, 0) EndIf If (_WinHttpReceiveResponse($hRequest) = 0) Then _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(8, 0, 0) EndIf Local $sStatusCode = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_STATUS_CODE, $WINHTTP_HEADER_NAME_BY_INDEX, $WINHTTP_NO_HEADER_INDEX) If (Int($sStatusCode, 1) <> $HTTP_STATUS_PARTIAL_CONTENT) Then _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(9, 0, 0) EndIf Local $sContentRange = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_RANGE, $WINHTTP_HEADER_NAME_BY_INDEX, $WINHTTP_NO_HEADER_INDEX) _WinHttpCloseHandle($hRequest) Local Enum $REQUEST_HANDLE, $RECEIVED_BYTES, $DOWNLOAD_OFFSET, $BYTES_TO_DOWNLOAD Local $avConnections[$iNumberOfConnections][4] Local $iDownloadSize = Int(StringTrimLeft($sContentRange, StringLen("bytes 0-0/")), 1) Local $iDownloadSizePerConnection = Floor($iDownloadSize / $iNumberOfConnections) Local $iLastByteToDownload = 0 For $i = 0 To $iNumberOfConnections - 1 $avConnections[$i][$REQUEST_HANDLE] = _WinHttpOpenRequest($hConnect, "GET", $OBJECT_NAME, 0, $WINHTTP_NO_REFERER, $WINHTTP_DEFAULT_ACCEPT_TYPES, $FLAGS) If ($avConnections[$i][$REQUEST_HANDLE] = 0) Then For $j = $i - 1 To 0 Step -1 _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(10, 0, 0) EndIf $avConnections[$i][$RECEIVED_BYTES] = 0 $avConnections[$i][$DOWNLOAD_OFFSET] = $i * $iDownloadSizePerConnection If ($i <> $iNumberOfConnections - 1) Then $avConnections[$i][$BYTES_TO_DOWNLOAD] = $iDownloadSizePerConnection $iLastByteToDownload = $avConnections[$i][$DOWNLOAD_OFFSET] + $iDownloadSizePerConnection - 1 Else $avConnections[$i][$BYTES_TO_DOWNLOAD] = $iDownloadSizePerConnection + Mod($iDownloadSize, $iNumberOfConnections) $iLastByteToDownload = $iDownloadSize - 1 EndIf _WinHttpSendRequest($avConnections[$i][$REQUEST_HANDLE], "Range: bytes=" & $avConnections[$i][$DOWNLOAD_OFFSET] & "-" & $iLastByteToDownload, $WINHTTP_NO_REQUEST_DATA, 0, 0) _WinHttpReceiveResponse($avConnections[$i][$REQUEST_HANDLE]) $sStatusCode = _WinHttpQueryHeaders($avConnections[$i][$REQUEST_HANDLE], $WINHTTP_QUERY_STATUS_CODE, $WINHTTP_HEADER_NAME_BY_INDEX, $WINHTTP_NO_HEADER_INDEX) If (Int($sStatusCode, 1) <> $HTTP_STATUS_PARTIAL_CONTENT) Then For $j = $i - 1 To 0 Step -1 _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(11, 0, 0) EndIf Next Local $tBuffer = DllStructCreate("BYTE[" & $iDownloadSize & "]") If (@error) Then Return SetError(12, 0, 0) Local $pBuffer = DllStructGetPtr($tBuffer) Local $iTotalReceivedBytes = 0 While (True) For $i = 0 To $iNumberOfConnections - 1 If ($avConnections[$i][$RECEIVED_BYTES] = $avConnections[$i][$BYTES_TO_DOWNLOAD]) Then ContinueLoop If (_WinHttpQueryDataAvailable($avConnections[$i][$REQUEST_HANDLE]) = 1) Then $iTotalReceivedBytes += @extended _WinHttpReadData($avConnections[$i][$REQUEST_HANDLE], 2, @extended, $pBuffer + $avConnections[$i][$DOWNLOAD_OFFSET] + $avConnections[$i][$RECEIVED_BYTES]) $avConnections[$i][$RECEIVED_BYTES] += @extended If ($CallbackFunction <> Null) Then If ($CallbackFunction(@extended, $iTotalReceivedBytes, $iDownloadSize, $vParam) = False) Then For $j = 0 To $iNumberOfConnections - 1 If ($avConnections[$j][$REQUEST_HANDLE] > 0) Then _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(14, 0, 0) EndIf EndIf If ($avConnections[$i][$RECEIVED_BYTES] = $avConnections[$i][$BYTES_TO_DOWNLOAD]) Then _WinHttpCloseHandle($avConnections[$i][$REQUEST_HANDLE]) $avConnections[$i][$REQUEST_HANDLE] = 0 EndIf Else For $j = 0 To $iNumberOfConnections - 1 If ($avConnections[$j][$REQUEST_HANDLE] > 0) Then _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(13, 0, 0) EndIf Next If ($iTotalReceivedBytes = $iDownloadSize) Then _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) ExitLoop EndIf WEnd If ($sFileName <> "") Then Local $hFile = FileOpen($sFileName, BitOR($FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY)) If ($hFile = -1) Then Return SetError(15, 0, 0) If (FileWrite($hFile, DllStructGetData($tBuffer, 1)) = 0) Then FileClose($hFile) Return SetError(16, 0, 0) EndIf FileClose($hFile) Return SetError(0, $iDownloadSize, 1) EndIf Return SetError(0, $iDownloadSize, DllStructGetData($tBuffer, 1)) EndFunc Example 1 - Download to a local file and use callback function to monitor and control the download progress: #include "UrlDownloadEx.au3" Global $t1 = TimerInit() Global $vResult = UrlDownloadEx("http://www.autoitscript.com/files/autoit3/autoit-v3-setup.exe", @ScriptDir & "\AutoIt 3.3.12.0.exe", Default, BytesReceived) ConsoleWrite(StringFormat("Result:\t%s\nSize:\t%u\nError:\t%u\nTimer:\t%u\n", $vResult, @extended, @error, TimerDiff($t1))) Func BytesReceived($iReceivedBytes, $iTotalReceivedBytes, $iDownloadSize, $vParam) If ($iTotalReceivedBytes >= 2 * 1024 * 1024) Then Return False ;Stop downloading ConsoleWrite(StringFormat("%u bytes received.\n%u/%u\nParam: %s\n\n", $iReceivedBytes, $iTotalReceivedBytes, $iDownloadSize, $vParam)) Return True ;Continue downloading EndFunc Example 2 - Download to memory and set the number of connections to 2: #include "UrlDownloadEx.au3" Global $t1 = TimerInit() Global $vResult = UrlDownloadEx("http://icdn4.digitaltrends.com/image/microsoft_xp_bliss_desktop_image-650x0.jpg", Default, 2) ConsoleWrite(StringFormat("Result:\t%s\nSize:\t%u\nError:\t%u\nTimer:\t%u\n", $vResult, @extended, @error, TimerDiff($t1))) You can also download all of the above scripts at once: UrlDownloadEx + Examples.zip
    1 point
  2. yousefsamy, Do you want the HotKey to act in the same way as the button and take a screenshot? If so you can do it like this: #include <GUIConstantsEx.au3> #include <ScreenCapture.au3> ; Run the function when you press the HotKey HotKeySet("d", "_ScreenShot") $Form1 = GUICreate("Form1", 468, 293, 192, 124) $Button1 = GUICtrlCreateButton("Button1", 48, 32, 369, 209) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 ; Run the function when you press the button _ScreenShot() EndSwitch WEnd Func _Screenshot() ; Take and dispaly the screenshot _ScreenCapture_Capture(@DesktopDir & "\11.jpg") ShellExecute (@DesktopDir & "\11.jpg") EndFunc All clear? M23
    1 point
  3. ConsoleWrite($o&@CRLF) This was here only for debugging purposes - I was using this to see what value the variable $o was returning. To see it in action just run the script and watch the console in the bottom section of SciTE (assuming you are using SciTE to code). You will notice that when you open an image the value of $o is the full directory path including the file name (e.g. C:WindowsExample.jpg).Alternatively you could display a MsgBox that shows the same thing: e.g. MsgBox(0,'',$o) would show a MsgBox with the text C:WindowsExample.jpg. ConsoleWrite is just a bit less intrusive, but both are good for debugging It's definitely good to utilise ConsoleWrite whilst writing code to make sure you are working with the right values! GDI Plus is a library that works with images/graphics - someone has worked hard to write all the code for us to use to make it simpler. For example you could spend hours working our how to get the image dimensions but someone has already done this and has written a function called _GDIPlus_ImageGetWidth() I don't really know much more about GDI Plus to be honest, I usually use these forums to find examples and also look at Help files (very important!!) You can actually click on every one of those functions right here in the forum and it would take you to a page explaining what each one does. That's the best place to start to understand how each function works. WinMove allows us to change the size of the GUI (and even move the GUI around if we need to), but in our case we just resize the GUI to fit the image. Hope this helps
    1 point
  4. recently, i was getting IE UDF errors when i was using functions like '_IEAction($object,"click")' in Win8. i searched forum and saw people that had same issues with me. i installed xp in virtualbox and tried my script there. woala! it is working. then i found this solve: - press ALT in IE to see top menus. - go to Tools > Compatibility View Settings > open "set Compatibility View for all web sites" (i dont know its english exactly) if u do that, you can use IE UDF in win7, win8 and win8.1 with no problem.
    1 point
  5. This will resize the GUI and the Pic control to fit the image dimensions (with optional scaling): #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> _GDIPlus_Startup() $iWidth = 500 $iHeight = 500 $iScale = 1 ;set scaling from 0.1 to x (e.g. 2 = scale image to 200% original size) $hGUI = GUICreate("Form1", $iWidth, $iHeight, @DesktopWidth/2 - $iWidth/2, @DesktopHeight/2 - $iHeight/2) $file = GUICtrlCreateMenu("&File") $open = GUICtrlCreateMenuItem("open"&@TAB&"", $file) $save = GUICtrlCreateMenuItem("save", $file) $hPic = GUICtrlCreatePic("", 0, 0, 0, 0) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _GDIPlus_Shutdown() Exit Case $open ;here ,, is it right to allow me view images as i like or ?? $o = FileOpenDialog("File Open", @DesktopDir , " TXT (*.jpg) " ) ConsoleWrite($o&@CRLF) $hImage = _GDIPlus_ImageLoadFromFile($o) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) _GDIPlus_ImageDispose($hImage) WinMove($hGUI, '', Default, Default, $iX * $iScale, $iY * $iScale + 45) GUICtrlSetPos($hPic, 0, 0, $iX * $iScale, $iY * $iScale) GUICtrlSetImage($hPic, $o) EndSwitch WEnd
    1 point
  6. Danyfirex

    CredWrite API

    I really don't know why the structure has no the data(I just see a little bit), but if you do something like this can see that you want: Func CredRead($Target) Local $targetName = DllStructCreate("wchar[100]") DllStructSetData($targetName,1,$Target) Local $Comment = DllStructCreate("wchar[100]") Local $userName = DllStructCreate("wchar[100]") Local $credentialBlob = DllStructCreate("wchar[100]") Local $structCREDENTIAL= "" & _ "DWORD Flags;" & _ "DWORD Type;" & _ "Ptr TargetName;" & _ "Ptr Comment;" & _ "UINT64 LastWritten;" & _ "DWORD CredintialBlobSize;" & _ "Ptr CredentialBlob;" & _ "DWORD Persist;" & _ "DWORD AttributeCount;" & _ "ptr Attributes;" & _ "Ptr TargetAlias;" & _ "Ptr Username" ;~ Local $OutCred = DllStructCreate($structCREDENTIAL) ;~ DllStructSetData($OutCred,"TargetName",DllStructGetPtr($targetName)) ;~ DllStructSetData($OutCred,"UserName",DllStructGetPtr($userName)) ;~ DllStructSetData($OutCred,"CredentialBlob",DllStructGetPtr($credentialBlob)) ;~ DllStructSetData($OutCred,"Comment",DllStructGetPtr($Comment)) Local $hAdvapi32 = DllOpen("Advapi32.dll") $Ret = DllCall($hAdvapi32, 'bool', 'CredReadW', 'ptr', DllStructGetPtr($targetName), 'dword', 1, 'dword', 0, 'ptr*', 0) _ArrayDisplay($Ret) Local $tdata=DllStructCreate("byte[200]",$Ret[4]) FileWrite("data.txt",(DllStructGetData($tdata,1))) ShellExecute("data.txt") Return 0 EndFunc Saludos
    1 point
  7. You need to find out the size of the image and then make the GUI the same size, or stretch the image if it's too small to fit.
    1 point
  8. Have a look here: http://www.autoitscript.com/autoit3/docs/functions/FileRead.htm FileRead returns the binary/string read. @extended is set to the number of bytes/characters returned. It is most ideal to use for text files, not images.
    1 point
  9. FileRead reads the contents of the file, FileOpenDialog returns the filename and GUICtrlSetImage requires a file name, not the contents of the file.
    1 point
  10. Try: #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form1_1 = GUICreate("Form1", 660, 444, 192, 145) $file = GUICtrlCreateMenu("&File") $open = GUICtrlCreateMenuItem("open"&@TAB&"", $file) $save = GUICtrlCreateMenuItem("save", $file) $Pic1 = GUICtrlCreatePic("", 16, 56, 561, 329) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $open ;here ,, is it right to allow me view images as i like or ?? $o = FileOpenDialog("File Open", @DesktopDir , " TXT (*.jpg) " ) GUICtrlSetImage( $Pic1 , $o) EndSwitch WEnd
    1 point
  11. Hi rootx If you want put a quote Do like this : '"' ShellExecute(@SystemDir&"\cmd.exe"," convert *.jpg -pointsize 20 -gravity Southeast -draw "&'"'&"fill white text 0,0 'banner'"&'"'&' banner.jpg',"c:\","")
    1 point
  12. trancexx

    Removal of PlugIn

    That is not true. And remark about improved stability is valid only from philosophical aspect. If you define the more stable code as code with lesser functionality then yes, general code stability is improved. But that would also mean that the most stable code would be no_code or simple comment. Late great David Huffman was able to reduce the whole computer to one logic gate that way. It worked, though not much.
    1 point
×
×
  • Create New...