Leaderboard
Popular Content
Showing content with the highest reputation on 11/22/2021 in all areas
-
Copying Data to/from C# or VB.NET Through IntPtr
Earthshine and 2 others reacted to LarsJ for a topic
StructuresWith the Marshal.StructureToPtr( Object, IntPtr, Boolean ) method, a structure can be copied from C#/VB.NET to AutoIt. With the Marshal.PtrToStructure(IntPtr, Type) method, a structure can be copied from AutoIt to C#/VB.NET. This is demonstrated in Example01 in \Examples\5) Structures\: #AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #AutoIt3Wrapper_UseX64=Y Opt( "MustDeclareVars", 1 ) #include <StructureConstants.au3> #include "..\..\Includes\DotNetAll.au3" Example() Func Example() Local $oNetCode = DotNet_LoadCScode( FileRead( "Example01.cs" ), "System.dll" ) Local $oTestClass = DotNet_CreateObject( $oNetCode, "TestClass" ) Local $pPtr = $oTestClass.Test1() Local $tPoint = DllStructCreate( $tagPOINT, $pPtr ) ConsoleWrite( "AutoIt: The value of first point is " & $tPoint.X & " and " & $tPoint.Y & @CRLF ); $tPoint.X = 3 $tPoint.Y = 4 ConsoleWrite( "AutoIt: The value of new point is " & $tPoint.X & " and " & $tPoint.Y & @CRLF ); $oTestClass.Test2() EndFunc using System; using System.Runtime.InteropServices; public struct Point { public int x; public int y; } class TestClass { // Global in class IntPtr pnt; public String Test1() { // Create a point struct Point p; p.x = 1; p.y = 2; Console.WriteLine( "CSharp: The value of first point is " + p.x + " and " + p.y ); // Initialize unmanged memory to hold the struct pnt = Marshal.AllocHGlobal( Marshal.SizeOf(p) ); // Copy the struct to unmanaged memory Marshal.StructureToPtr( p, pnt, false ); // Return memory pointer to AutoIt return pnt.ToString(); } public void Test2() { // Create another point Point anotherP; // Set this Point to the value of the Point in unmanaged memory anotherP = (Point)Marshal.PtrToStructure( pnt, typeof(Point) ); Console.WriteLine( "CSharp: The value of new point is " + anotherP.x + " and " + anotherP.y ); // Free the unmanaged memory Marshal.FreeHGlobal( pnt ); } } Example02 and Example03Example03 uses a structure that contains strings, integers and floating point numbers (doubles). In the example, 50,000, 100,000 and 250,000 instances of this structure are generated and copied from .NET to AutoIt. The purpose is to get an idea of how fast such structures can be copied with this data copying technique. The data copying technique is compared to the data passing technique used in Using C# and VB Code. Example02 passes 2d arrays from .NET to AutoIt where the columns match the data fields in the structure. In the example, arrays with 50,000, 100,000 and 250,000 rows are generated and passed. Both Example02 and Example03 are based on existing code to generate random data of types Strings, Integers, Floats, Dates, Times and Rows/Cols. See e.g. Real C# and VB examples. See also Random2dArray.txt. Example02 implementation #AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #AutoIt3Wrapper_UseX64=Y Opt( "MustDeclareVars", 1 ) #include <Array.au3> #include "Random2dArray.au3" Example( 100, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Example( 50000, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Example( 100000, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Example( 250000, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Func Example( _ $iRows, _ ; Number of rows in the array $aColumns, _ ; Number of columns and data types in the array, see 1) in docu $sSource = "" ) ; Character source for string columns, random strings are created from these characters ; Load .NET code, create object Local $oNetCode = DotNet_LoadVBcode( FileRead( "Example02.vb" ), "System.dll" ) Local $oRand2dArrayClass = DotNet_CreateObject( $oNetCode, "Rand2dArrayClass" ) ; Check $aColumns array Local $iError = CheckColumnsArray( $aColumns ) If $iError Then Return SetError($iError,0,0) ; Define $aColTypes array Local $iColumns = UBound( $aColumns ) Local $aColTypes[6][$iColumns+1] ; Update $aColTypes and $aColumns For $i = 0 To $iColumns - 1 If CalcColTypes( $i, $aColumns, $aColTypes, $iRows, $sSource ) Then ContinueLoop $aColumns[$i][2] += 1 ; Because of .NET Random class ; Convert duplicates as a percentage to number of unique rows $aColumns[$i][3] = $iRows - Int( $iRows * $aColumns[$i][3] / 100 ) If $aColumns[$i][3] And $aColumns[$i][3] < $iRows Then $aColumns[$i][3] -= 1 Next ; Visual code verification If $iRows = 100 Then Local $aArray100 = $oRand2dArrayClass.Rand2dArray( $iRows, $aColumns, $aColTypes, $sSource ) _ArrayDisplay( $aArray100 ) Return EndIf Local $hTimer = TimerInit() Local $aArray = $oRand2dArrayClass.Rand2dArray( $iRows, $aColumns, $aColTypes, $sSource ) ConsoleWrite( TimerDiff( $hTimer ) & @CRLF ) #forceref $aArray EndFunc #cs 180.043279980917 387.024483304017 966.513008235719 #ce Imports System Class Rand2dArrayClass Public Function Rand2dArray( iRows As Integer, aColumns As Object(,), aColTypes As Object(,), sSource As String ) As Object(,) Dim iColumns As Integer = aColumns.GetUpperBound(1) + 1 Dim aSource As Char() = sSource.ToCharArray() Dim iSourceLen As Integer = sSource.Length Dim aObjects(iColumns-1,iRows-1) Dim oRnd As New Random() Dim sStr As String = "" Dim k, y, m, dMax As Integer For i As Integer = 0 To iRows - 1 'String columns For j As Integer = 1 To aColTypes(0,0) k = aColTypes(j,0) For l As Integer = 1 To oRnd.Next( aColumns(1,k), aColumns(2,k) ) sStr &= aSource(oRnd.Next( iSourceLen )) Next aObjects(k,i) = sStr sStr = "" Next 'Integer columns For j As Integer = 1 To aColTypes(0,1) k = aColTypes(j,1) aObjects(k,i) = oRnd.Next( aColumns(1,k), aColumns(2,k) ) Next 'Float columns For j As Integer = 1 To aColTypes(0,2) k = aColTypes(j,2) aObjects(k,i) = aColumns(1,k) + ( aColumns(2,k) - aColumns(1,k) ) * oRnd.NextDouble() Next 'Date columns For j As Integer = 1 To aColTypes(0,3) k = aColTypes(j,3) y = oRnd.Next( aColumns(1,k), aColumns(2,k) ) m = oRnd.Next( 1, 13 ) Select m Case 1, 3, 5, 7, 8, 10, 12 dMax = 31 Case 4, 6, 9, 11 dMax = 30 Case Else '2 dMax = If( y Mod 4 <> 0, 28, If( y Mod 400 = 0, 29, If( y Mod 100 = 0, 28, 29 ) ) ) End Select aObjects(k,i) = y * 10000 + m * 100 + oRnd.Next( 1, dMax + 1 ) Next 'Time columns For j As Integer = 1 To aColTypes(0,4) k = aColTypes(j,4) aObjects(k,i) = oRnd.Next( aColumns(1,k), aColumns(2,k) ) * 10000 + oRnd.Next( 0, 60 ) * 100 + oRnd.Next( 0, 60 ) Next 'Row/col columns For j As Integer = 1 To aColTypes(0,5) k = aColTypes(j,5) aObjects(k,i) = i & "/" & k Next Next Return aObjects End Function End Class Structure definitionThe structure in Example03.vb is defined this way: Public Structure Test Public iString As Integer 'String length Public sString As String Public iInteger As Integer Public dDouble As Double Public iDate As Integer Public iTime As Integer Public iRowCol As Integer 'String length Public sRowCol As String End Structure When a string is copied from the .NET structure to a corresponding AutoIt structure, only a string pointer is copied to the structure, while the string itself is stored in memory outside the structure. Therefore, AutoIt code must be used this way to read the string: $tString = DllStructCreate( "char sString[" & $tStruct.iString & "]", $tStruct.pString ) ConsoleWrite( "$tString.sString = " & $tString.sString & @CRLF ) Example03 implementation #AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #AutoIt3Wrapper_UseX64=Y Opt( "MustDeclareVars", 1 ) #include "Random2dArray.au3" Example( 100, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Example( 50000, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Example( 100000, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Example( 250000, "sifdtr", "abcdefghijklmnopqrstuvwxyz" ) Func Example( _ $iRows, _ ; Number of rows in the array $aColumns, _ ; Number of columns and data types in the array, see 1) in docu $sSource = "" ) ; Character source for string columns, random strings are created from these characters ; Load .NET code, create object Local $oNetCode = DotNet_LoadVBcode( FileRead( "Example03.vb" ), "System.dll" ) Local $oRand2dArrayClass = DotNet_CreateObject( $oNetCode, "Rand2dArrayClass" ) ; Check $aColumns array Local $iError = CheckColumnsArray( $aColumns ) If $iError Then Return SetError($iError,0,0) ; Define $aColTypes array Local $iColumns = UBound( $aColumns ) Local $aColTypes[6][$iColumns+1] ; Update $aColTypes and $aColumns For $i = 0 To $iColumns - 1 If CalcColTypes( $i, $aColumns, $aColTypes, $iRows, $sSource ) Then ContinueLoop $aColumns[$i][2] += 1 ; Because of .NET Random class ; Convert duplicates as a percentage to number of unique rows $aColumns[$i][3] = $iRows - Int( $iRows * $aColumns[$i][3] / 100 ) If $aColumns[$i][3] And $aColumns[$i][3] < $iRows Then $aColumns[$i][3] -= 1 Next ; Visual code verification If $iRows = 100 Then Local $pPtr100 = $oRand2dArrayClass.Rand2dArray( $iRows, $aColumns, $aColTypes, $sSource ), $tStruct, $tString, $iSize = @AutoItX64 ? 56 : 40 ConsoleWrite( "$pPtr100 = " & Ptr( $pPtr100 ) & @CRLF & @CRLF ) ; Display first structure ConsoleWrite( "First structure:" & @CRLF ) $tStruct = DllStructCreate( "int iString;ptr pString;int iInteger;double dDouble;int iDate;int iTime;int iRowCol;ptr pRowCol", $pPtr100 ) ConsoleWrite( "$tStruct.iString = " & $tStruct.iString & @CRLF ) ConsoleWrite( "$tStruct.pString = " & Ptr( $tStruct.pString ) & @CRLF ) $tString = DllStructCreate( "char sString[" & $tStruct.iString & "]", $tStruct.pString ) ConsoleWrite( "$tString.sString = " & $tString.sString & @CRLF ) ConsoleWrite( "$tStruct.iInteger = " & $tStruct.iInteger & @CRLF ) ConsoleWrite( "$tStruct.dDouble = " & $tStruct.dDouble & @CRLF ) ConsoleWrite( "$tStruct.iDate = " & $tStruct.iDate & @CRLF ) ConsoleWrite( "$tStruct.iTime = " & $tStruct.iTime & @CRLF ) ConsoleWrite( "$tStruct.iRowCol = " & $tStruct.iRowCol & @CRLF ) ConsoleWrite( "$tStruct.pRowCol = " & Ptr( $tStruct.pRowCol ) & @CRLF ) $tString = DllStructCreate( "char sString[" & $tStruct.iRowCol & "]", $tStruct.pRowCol ) ConsoleWrite( "$tString.sString = " & $tString.sString & @CRLF & @CRLF ) ; Display last structure ConsoleWrite( "Last structure:" & @CRLF ) $tStruct = DllStructCreate( "int iString;ptr pString;int iInteger;double dDouble;int iDate;int iTime;int iRowCol;ptr pRowCol", $pPtr100 + $iSize * ( $iRows - 1 ) ) ConsoleWrite( "$tStruct.iString = " & $tStruct.iString & @CRLF ) ConsoleWrite( "$tStruct.pString = " & Ptr( $tStruct.pString ) & @CRLF ) $tString = DllStructCreate( "char sString[" & $tStruct.iString & "]", $tStruct.pString ) ConsoleWrite( "$tString.sString = " & $tString.sString & @CRLF ) ConsoleWrite( "$tStruct.iInteger = " & $tStruct.iInteger & @CRLF ) ConsoleWrite( "$tStruct.dDouble = " & $tStruct.dDouble & @CRLF ) ConsoleWrite( "$tStruct.iDate = " & $tStruct.iDate & @CRLF ) ConsoleWrite( "$tStruct.iTime = " & $tStruct.iTime & @CRLF ) ConsoleWrite( "$tStruct.iRowCol = " & $tStruct.iRowCol & @CRLF ) ConsoleWrite( "$tStruct.pRowCol = " & Ptr( $tStruct.pRowCol ) & @CRLF ) $tString = DllStructCreate( "char sString[" & $tStruct.iRowCol & "]", $tStruct.pRowCol ) ConsoleWrite( "$tString.sString = " & $tString.sString & @CRLF & @CRLF ) ; Free structure memory $oRand2dArrayClass.FreeStructure() Return EndIf Local $hTimer = TimerInit() Local $pPtr = $oRand2dArrayClass.Rand2dArray( $iRows, $aColumns, $aColTypes, $sSource ) ConsoleWrite( TimerDiff( $hTimer ) & @CRLF ) ; Free structure memory $oRand2dArrayClass.FreeStructure() #forceref $pPtr EndFunc #cs 94.8014985921955 190.196704770522 465.101048033797 #ce Imports System Imports System.Runtime.InteropServices Public Structure Test Public iString As Integer 'String length Public sString As String Public iInteger As Integer Public dDouble As Double Public iDate As Integer Public iTime As Integer Public iRowCol As Integer 'String length Public sRowCol As String End Structure Class Rand2dArrayClass Dim pnt As IntPtr Public Function Rand2dArray( iRows As Integer, aColumns As Object(,), aColTypes As Object(,), sSource As String ) As String Dim iColumns As Integer = aColumns.GetUpperBound(1) + 1 Dim aSource As Char() = sSource.ToCharArray() Dim iSourceLen As Integer = sSource.Length Dim oRnd As New Random() Dim tTest As Test Dim iTotSize As IntPtr = 0 Dim iSize As Integer = Marshal.SizeOf( tTest ) pnt = Marshal.AllocHGlobal( iRows * iSize ) 'Console.WriteLine( "Marshal.SizeOf( tTest ) = {0}", iSize ) Dim sStr As String = "" Dim k, y, m, dMax As Integer For i As Integer = 0 To iRows - 1 'String columns For j As Integer = 1 To aColTypes(0,0) k = aColTypes(j,0) For l As Integer = 1 To oRnd.Next( aColumns(1,k), aColumns(2,k) ) sStr &= aSource(oRnd.Next( iSourceLen )) Next tTest.iString = sStr.Length + 1 tTest.sString = sStr sStr = "" Next 'Integer columns For j As Integer = 1 To aColTypes(0,1) k = aColTypes(j,1) tTest.iInteger = oRnd.Next( aColumns(1,k), aColumns(2,k) ) Next 'Float columns For j As Integer = 1 To aColTypes(0,2) k = aColTypes(j,2) tTest.dDouble = aColumns(1,k) + ( aColumns(2,k) - aColumns(1,k) ) * oRnd.NextDouble() Next 'Date columns For j As Integer = 1 To aColTypes(0,3) k = aColTypes(j,3) y = oRnd.Next( aColumns(1,k), aColumns(2,k) ) m = oRnd.Next( 1, 13 ) Select m Case 1, 3, 5, 7, 8, 10, 12 dMax = 31 Case 4, 6, 9, 11 dMax = 30 Case Else '2 dMax = If( y Mod 4 <> 0, 28, If( y Mod 400 = 0, 29, If( y Mod 100 = 0, 28, 29 ) ) ) End Select tTest.iDate = y * 10000 + m * 100 + oRnd.Next( 1, dMax + 1 ) Next 'Time columns For j As Integer = 1 To aColTypes(0,4) k = aColTypes(j,4) tTest.iTime = oRnd.Next( aColumns(1,k), aColumns(2,k) ) * 10000 + oRnd.Next( 0, 60 ) * 100 + oRnd.Next( 0, 60 ) Next 'Row/col columns For j As Integer = 1 To aColTypes(0,5) k = aColTypes(j,5) tTest.sRowCol = i & "/" & k tTest.iRowCol = tTest.sRowCol.Length + 1 Next 'Copy structure to unmanaged memory Marshal.StructureToPtr( tTest, pnt + iTotSize, False ) iTotSize += iSize Next Return pnt.ToString() End Function Public Sub FreeStructure() Marshal.FreeHGlobal( pnt ) End Sub End Class Performance measurementPerformance measurements for 50,000, 100,000 and 250,000 array rows and structure instances, respectively. Example02: 180.043279980917 387.024483304017 966.513008235719 Example03: 94.8014985921955 190.196704770522 465.101048033797 The passing technique in Example02 takes twice as long as the copying technique in Example03. Unicode supportThe characters to generate random strings ("abcdefghijklmnopqrstuvwxyz" in the examples) are copied directly from the au3 source files. To use Unicode characters, the text encoding of the source files must support Unicode characters. I use Notepad++ as editor and SciTE to run the code so that output can be seen in SciTE console. To use Unicode characters I set the text encoding in Notepad++ to UCS-2 Little Endian. And the char in the AutoIt DllStruct definition must of course be directed to wchar. 7z-fileNew 7z-file at bottom of first post.3 points -
Version 1.6.3.0
17,285 downloads
Extensive library to control and manipulate Microsoft Active Directory. Threads: Development - General Help & Support - Example Scripts - Wiki Previous downloads: 30467 Known Bugs: (last changed: 2020-10-05) None Things to come: (last changed: 2020-07-21) None BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort1 point -
Active Directory UDF (II)
crackdonalds reacted to water for a topic
Example Script: #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 300, 200) ; Create a checkbox control. Local $idCheckbox = GUICtrlCreateCheckbox("Checkbox 1", 10, 10, 185, 25) GUICtrlCreateCheckbox("Checkbox 2", 10, 40, 185, 25) GUICtrlCreateCheckbox("Checkbox 3", 10, 70, 185, 25) GUICtrlCreateCheckbox("Checkbox 4", 10, 100, 185, 25) GUICtrlCreateCheckbox("Checkbox 5", 10, 130, 185, 25) ; Check all Checkboxes For $i = 1 to 5 GUICtrlSetState($idCheckbox + $i - 1, $GUI_CHECKED) Next Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idButton_Close ExitLoop EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example That's a quick and dirty example. With a little bit of effort I could create the Checkbox Controls in a loop as well1 point -
Hi All, This is a start ... #include <string.au3> #include <array.au3> Dim $temp Dim $arrLogonHoursBits[168] Dim $arrDayOfWeek[7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat","Sun"] $objUser = ObjGet ("LDAP://cn=User_name,ou=DEPT,dc=DOMAIN,dc=COM") $arrLogonHours = $objUser.Get("LogonHours") ;~ ConsoleWrite((StringMid($arrLogonHours,3)) & @CRLF) ;LittleEndian(StringMid($arrLogonHours,3)) $arrLogonHoursBytes = StringSplit(StringMid($arrLogonHours,3), "") $intCounter = 0 $intLoopCounter = 0 $arrLogonHourBit = Stringsplit(_HexToBinaryString(StringMid($arrLogonHours,3)), "") ConsoleWrite ("Day Byte 1 Byte 2 Byte 3" & @CRLF) ;For $LogonHourByte In $arrLogonHoursBytes For $x = 1 To UBound($arrLogonHoursBytes)-1 $arrLogonHourBits = Stringsplit(_HexToBinaryString($arrLogonHoursBytes[$x]), "") If $intCounter = 0 Then ConsoleWrite( $arrDayOfWeek[$intLoopCounter] & " " ) $intLoopCounter += 1 EndIf ;For $LogonHourBit In $arrLogonHourBits For $y = 1 to UBound($arrLogonHourBits)-1 ;ConsoleWrite($arrLogonHourBits[$y]) $temp = $temp & $arrLogonHourBits[$y] $intCounter += 1 If $intCounter = 8 or $intCounter = 16 Then ConsoleWrite(_StringReverse($temp)) $temp = "" ConsoleWrite( " " ) EndIf If $intCounter = 24 Then ConsoleWrite(_StringReverse($temp)) $temp = "" ConsoleWrite (@CRLf) $intCounter = 0 EndIf Next Next ; Hex To Binary Func _HexToBinaryString($HexValue) Local $Allowed = '0123456789ABCDEF' Local $Test,$n Local $Result = '' if $hexValue = '' then SetError(-2) Return EndIf $hexvalue = StringSplit($hexvalue,'') for $n = 1 to $hexValue[0] if not StringInStr($Allowed,$hexvalue[$n]) Then SetError(-1) return 0 EndIf Next Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111" $bits = stringsplit($bits,'|') for $n = 1 to $hexvalue[0] $Result &= $bits[Dec($hexvalue[$n])+1] Next Return $Result EndFunc Func _StringReverse($string) $n = StringLen($string) $reversed = "" For $i = 1 To $n Step +1 $reversed = $reversed & StringLeft(StringTrimLeft($string,$n-$i),1) Next Return $reversed EndFunc PS : Don't forget to change the LDAP settings.1 point
-
I've always used CertUtil to add/update/remove certificates.1 point
-
Minimize an active window - (Moved)
SkysLastChance reacted to Danp2 for a topic
@rixYes, I'm sure it's possible. But it would be way less reliable than using WinSetState("[ACTIVE]","",@SW_MINIMIZE)1 point -
For anyone who may need it and in response to the above post, I have attached a modified version of the latest UDF that adds 3 new "helper" functions: Json_ArrayAdd(ByRef $Var, $Notation, $Data) Adds an item (String, number, object, array, or boolean) to an existing array. Json_ArrayInsert(ByRef $Var, $Notation, $Index, $Data) Inserts an array item (String, number, object, array, or boolean) at the specified index. Json_ArrayDelete(ByRef $Var, $Notation, $Range) Deletes a single item, a range of items, or multiple items, from an existing array. The syntax for $Range is the same as _ArrayDelete() since that is what it uses to delete array items. Note: Json_ArrayAdd() and Json_ArrayInsert() only allow adding or inserting a single array item (String, number, object, array, or boolean) at a time. Below are some examples using the functions and the output that's generated Output: Json(with array funcs).au31 point
-
@CYBRIX please check this: ; Example script #include <File.au3> #include <GUIConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPISys.au3> Opt("GUIOnEventMode", 1) ;GUIOnEventMode = Yes Opt("MustDeclareVars", 1) ;Window properties Global Const $nWidth = 500, $nHeight = 200 Global Const $nRefreshRate = 25, $nRefreshRateMs = 1000 / $nRefreshRate Global $STATE = 0 Global Const $ST_END = -1 Global $sSelectedFolder ;Prompt for folder Do $sSelectedFolder = FileSelectFolder("Select a folder with a few files in it", @UserProfileDir & "\Documents") If @error Then Exit MsgBox(0, "Bye now", "Ended program.") Until FileExists($sSelectedFolder) ;Ensure I am not liable If MsgBox(49 + 256, "Warning", "This script clones the folder you selected (in the same directory)." & @CRLF & "It copys and deletes files until you stop it." & @CRLF & @CRLF & "I advise you not to proceed if you selected a folder on an SSD." & @CRLF & "I'M NOT RESPONSABLE FOR REDUCED SSD LIFE TIMES!") <> 1 Then Exit ;List files in folder Global $aFiles = _FileListToArrayRec($sSelectedFolder, "*", 13) If Not IsArray($aFiles) Or $aFiles[0] = 0 Then Exit MsgBox(16, "Bad pick", "It would seem the folder is empty." & @CRLF & "Ended Program.") ;Some variables Global Const $hElapsedTime = TimerInit() Global Const $nBlockSize = 1024 * 1024 Global $nTransfered = 0, $hProcessTimer Global $sLabelData = "Not yet updated", $nLoops Global $aDirCreated[2] = [False, ""] ;GUI Elements Global Const $hWnd = GUICreate("Freeze Example", $nWidth, $nHeight) Global Const $hLabel = GUICtrlCreateLabel($sLabelData, 10, 10, $nWidth - 20, $nHeight - 60) Global Const $hSpeedLabel = GUICtrlCreateLabel("", 10, $nHeight - 60, $nWidth - 20, 50) GUISetOnEvent($GUI_EVENT_CLOSE, _Close) GUISetState(@SW_SHOW, $hWnd) ;Let windows update UI elements ;~ Global Const $pUpdateFunction = DllCallbackRegister(_UpdateUI, "none", "") ;~ Global Const $hRefreshTimer = _WinAPI_SetTimer($hWnd, 0, $nRefreshRateMs, DllCallbackGetPtr($pUpdateFunction)) OnAutoItExitRegister(_EndScript) ;Ensure everything is closed properly ;Main Loop ;~ While $STATE <> $ST_END For $i = 1 To $aFiles[0] $hProcessTimer = TimerInit() _BlockingFunction($aFiles[$i], $sSelectedFolder, $sSelectedFolder & " (Copy)", $i & ' / ' & $aFiles[0]) ;The blocking function Next ;~ WEnd Exit Func _BlockingFunction($sFileName, $sSource, $sDestination, $s_FileProgress) ;Clone the selected folder Local $sData, $nError If Not $aDirCreated[0] And FileExists($sDestination) Then Do $sDestination &= " (Copy)" Until Not FileExists($sDestination) EndIf DirCreate($sDestination) $aDirCreated[0] = True $aDirCreated[1] = $sDestination $sDestination &= "\" & $sFileName If FileExists($sDestination) Then FileDelete($sDestination) _FileCreate($sDestination) Local $hCurrentSource = FileOpen($sSource & "\" & $sFileName, 16) Local $hCurrentDest = FileOpen($sDestination, 1) $sLabelData = ' File: ' & $s_FileProgress & " ... Copying: ...\" & $sFileName Do $sData = FileRead($hCurrentSource, $nBlockSize) $nError = @error FileWrite($hCurrentDest, $sData) $nLoops += 1 If $nError = -1 Then ; if EOF $nTransfered += StringLen($sData) ExitLoop EndIf $nTransfered += $nBlockSize _UpdateUI() Until $STATE = $ST_END Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable. FileClose($hCurrentSource) FileClose($hCurrentDest) Local $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff. ConsoleWrite("! Saved: " & $sFileName & ' ...... in ' & $fDiff & ' mili seconds' & @CRLF) EndFunc ;==>_BlockingFunction Func _Close() $STATE = $ST_END EndFunc ;==>_Close Func _EndScript() If $aDirCreated[0] And MsgBox(36, "Delete created folder?", "Do you want to delete " & $aDirCreated[1] & "?" & @CRLF & "(No, that is not the original. But do double check.)") = 6 Then $sLabelData = "Removing: " & $aDirCreated[1] If DirRemove($aDirCreated[1], 1) = 0 Then MsgBox(16, "Error", "Failed to remove: " & $aDirCreated[1] & @CRLF & "Guess you'll have to do that. sry...") Else MsgBox(0, "Success", "Folder was removed.") EndIf EndIf ;~ _WinAPI_KillTimer($hWnd, $hRefreshTimer) ;~ DllCallbackFree($pUpdateFunction) GUIDelete($hWnd) EndFunc ;==>_EndScript Func _UpdateUI() ;Update the UI elements Local $speed = ($nLoops / $nRefreshRateMs) * 1000 $nLoops = 0 GUICtrlSetData($hLabel, $sLabelData & @CRLF & @CRLF & "Processing: " & _FormatSize(($nTransfered / $nRefreshRateMs) * 1000) & "/s") $nTransfered = 0 GUICtrlSetData($hSpeedLabel, "Hz = " & $speed & @CRLF & "Blocking function loop time: " & _FormatTime(TimerDiff($hProcessTimer)) & @CRLF & @CRLF & "Runtime: " & _FormatTime(TimerDiff($hElapsedTime))) EndFunc ;==>_UpdateUI ; These functions are just for asthetic, and Otherwise irrelevant Func _FormatTime($nTime) Local $sReturn = "", $Temp If $nTime > 60000 Then $Temp = Int($nTime / 60000) $sReturn &= $Temp & "m " $nTime -= $Temp * 60000 EndIf $sReturn &= Int($nTime / 1000) & "s" Return $sReturn EndFunc ;==>_FormatTime Func _FormatSize($nBits) Local $sReturn = "" Local $nBitsInGibibyte = 8589934592, $nBitsInMebibyte = 8388608, $nBitsInKibibyte = 8192, $nBitsInByte = 8 Select Case $nBits > $nBitsInGibibyte $sReturn = Round($nBits / $nBitsInGibibyte, 1) & " GB" Case $nBits > $nBitsInMebibyte $sReturn = Round($nBits / $nBitsInMebibyte, 1) & " MB" Case $nBits > $nBitsInKibibyte $sReturn = Round($nBits / $nBitsInKibibyte) & " KB" Case Else $sReturn = ($nBits / $nBitsInByte & " B") EndSelect Return $sReturn EndFunc ;==>_FormatSize What results to the console gives you this following line: ConsoleWrite("! Saved: " & $sFileName & ' ...... in ' & $fDiff & ' mili seconds' & @CRLF) ??1 point
-
[Question] Why `send('#L') ` does not work?
HoratioCaine reacted to pseakins for a topic
There are a lot of hot key combinations that do not work under Windows 7, probably more under W10. I developed a suite of special functions over a decade ago and was frustrated when it stopped working under W7. I have never tried the Windows key functions but would not be surprised if several of them do not work in AutoIt. HotKeySet("^!{F1}", HotKeySet("^!{F2}", HotKeySet("^!{F3}" and HotKeySet("^!{F4}" do not work.1 point -
Active Directory UDF (II)
SkysLastChance reacted to water for a topic
If you do not know how to code, you should not be allowed to modify the Active Directory of your company using a script. Just my 2 cents worth 😃1 point -
0 just indicates the number of dimensions for example is it a 1d array or 2d array, you could use something like: Switch UBound($aTableData, 0) ;~ Switch between 1d/2d arrays Case 1 ;~ 1d Array For $iRow = 0 To UBound($aTableData) - 1 MsgBox(4096, "Item", "Row : " & $iRow & @CRLF & "Val : " & $aTableData[$iRow]) Next Case 2 ;~ 2d Array For $iRow = 0 To UBound($aTableData) - 1 For $iCol = 0 To UBound($aTableData, 2) - 1 MsgBox(4096, "Item", "Row : " & $iRow & @CRLF & "Col : " & $iCol & @CRLF & "Val : " & $aTableData[$iRow][$iCol]) Next Next EndSwitch1 point