Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Application specific Places Bar

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Application specific Places Bar

    The following is a method to generate a Places Bar configuration on an application specific basis. The method was got from Cutting Edge.

    The code below is quick and dirty and simply demonstrates the method.

    It relies on the registry function RegOverridePredefKey which was introduced in Win2000 so will not not work on an NT kernel prior and, of course, the 9x/Me platform. The code does not check the OS version.

    The principle is to map a pre-defined key to another key.

    In the following post I have listed the Dword values available for the system folders.

    On executing, an open file dialog is displayed. Do not Cancel yet. Run Notepad, for example, and display an open file dialog. This second open file dialog will use the global Places Bar.

    The method employed may, of course, be used on other aspects which have a global nature.

    That is it. Have fun.

    Code:
    #Compile  Exe '8.04
    
    #REGISTER NONE
    #DIM      ALL
    #TOOLS    OFF
    
    #Include "WIN32API.inc" ' 20 April 2007
    #Include "COMDLG32.inc" ' 1 November 2006
    
    ' The following three functions are by Pierre Bellisle
    Declare Function RegKeySet(Dword, String) As Long
    Declare Function RegDataSet(Dword, String, String, String, ByVal Dword) As Long
    Declare Function RegKeyDelTreeRecursive(Dword, String) As Long
    
    
    Function PBMain( ) As Long 
    
    Local zMyHKCU As Asciiz * 256
    Local hMyHKCU As Dword ' MyHKCU handle
    
      ' Create the key that we are going to map to
      
      zMyHKCU = "MyHKCU"
      If RegCreateKeyEx( %HKEY_CURRENT_USER, zMyHKCU, ByVal %Null, ByVal %Null, %REG_OPTION_NON_VOLATILE, %KEY_ALL_ACCESS, ByVal %Null, hMyHKCU, ByVal %Null ) <> %Error_Success Then
        MsgBox "Failed to create MyHKCU"
        Exit Function
      End If
      
      ' Create redirection - we are going to map HKEY_CURRENT_USER to MyHKCU
      ' MyHKCU must remain open
      
      If RegOverridePredefKey( %HKEY_CURRENT_USER, hMyHKCU ) <> %Error_Success Then
        MsgBox "Failed to create mapping key"
        Exit Function
      End If
      
      ' Note: MyHKCU is empty at this stage.
      ' Any writes to HKEY_CURRENT_USER from now on will be mapped To MyHKCU - HKEY_CURRENT_USER will remain intact
      ' Any reads from HKEY_CURRENT_USER other than mapped entries will fail - they do not exist in mapped domain
          
      'It is now safe to close MyHKCU
      
      RegCloseKey hMyHKCU
    
      
      [COLOR="Blue"]' Do not hide extensions for known file types
    
      If RegDataSet(%HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced","HideFileExt","&h00", %REG_DWORD) <> %Error_Success Then
        MsgBox "Failed to write data"
        Exit Function
      End If[/COLOR]
      
      ' Populate the PlacesBar key
        
      ' Example using Dword
      
      If RegDataSet(%HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies\comdlg32\PlacesBar", "Place0", "&h11", %REG_DWORD) <> %Error_Success Then
        MsgBox "Failed to write data"
        Exit Function
      End If
      
      'Example using path
      
      If RegDataSet(%HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies\comdlg32\PlacesBar", "Place1", "C:\", %REG_SZ) <> %Error_Success Then
        MsgBox "Failed to write data"
        Exit Function
      End If
      
      'Example using environment variable
      
      If RegDataSet(%HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies\comdlg32\PlacesBar", "Place2", "%ProgramFiles%", %REG_EXPAND_SZ) <> %Error_Success Then
        MsgBox "Failed to write data"
        Exit Function
      End If
      
      ' Now cancel redirection - HKEY_CURRENT_USER is now available as normal
      
      RegOverridePredefKey( %HKEY_CURRENT_USER, ByVal %Null )
      
        
      ' OK, now lets use our application specific Places Bar
      
      ' -----------------------------------------------------------------------------------------------------
          
      ' Open MyHKCU for mapping
      
      If RegOpenKeyEx( %HKEY_CURRENT_USER, zMyHKCU, ByVal %Null, %KEY_READ, hMyHKCU ) <> %Error_Success Then
        MsgBox "Failed to open MyHKCU"
      End If
       
      ' Create redirection
        
      If RegOverridePredefKey( %HKEY_CURRENT_USER, hMyHKCU ) <> %Error_Success Then
        MsgBox "Failed to create mapping key"
        Exit Function
      End If
      
      RegCloseKey hMyHKCU
      
      ' Now test
        
      OPENFILEDIALOG( 0, "Select a file", "", "", "All Files (*.*)|*.*", "*", %OFN_FILEMUSTEXIST Or %OFN_FORCESHOWHIDDEN Or %OFN_HIDEREADONLY )
         
      ' Cancel redirection
      
      RegOverridePredefKey( %HKEY_CURRENT_USER, ByVal %Null )
      
      ' -----------------------------------------------------------------------------------------------------
      
      ' and finally delete the mapped key
      
      If RegKeyDelTreeRecursive( %HKEY_CURRENT_USER, "MyHKCU" ) <> %Error_Success Then MsgBox "MyHKCU deletion failed"
      
      ' -----------------------------------------------------------------------------------------------------
      
      
    End Function
    
    ' **********
    
    Function RegKeySet(hMainKey As Dword, sKeyName As String) As Long
     'Create one or many level of key. Return %ERROR_SUCCESS (0) if no error.
     'Retval = RegKeySet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test")
     Local hKey   As Dword
     Local dwDisp As Dword
     
     Function = RegCreateKeyEx(hMainKey, ByVal StrPtr(SKeyName), ByVal %Null, "", %REG_OPTION_NON_VOLATILE, _
                               %KEY_ALL_ACCESS, ByVal %Null, hKey, dwDisp)
     If hKey Then RegCloseKey(hKey)
     
    End Function
    
    Function RegDataSet(hMainKey As Dword, sKeyName As String, sValueName As String, _
                        sData As String, ByVal dwDataType As Dword) As Long
     'Create value if necessary and set data. Return %ERROR_SUCCESS (0) if no error.
     'RegDataSet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test", "", "DefaultData", %REG_SZ)
     'RegDataSet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test", "Value REG_SZ", "SomeData", %REG_SZ)
     'RegDataSet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test", "Value REG_MULTI_SZ", "SomeData" & $NUL & "MoreData" & $NUL & "NoMoreData", %REG_MULTI_SZ)
     'RegDataSet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test", "Value REG_EXPAND_SZ", "%ProgramFiles%\SomeFolder", %REG_EXPAND_SZ)
     'RegDataSet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test", "Value REG_DWORD", "1234567890", %REG_DWORD)
     Local hKey    As Dword
     Local DalaLen As Dword
     Local dwData  As Dword
     Local Retval  As Long
     Local pData   As Dword
     
     Retval = RegOpenKeyEx(hMainKey, ByVal StrPtr(sKeyName), ByVal %Null, %KEY_ALL_ACCESS, hKey)
     If Retval = %ERROR_FILE_NOT_FOUND Then
       RegKeySet(hMainKey, sKeyName)
       Retval = RegOpenKeyEx(hMainKey, ByVal StrPtr(sKeyName), ByVal %Null, %KEY_ALL_ACCESS, hKey)
     End If
     
     If Retval = %ERROR_SUCCESS Then
       If dwDataType = %REG_DWORD Then
         dwData = Val(sData)
         pData = VarPtr(dwData)
         DalaLen = 4
       Else
         pData = StrPtr(sData)
         DalaLen = Len(sData) 'Use LEN not SIZEOFF
       End If
       Retval = RegSetValueEx(hKey, ByVal StrPtr(sValueName), ByVal %Null, dwDataType, ByVal pData, ByVal DalaLen)
       RegCloseKey(hKey)
     End If
     Function = Retval
     
    End Function
    
    Function RegKeyDelTreeRecursive(hMainKey As Dword, sKeyToDel As String) As Long
     'Delete a key and and everything under recursively. Return %ERROR_SUCCESS (0) if no error.
     'RegKeyDelTreeRecursive(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test\KeyToDel")
     Local  zBuffer    As Asciiz * 16384
     Local  zBufferLen As Dword
     Local  hKey       As Dword
     Static dwDeep     As Dword
     Static ErrorCount As Long
     Local  RetVal     As Long 
     
     If dwDeep = 0 Then ErrorCount = 0 'Reset ErrorCount
     Incr dwDeep 'Keep track of auto recursion deepness
     If RegOpenKeyEx(hMainKey, ByVal StrPtr(sKeyToDel), 0, %KEY_ALL_ACCESS, hKey) = %ERROR_SUCCESS Then
     
       Do 'Enumerate all values and delete each one
         zBufferLen = SizeOf(zBuffer)
         Retval = RegEnumValue(hKey, ByVal 0, zBuffer, zBufferLen, ByVal %Null, ByVal %Null, ByVal %Null, ByVal %Null)
         If Retval <> %ERROR_SUCCESS Then 
           If Retval <> %ERROR_NO_MORE_ITEMS Then Incr ErrorCount ': MsgBox "%ERROR_NO_MORE_ITEMS"
           Exit Do 'No value found, so exit
         End If  
         Retval = RegDeleteValue(hKey, ByVal VarPtr(zBuffer)) 'Delete the value
         If Retval Then Incr ErrorCount     
       Loop
     
       Do 'Enumerate all key and delete each one
         zBufferLen = SizeOf(zBuffer)
         Retval = RegEnumKeyEx(hKey, ByVal 0, zBuffer, zBufferLen, 0, ByVal %Null, ByVal %Null, ByVal %Null)
         If Retval <> %ERROR_SUCCESS Then 
           If Retval <> %ERROR_NO_MORE_ITEMS Then Incr ErrorCount ': MsgBox "%ERROR_NO_MORE_ITEMS"
           Exit Do 'No key found, so exit
         End If  
         RegKeyDelTreeRecursive hMainKey, sKeyToDel & "\" & zBuffer 'The function call itself to go deeper doing a recursive action
         zBuffer = sKeyToDel & "\" & zBuffer
         Retval = RegDeleteKey(hMainKey, ByVal VarPtr(zBuffer)) 'Delete founded keys 
         If Retval Then Incr ErrorCount
       Loop
       RegCloseKey hKey
     Else
       Incr ErrorCount
     End If
     
     Decr dwDeep 'Recursion is all done when dwDeep is back to zero
     If dwDeep = 0 Then
       'Job is almost done, still have to delete the sKeyToDel
       'that was set before any recursion occured
       Retval = RegDeleteKey(hMainKey, ByVal StrPtr(sKeyToDel))
       If Retval Then Incr ErrorCount
     End If
     
     Function = ErrorCount ': MsgBox Str$(ErrorCount)
     
    End Function
    
    ' **********
    Last edited by David Roberts; 23 Jan 2008, 04:06 AM. Reason: Added blue section

  • #2
    00 - Desktop
    01 - Internet Explorer
    02 - Start Menu\Programs
    03 - My Computer\Control Panel
    04 - My Computer\Printers
    05 - My Documents
    06 - Favorites
    07 - Start Menu\Programs\Startup
    08 - \Recent
    09 - \SendTo
    0a - \Recycle Bin
    0b - \Start Menu
    0c - - logical "My Documents" desktop icon
    0d - My Music
    0e - My Videos
    10 - \Desktop
    11 - My Computer
    12 - My Network Places
    13 - \NetHood
    14 - WINDOWS\Fonts
    15 - Templates
    16 - All Users\Start Menu
    17 - All Users\Programs
    18 - All Users\Start Menu
    19 - All Users\Desktop
    1a - \Application Data
    1b - \PrintHood
    1c - \Local Settings\Application Data 1d - - Nonlocalized startup
    1e - - Nonlocalized common startup
    1f - Favorites
    20 - Temporary Internet Files
    21 - Cookies
    22 - History
    23 - All Users\Application Data
    24 - WINDOWS directory
    25 - System32 directory
    26 - Program files
    27 - My Pictures
    28 - USERPROFILE
    29 - - x86 system directory on RISC
    2a - - x86 C:\Program Files on RISC
    2b - C:\Program Files\Common
    2c - - x86 Program Files\Common on RISC
    2d - All Users\Templates
    2e - All Users\Documents
    2f - All Users\Start Menu\Programs\Administrative Tools
    30 - - \Start Menu\Programs\Administrative Tools
    31 - Network and Dial-up Connections
    35 - All Users\My Music
    36 - All Users\My Pictures
    37 - All Users\My Video
    38 - Resource Directory
    39 - Localized Resource Directory
    3a - Links to All Users OEM specific apps
    3b - USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning

    Comment


    • #3
      Oops.

      I noticed that most file extensions were missing. The ones listed seemed to be my own and a few others. The old grey matter came to the rescue. I remembered years ago on Win98 the facility to hide them in Explorer via Folder Options. Well it seems that the API GetOpenFilename honours this so we need to do a little mapping of the appropriate registry key since MyHKCU did not have it. It follows then that without this entry extensions for known file types are hidden. The Cutting Edge article missed this issue. The blue section in the first post includes the additional code required.

      Sorry folks, but all is well now.

      Err, you do want extensions? Actually, the correct way would be to copy the existing key and map that. I'll leave that to you if you want that.

      Comment

      Working...
      X