Jump to content



Photo

MRU.au3


  • Please log in to reply
14 replies to this topic

#1 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 25 February 2009 - 01:57 AM

AutoIt         
#Region Header #cs     Title:          Most Recently Used (MRU) List Automation UDF Library for AutoIt3     Filename:       MRU.au3     Description:    A collection of functions for working with lists of MRU                     (see 'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList' for example)     Author:         Yashied     Version:        1.3     Requirements:   AutoIt v3.3 +, Developed/Tested on WindowsXP Pro Service Pack 2     Uses:           Memory.au3     Notes:          -     Available functions:     _MRU_Create     _MRU_Release     _MRU_GetSize     _MRU_SetSize     _MRU_GetItem     _MRU_GetItemCount     _MRU_AddItem     _MRU_AddArray     _MRU_DeleteItem     _MRU_SearchItem     _MRU_Reset     _MRU_GetAsArray     _MRU_GetAsString     _MRU_RegAssign     _MRU_RegRead     _MRU_RegWrite         Example1:         #Include <MRU.au3>         const $RegKey = 'HKLM\SOFTWARE\Test\MRU'         local $i, $Mru, $Array         $Mru = _MRU_Create()         _MRU_AddItem($Mru, 'Item 4')         _MRU_AddItem($Mru, 'Item 3')         _MRU_AddItem($Mru, 'Item 2')         _MRU_AddItem($Mru, 'Item 1')         MsgBox(0, '', _MRU_GetAsString($Mru, '|'))         _MRU_SetSize($Mru, 2)         $Array = _MRU_GetAsArray($Mru)         for $i = 1 to $Array[0]             MsgBox(0, '', $Array[$i])         next         _MRU_RegWrite($Mru, $RegKey)                 _MRU_Release($Mru)     Example2:         #Include <GUIComboBox.au3>         #Include <GUIConstantsEx.au3>         #Include <WindowsConstants.au3>         #Include <MRU.au3>         const $RegKey = 'HKLM\SOFTWARE\Test\MRU'         local $ButtonOk, $Combo, $Msg, $Mru         $Mru = _MRU_Create($RegKey)         GUICreate('Test', 400, 88)         $Combo = GUICtrlCreateCombo('', 20, 20, 360, 21)         GUICtrlSetData(-1, _MRU_GetAsString($Mru, '|'), _MRU_GetItem($Mru, 1))         $ButtonOk = GUICtrlCreateButton('OK', 165, 58, 70, 21)         GUISetState()         GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')         do             $Msg = GUIGetMsg()             if $Msg = $ButtonOk then                 _MRU_AddItem($Mru, GUICtrlRead($Combo))                 _MRU_RegWrite($Mru)             endif         until ($Msg = $ButtonOk) or ($Msg = $GUI_EVENT_CLOSE)         _MRU_Release($Mru)         func WM_COMMAND($hWnd, $msgID, $wParam, $lParam)             switch $lParam                 case GUICtrlGetHandle($Combo)                     if BitShift($wParam, 0x10) = $CBN_EDITCHANGE then                         _GUICtrlComboBox_AutoComplete($Combo)                     endif                 case else             endswitch             return $GUI_RUNDEFMSG         endfunc; WM_COMMAND #ce #Include-once #Include <Memory.au3> #EndRegion Header #Region Local Variables and Constants dim $idList[1][6] = [[0]] #cs DO NOT USE THIS ARRAY IN THE SCRIPT, INTERNAL USE ONLY! $idList[0][0]   - Count item of array        [0][1-5] - Don`t used $idList[i][0]   - The control identifier of the MRU list (1, 2, etc.)        [i][1]   - Size in words (each unicode character requires 2 bytes of memory) of allocated memory block.        [i][2]   - Handle to the allocated memory block (returned by _MemGlobalAlloc())        [i][3]   - Maximum length of the MRU (see _MRU_Create())        [i][4]   - The registry key where is located the MRU list (see _MRU_Create())        [i][5]   - Reserved #ce #EndRegion Local Variables and Constants #Region Public Functions ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_Create ; Description:      Creates an empty MRU list. ; Syntax            _MRU_Create ( [$sRegKey [, $iSize]] ) ; Parameter(s):     $sRegKey - [optional] The registry key where is located the MRU list. If MRU control created is successful, ;                              _MRU_RegRead() function will be called. If $sRegKey is empty, you need to specify the value when calling ;                              _MRU_RegRead() and _MRU_RegWrite() functions. ; ;                   $iSize   - [optional] The size of the MRU list in the number of items. The maximum value - 26 (default). ; Return Value(s):  Success: Returns the identifier (controlID) of the new MRU list. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          MRU list must be created by _MRU_Create() before use this list and calling other functions. When the list ;                   will no longer need to destroy it by _MRU_Release(). _MRU_Create() function created MRU lists in the heap and ;                   do not need to destroy them before the end of the script. ;==================================================================================================================================== func _MRU_Create($sRegKey = '', $iSize = 26)         local $i, $s, $b     local $hMem, $pMem     local $MruID         if ($iSize < 0) or ($iSize > 26) then         return SetError(1, 0, 0)     endif     $hMem = _MemGlobalAlloc(2)     if $hMem = 0 then         return SetError(1, 0, 0)     endif     $pMem = _MemGlobalLock($hMem)     if $pMem = 0 then         _MemGlobalFree($hMem)         return SetError(1, 0, 0)     endif     $MruID = 1     do         $b = 1         for $i = 1 to $idList[0][0]             if $idList[$i][0] = $MruID then                 $MruId += 1                 $b = 0                 exitloop             endif         next     until $b     $idList[0][0] += 1     redim $idList[$idList[0][0] + 1][6]     $idList[$idList[0][0]][0] = $MruID     $idList[$idList[0][0]][1] = 1     $idList[$idList[0][0]][2] = $hMem     $idList[$idList[0][0]][3] = $iSize     $idList[$idList[0][0]][4] = StringStripWS($sRegKey, 3)     $idList[$idList[0][0]][5] = 0     $s = DllStructCreate('ushort[1]', $pMem)     DllStructSetData($s, 1, 0, 1)     _MemGlobalUnlock($hMem)     _MRU_RegRead($MruID, $idList[$idList[0][0]][4])     return SetError(0, 0, $MruID) endfunc; _MRU_Create ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_Release ; Description:      Deletes an MRU control. ; Syntax            _MRU_Release ( $MruID ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_Release($MruID)     local $i, $j, $k     $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif     if not _MemGlobalFree($idList[$i][2]) then         return SetError(1, 0, 0)     endif     for $j = $i to $idList[0][0] - 1         for $k = 0 to 5             $idList[$j][$k] = $idList[$j + 1][$k]         next     next     $idList[0][0] -= 1     redim $idList[$idList[0][0] + 1][6]     return SetError(0, 0, 1) endfunc; _MRU_Release ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_GetSize ; Description:      Returns a size of the MRU list. ; Syntax            _MRU_GetSize ( $MruID ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ; Return Value(s):  Success: Returns the size of the MRU list in the number of items. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_GetSize($MruID)         local $i     $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif     return SetError(0, 0, $idList[$i][3]) endfunc; _MRU_GetSize ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_SetSize ; Description:      Sets a size of the MRU list. ; Syntax            _MRU_SetSize ( $MruID, $iSize ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $iSize - The size of the MRU list in the number of items. The maximum value - 26. ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          If the number of items contained in the MRU list greater than the size, the list will be cut off from the ;                   bottom up to the current size. ;==================================================================================================================================== func _MRU_SetSize($MruID, $iSize)         local $i, $j, $n, $b, $c     local $MruArray         $i = _Index($MruID)     if ($i = 0) or ($iSize < 0) or ($iSize > 26) then         return SetError(1, 0, 0)     endif     $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     $n = StringLen($MruArray[0])     if $iSize < $n then         do             $c = StringRight($MruArray[0], 1)             for $j = Asc($c) - 96 to $n - 1                 $MruArray[$j] = $MruArray[$j + 1]             next             $MruArray[0] = StringTrimRight($MruArray[0], 1)             $n -= 1             for $j = 1 to $n                 $b = StringLeft(StringTrimLeft($MruArray[0], $j - 1), 1)                 if $b > $c then                     $MruArray[0] = StringReplace($MruArray[0], $j, Chr(Asc($b) - 1), 1)                 endif             next         until $n = $iSize         _Array2Mem($MruID, $MruArray)         if @error then             return SetError(1, 0, 0)         endif     endif     $idList[$i][3] = $iSize     return SetError(0, 0, 1) endfunc; _MRU_SetSize ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_GetItem ; Description:      Returns an item from the MRU list. ; Syntax            _MRU_GetItem ( $MruID, $iIndex ) ; Parameter(s):     $MruID  - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $iIndex - Number of the item in the MRU list. ; Return Value(s):  Success: Returns a string containing the desired item from the MRU list. ;                   Failure: Returns "" and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_GetItem($MruID, $iIndex)         local $MruArray     $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, '')     endif     if $iIndex > StringLen($MruArray[0]) then         return SetError(1, 0, '')     endif     return SetError(0, 0, $MruArray[Asc(StringLeft(StringTrimLeft($MruArray[0], $iIndex - 1), 1)) - 96]) endfunc; _MRU_GetItem ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_GetItemCount ; Description:      Returns a number of items in the MRU list. ; Syntax            _MRU_GetItemCount ( $MruID ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ; Return Value(s):  Success: Returns the number of items in the MRU list. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_GetItemCount($MruID)         local $MruArray     $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     return SetError(0, 0, StringLen($MruArray[0])) endfunc; _MRU_GetItemCount ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_AddItem ; Description:      Adds an item to the MRU list. ; Syntax            _MRU_AddItem ( $MruID, $sItem ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $sItem - Item to be added as a string. ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          Add items is always placed in the first position list. If the number of items contained in the MRU list ;                   will be greater than its size, the last element will be removed. If the item already exists in the MRU list, ;                   it will be moved to first position. ;==================================================================================================================================== func _MRU_AddItem($MruID, $sItem)         local $i, $j, $n, $next = true     local $MruArray         $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif     $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     $n = StringLen($MruArray[0])     for $j = 1 to $n         if $MruArray[$j] = $sItem then             $MruArray[0] = Chr(96 + $j) & StringReplace($MruArray[0], Chr(96 + $j), '')             $next = 0             exitloop         endif     next     if $next then         if $n = $idList[$i][3] then             $MruArray[Asc(StringRight($MruArray[0], 1)) - 96] = $sItem             $MruArray[0] = StringRight($MruArray[0], 1) & StringTrimRight($MruArray[0], 1)         else             redim $MruArray[$n + 2]             $MruArray[$n + 1] = $sItem             $MruArray[0] = Chr(97 + $n) & $MruArray[0]         endif     endif     _Array2Mem($MruID, $MruArray)     if @error then         return SetError(1, 0, 0)     endif     return SetError(0, 0, 1) endfunc; _MRU_AddItem ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_AddArray ; Description:      Adds an array containing the items to the MRU list. ; Syntax            _MRU_AddArray ( $MruID, $ArrayIn ) ; Parameter(s):     $MruID   - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $ArrayIn - Add an array containing string. The first element has index 0 in the array. ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          Function works as _MRU_AddItem(), only for the array. Adding to the MRU list starts from the last element of the array. ;                   If successful, the top element of the array will be the first in the MRU list etc. ;==================================================================================================================================== func _MRU_AddArray($MruID, ByRef $ArrayIn)         local $i, $j, $k, $l, $p, $dub, $ArrayOut[1] = [0]     local $MruArray         $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif     $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     for $j = 1 to UBound($ArrayIn)         if $ArrayOut[0] = $idList[$i][3] then             exitloop         endif         $dub = 0         for $l = 1 to $ArrayOut[0]             if $ArrayOut[$l] = $ArrayIn[$j - 1] then                 $dub = 1                 exitloop             endif         next         if not $dub then             $ArrayOut[0] += 1             redim $ArrayOut[$ArrayOut[0] + 1]             $ArrayOut[$ArrayOut[0]] = $ArrayIn[$j - 1]         endif     next     if $ArrayOut[0] < $idList[$i][3] then         $n = StringLen($MruArray[0])         $k = $ArrayOut[0]         for $j = 1 to $n             if $ArrayOut[0] = $idList[$i][3] then                 exitloop             endif             $p = $MruArray[Asc(StringLeft(StringTrimLeft($MruArray[0], $j - 1), 1)) - 96]             $dub = 0             for $l = 1 to $k                 if $ArrayOut[$l] = $p then                     $dub = 1                     exitloop                 endif             next             if not $dub then                 $ArrayOut[0] += 1                 redim $ArrayOut[$ArrayOut[0] + 1]                 $ArrayOut[$ArrayOut[0]] = $p             endif         next     endif     redim $MruArray[$ArrayOut[0] + 1]     $MruArray[0] = ''     for $j = 1 to $ArrayOut[0]         $MruArray[$j] = $ArrayOut[$j]         $MruArray[0] &= Chr(96 + $j)     next     _Array2Mem($MruID, $MruArray)     if @error then         return SetError(1, 0, 0)     endif     return SetError(0, 0, 1) endfunc; _MRU_AddArray ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_DeleteItem ; Description:      Deletes an item from the MRU list. ; Syntax            _MRU_DeleteItem ( $MruID, $iIndex ) ; Parameter(s):     $MruID  - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $iIndex - Number of the item in the MRU list. ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_DeleteItem($MruID, $iIndex)         local $j, $n, $b, $c     local $MruArray         $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     $n = StringLen($MruArray[0])     if $iIndex > $n then         return SetError(1, 0, 0)     endif     $c = StringLeft(StringTrimLeft($MruArray[0], $iIndex - 1), 1)     for $j = Asc($c) - 96 to $n - 1         $MruArray[$j] = $MruArray[$j + 1]     next     $MruArray[0] = StringReplace($MruArray[0], $c, '')     for $j = 1 to $n - 1         $b = StringLeft(StringTrimLeft($MruArray[0], $j - 1), 1)         if $b > $c then             $MruArray[0] = StringReplace($MruArray[0], $j, Chr(Asc($b) - 1), 1)         endif     next     _Array2Mem($MruID, $MruArray)     if @error then         return SetError(1, 0, 0)     endif     return SetError(0, 0, 1) endfunc; _MRU_DeleteItem ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_SearchItem ; Description:      Searches an item in the MRU list. ; Syntax            _MRU_SearchItem ( $MruID, $sItem [, $iFlag] ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $sItem - Item to be found as a string. ;                   $iFlag - [optional] Flag to indicate if the operations should be case sensitive. ;                            0 - not case sensitive, using the user's locale (default) ;                            1 - case sensitive ;                            2 - not case sensitive, using a basic/faster comparison ; ; Return Value(s):  Success: Returns the index of item in the MRU list. (0 - specified element is not found) ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_SearchItem($MruID, $sItem, $iFlag = 0)         local $j, $k, $n     local $MruArray         $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     $n = StringLen($MruArray[0])     for $j = 1 to $n         $k = Asc(StringLeft(StringTrimLeft($MruArray[0], $j - 1), 1)) - 96         if (StringInStr($MruArray[$k], $sItem, $iFlag) = 1) and (StringLen($MruArray[$k]) = StringLen($sItem)) then             return SetError(0, 0, $j)         endif     next     return SetError(0, 0, 0) endfunc; _MRU_SearchItem ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_Reset ; Description:      Removes all items from the MRU list. ; Syntax            _MRU_Clear ( $MruID ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_Reset($MruID)         local $MruArray         $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     $MruArray[0] = ''     _Array2Mem($MruID, $MruArray)     if @error then         return SetError(1, 0, 0)     endif     return SetError(0, 0, 1) endfunc; _MRU_Reset ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_GetAsArray ; Description:      Returns an array of items of the MRU list. ; Syntax            _MRU_GetAsArray ( $MruID ) ; Parameter(s):     $MruID - The control identifier (controlID) as returned by _MRU_Create() function. ; Return Value(s):  Success: Returns an array of items of the MRU list. Element in the array with the index 1 is the first ;                            item in the MRU list etc. Value in the array of index 0 contains the number of items in the MRU list. ;                            If the MRU list is empty, the function returns an empty array ($ArrayOut[0] = 0). ; ;                   Failure: Returns an empty array ($ArrayOut[0] = 0) and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          Array returned by this function is not suitable for the _MRU_AddArray() function (use _ArrayDelete($ArrayOut, 0)). ;==================================================================================================================================== func _MRU_GetAsArray($MruID)         local $j, $ArrayOut[1] = [0]     local $MruArray         $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, $ArrayOut)     endif     $ArrayOut[0] = StringLen($MruArray[0])     redim $ArrayOut[$ArrayOut[0] + 1]     for $j = 1 to $ArrayOut[0]         $ArrayOut[$j] = $MruArray[Asc(StringLeft(StringTrimLeft($MruArray[0], $j - 1), 1)) - 96]     next     return SetError(0, 0, $ArrayOut) endfunc; _MRU_GetAsArray ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_GetAsString ; Description:      Returns a string containing the items from the MRU list. ; Syntax            _MRU_GetAsString ( $MruID, $sSeparator ) ; Parameter(s):     $MruID      - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $sSeparator - Separating characters. ; Return Value(s):  Success: Returns a string containing of a combination of the items from the MRU list and separating characters. ;                            ("Item1" & $sSeparator & "Item2" & $sSeparator & etc) ; ;                   Failure: Returns "" and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          Function can be useful when working with a ComboBox control. ;==================================================================================================================================== func _MRU_GetAsString($MruID, $sSeparator)         local $j, $n, $str = ''     local $MruArray         $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, '')     endif     $n = StringLen($MruArray[0])     for $j = 1 to $n         $str &= $MruArray[Asc(StringLeft(StringTrimLeft($MruArray[0], $j - 1), 1)) - 96]         if $j < $n then             $str &= $sSeparator         endif     next     return SetError(0, 0, $str) endfunc; _MRU_GetAsString ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_RegAssign ; Description:      Assigns an MRU list with the specified registry key. ; Syntax            _MRU_RegRead ( $MruID [, $sRegKey] ) ; Parameter(s):     $MruID   - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $sRegKey - The registry key to assign. (see _MRU_Create()) ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          - ;==================================================================================================================================== func _MRU_RegAssign($MruID, $sRegKey)         local $i     $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif     $idList[$i][4] = StringStripWS($sRegKey, 3)     return SetError(0, 0, 1) endfunc; _MRU_RegAssign ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_RegRead ; Description:      Reads an MRU list from the registry. ; Syntax            _MRU_RegRead ( $MruID [, $sRegKey] ) ; Parameter(s):     $MruID   - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $sRegKey - [optional] The registry key to read. If $sRegKey is empty (default), value will be taken from the ;                              _MRU_Create() or _MRU_RegAssign() function. ; ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          Function does not add items as _MRU_AddArray(). If the MRU list contains elements, they will be overwritten. ;==================================================================================================================================== func _MRU_RegRead($MruID, $sRegKey = '')     local $i, $j, $k, $l, $n = 0, $c, $p, $t, $dub         dim $MruArray[1] = ['']     $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif     if StringStripWS($sRegKey, 3) = '' then         $sRegKey = $idList[$i][4]     endif     $t = StringLower(RegRead($sRegKey, 'MRUList'))     if not @error then         $n = StringLen($t)         if $n > 0 then             $k = 1             for $j = 1 to $n                 if $k > $idList[$i][3] then                     exitloop                 endif                 $c = StringLeft(StringTrimLeft($t, $j - 1), 1)                 if StringIsASCII($c) then                     $p = RegRead($sRegKey, $c)                     if not @error then                         $dub = 0                         for $l = 1 to $k - 1                             if $p = $MruArray[$l] then                                 $dub = 1                                 exitloop                             endif                         next                         if not $dub then                             redim $MruArray[$k + 1]                             $MruArray[$k] = $p                             $MruArray[0] &= Chr(96 + $k)                             $k += 1                         endif                     endif                 endif             next         endif     endif     _Array2Mem($MruID, $MruArray)     if @error then         return SetError(1, 0, 0)     endif     return SetError(0, 0, 1) endfunc; _MRU_RegRead ; #FUNCTION# ======================================================================================================================== ; Function Name:    _MRU_RegWrite ; Description:      Writes an MRU list in the registry. ; Syntax            _MRU_RegWrite ( $MruID [, $sRegKey] ) ; Parameter(s):     $MruID   - The control identifier (controlID) as returned by _MRU_Create() function. ;                   $sRegKey - [optional] The registry key to write to. If $sRegKey is empty, value will be taken from the _MRU_Create() or ;                              _MRU_RegAssign() function. If no other parameters are specified this key will simply be created. ; ; Return Value(s):  Success: Returns 1. ;                   Failure: Returns 0 and sets the @error flag to non-zero. ; Author(s):        Yashied ; Note(s):          Before the function to write MRU list in the registry, the registry key will be deleted! ;==================================================================================================================================== func _MRU_RegWrite($MruID, $sRegKey = '')         local $i, $j, $n     local $MruArray         $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif     if StringStripWS($sRegKey, 3) = '' then         $sRegKey = $idList[$i][4]     endif     $MruArray = _Mem2Array($MruID)     if @error then         return SetError(1, 0, 0)     endif     if RegDelete($sRegKey) = 2 then         return SetError(1, 0, 0)     endif     $n = StringLen($MruArray[0])     for $j = 1 to $n         RegWrite($sRegKey, Chr(96 + $j), 'REG_SZ', $MruArray[$j])     next     RegWrite($sRegKey, 'MRUList', 'REG_SZ', $MruArray[0])     return SetError(0, 0, 1) endfunc; _MRU_RegWrite #EndRegion Public Functions #Region Internal Functions func _Index($MruID)         local $i         for $i = 1 to $idList[0][0]         if $idList[$i][0] = $MruID then             return SetError(0, 0, $i)         endif     next     return SetError(1, 0, 0) endfunc; _Index func _Array2Mem($MruID, ByRef $MruArray)         local $i, $j, $k, $l, $n, $b, $s, $size     local $pMem         $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif         $n = StringLen($MruArray[0])     $size = $n + 1     for $j = 1 to $n         $size += StringLen($MruArray[$j]) + 1     next     if not _MemGlobalFree($idList[$i][2]) then         return SetError(1, 0, 0)     endif     $idList[$i][1] = 0     $idList[$i][2] = _MemGlobalAlloc($size * 2)     if $idList[$i][2] = 0 then         return SetError(1, 0, 0)     endif     $pMem = _MemGlobalLock($idList[$i][2])     if $pMem = 0 then         _MemGlobalFree($idList[$i][2])         $idList[$i][2] = 0         return SetError(1, 0, 0)     endif     $s = DllStructCreate('ushort[' & $size & ']', $pMem)     $b = 1     for $j = 0 to $n         $l = StringLen($MruArray[$j])         for $k = 1 to $l             DllStructSetData($s, 1, AscW(StringLeft(StringTrimLeft($MruArray[$j], $k - 1), 1)), $b)             $b += 1         next         DllStructSetData($s, 1, 0, $b)         $b += 1     next     _MemGlobalUnlock($idList[$i][2])     $idList[$i][1] = $size     return SetError(0, 0, 1) endfunc; _Array2Mem func _Mem2Array($MruID)         local $i, $j, $b, $r, $s, $t     local $pMem     dim $MruArray[1] = ['']         $i = _Index($MruID)     if $i = 0 then         return SetError(1, 0, 0)     endif         $pMem = _MemGlobalLock($idList[$i][2])     if $pMem = 0 then         return SetError(1, 0, 0)     endif     $s = DllStructCreate('ushort[' & $idList[$i][1] & ']', $pMem)     $r = 0     for $j = 1 to $idList[$i][1]         $b = DllStructGetData($s, 1, $j)         if $b = 0 then             $r += 1             if $j < $idList[$i][1] then                 redim $MruArray[$r + 1]                 $MruArray[$r] = ''             endif             continueloop         endif         $MruArray[$r] &= ChrW($b)     next     _MemGlobalUnlock($idList[$i][2])     return SetError(0, 0, $MruArray) endfunc; _Mem2Array #EndRegion Internal Functions
Attached File  MRU.au3   28.28K   613 downloads

Example1
#Include <MRU.au3> const $RegKey = 'HKLM\SOFTWARE\Test\MRU' local $i, $Mru, $Array $Mru = _MRU_Create() _MRU_AddItem($Mru, 'Item 4') _MRU_AddItem($Mru, 'Item 3') _MRU_AddItem($Mru, 'Item 2') _MRU_AddItem($Mru, 'Item 1') MsgBox(0, '', _MRU_GetAsString($Mru, '|')) _MRU_SetSize($Mru, 2) $Array = _MRU_GetAsArray($Mru) for $i = 1 to $Array[0]     MsgBox(0, '', $Array[$i]) next _MRU_RegWrite($Mru, $RegKey)     _MRU_Release($Mru)

Example2
AutoIt         
#Include <GUIComboBox.au3> #Include <GUIConstantsEx.au3> #Include <WindowsConstants.au3> #Include <MRU.au3> const $RegKey = 'HKLM\SOFTWARE\Test\MRU' local $ButtonOk, $Combo, $Msg, $Mru $Mru = _MRU_Create($RegKey) GUICreate('Test', 400, 88) $Combo = GUICtrlCreateCombo('', 20, 20, 360, 21) GUICtrlSetData(-1, _MRU_GetAsString($Mru, '|'), _MRU_GetItem($Mru, 1)) $ButtonOk = GUICtrlCreateButton('OK', 165, 58, 70, 21) GUISetState() GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND') do     $Msg = GUIGetMsg()     if $Msg = $ButtonOk then         _MRU_AddItem($Mru, GUICtrlRead($Combo))         _MRU_RegWrite($Mru)     endif until ($Msg = $ButtonOk) or ($Msg = $GUI_EVENT_CLOSE) _MRU_Release($Mru) func WM_COMMAND($hWnd, $msgID, $wParam, $lParam)     switch $lParam         case GUICtrlGetHandle($Combo)             if BitShift($wParam, 0x10) = $CBN_EDITCHANGE then                 _GUICtrlComboBox_AutoComplete($Combo)             endif         case else     endswitch     return $GUI_RUNDEFMSG endfunc; WM_COMMAND

Edited by Yashied, 06 April 2009 - 11:26 AM.






#2 Dolemite50

Dolemite50

    Prodigy

  • Active Members
  • PipPipPip
  • 157 posts

Posted 02 April 2009 - 02:53 AM

Hi Yashied,

I haven't had a chance to try this out yet but wanted to say thanks for all of the cool examples you've been kicking out.

I'll let you know how it goes.

Hat's off, sir!

#3 JRSmile

JRSmile

    IT infrastructure specialist

  • Active Members
  • PipPipPipPipPipPip
  • 453 posts

Posted 02 April 2009 - 11:59 AM

is it possible with this to grap the MRU of the Win+R command?
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));)''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)Try2Hack: http://www.try2hack.nl/levels/level13-olwehfdow.xhtml

#4 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 02 April 2009 - 12:23 PM

Hi Yashied,

I haven't had a chance to try this out yet but wanted to say thanks for all of the cool examples you've been kicking out.

I'll let you know how it goes.

Hat's off, sir!

Thanks.

is it possible with this to grap the MRU of the Win+R command?

No. This UDF will not work with an MRU of Run Dialog. At least without further modification. You can watch himself on the MRU structure of a Run Dialog.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU

#5 JRSmile

JRSmile

    IT infrastructure specialist

  • Active Members
  • PipPipPipPipPipPip
  • 453 posts

Posted 02 April 2009 - 12:27 PM

Thanks.


No. This UDF will not work with an MRU of Run Dialog. At least without further modification. You can watch himself on the MRU structure of a Run Dialog.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU


yea, tried it out myself, unfortunately you are right, (cry for standardisation), thanks anyway.
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));)''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)Try2Hack: http://www.try2hack.nl/levels/level13-olwehfdow.xhtml

#6 JRSmile

JRSmile

    IT infrastructure specialist

  • Active Members
  • PipPipPipPipPipPip
  • 453 posts

Posted 02 April 2009 - 01:26 PM

modified two lines and thats it:-)
AutoIt         
#Include <GUIComboBox.au3> #Include <GUIConstantsEx.au3> #Include <WindowsConstants.au3> #include <process.au3> #Include <MRU.au3> ; modified two lines in _MRUGetAsString ; func _MRUGetAsString($MruID, $sSeparator, $remove = "") ; $str &= stringreplace($MruArray[Asc(StringLeft(StringTrimLeft($MruArray[0], $j - 1), 1)) - 96],$remove,"") #include <misc.au3> ;~ const $RegKey = 'HKLM\SOFTWARE\Test\MRU' const $RegKey = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' local $ButtonOk, $Combo, $Msg, $Mru $Mru = _MRUCreate($RegKey) GUICreate('Test', 400, 88) $Combo = GUICtrlCreateCombo('', 20, 20, 360, 80) GUICtrlSetData(-1, _MRUGetAsString($Mru, '|',"\1"), _MruGetItem($Mru, 1)) $ButtonOk = GUICtrlCreateButton('OK', 165, 58, 70, 21) GUISetState() GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND') $done = False do     $Msg = GUIGetMsg()     if $Msg = $ButtonOk or _IsPressed("0D") then         _MRUAddItem($Mru, GUICtrlRead($Combo)& "\1")         _MRURegWrite($Mru)         _rundos(GUICtrlRead($Combo))         $done = True     endif until ($done) or ($Msg = $GUI_EVENT_CLOSE) _MRURelease($Mru) func WM_COMMAND($hWnd, $msgID, $wParam, $lParam)     switch $lParam         case GUICtrlGetHandle($Combo)             if BitShift($wParam, 0x10) = $CBN_EDITCHANGE then                 _GUICtrlComboBox_AutoComplete($Combo)             endif         case else             ;;;     endswitch     return $GUI_RUNDEFMSG endfunc; WM_COMMAND

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));)''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)Try2Hack: http://www.try2hack.nl/levels/level13-olwehfdow.xhtml

#7 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 02 April 2009 - 02:04 PM

modified two lines and thats it:-)

Very good, but this will be true only for the Run Dialog. There are several types of MRU, but the most common and easiest to use is the one that I realized in my UDF. Here are a few species.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs

:D

#8 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 02 April 2009 - 02:14 PM

Remark.

If you want to write a program to replace the Run Dialog, there is one problem. Windows does not immediately write to the registry information on the running program. I used for this purpose its own MRU.

#9 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 06 April 2009 - 11:27 AM

This library has been updated.

:D

#10 qsek

qsek

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 256 posts

Posted 13 February 2010 - 06:47 PM

Really cool UDF, very easy to use.
I think i can use this on some of my scripts, thank you :mellow:
Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

#11 lsakizada

lsakizada

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 392 posts

Posted 04 March 2011 - 12:21 PM

Yashied, the example #1 is crashing the application. The second one does not work for me either after specifying data in the input box.

The error was come after calling to the first _MRU_AddItem function:

C:\All\B_QA\QA\MyTools\Astetaine\examples\MRU Yashied\MRU.au3 (379) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: if $MruArray[$j] = $sItem then if ^ ERROR


I also noticed that the registry is not created "$RegKey = 'HKLM\SOFTWARE\Test\MRU'"
Does the UDF should create the registry key above?
Be Green Now or Never (BGNN)!

#12 KaFu

KaFu

    Hey, it's just me, KhaFoo...

  • MVPs
  • 3,161 posts

Posted 04 March 2011 - 12:32 PM

I guess the problem is that with UAC activated under Vista+ you need to run the script with elevated Admin rights to access the HKLM registry hive. Maybe renaming any occurrence of HKLM to HKU or HKCU will do the trick.

#13 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 04 March 2011 - 12:41 PM

I no longer support this UDF. Use MRU* functions from the WinAPIEx UDF library.

#14 lsakizada

lsakizada

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 392 posts

Posted 04 March 2011 - 01:39 PM

I guess the problem is that with UAC activated under Vista+ you need to run the script with elevated Admin rights to access the HKLM registry hive. Maybe renaming any occurrence of HKLM to HKU or HKCU will do the trick.



Thanks Kafu for the advice, I am running on XP.
But as Yashied says latter on, this udf is not supported anymore.

Thanks Yashied, I was not aware to the MRU functions at the WinAPIEX UDF, I will dig into them.
Be Green Now or Never (BGNN)!

#15 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 04 March 2011 - 01:59 PM

I was not aware to the MRU functions at the WinAPIEX UDF, I will dig into them.

There is a simple example.

:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users