RegDeleteKeysRecursive

Share your advanced PureBasic knowledge/code with the community.
Fred
Administrator
Administrator
Posts: 18234
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

RegDeleteKeysRecursive

Post by Fred »

Code updated For 5.20+

Deletes a registry key recursive, with all its childs..

Code: Select all

;
; Recursive registry delete routine, to be compatible with all windows versions
;
Procedure RegDeleteKeysRecursive(StartKey, pKeyName$)

  Result = ~#ERROR_SUCCESS
  
  If pKeyName$
  
    Result = RegOpenKeyEx_(StartKey, pKeyName$, 0, #KEY_ENUMERATE_SUB_KEYS | #DELETE, @Key )
    If Result = #ERROR_SUCCESS
    
      SubKeyName$ = Space(512)
    
      While Result = #ERROR_SUCCESS And Quit = 0
        SubKeyLength = 512
        Result = RegEnumKeyEx_(Key, SubKeyIndex, SubKeyName$, @SubKeyLength, 0, 0, 0, 0)
        SubKeyIndex+1
        
        If Result = #ERROR_NO_MORE_ITEMS
          Result = RegDeleteKey_(StartKey, pKeyName$)
          Quit = 1
          
        ElseIf Result = #ERROR_SUCCESS
                 
          SubKeyIndex-1
          If RegDeleteKeysRecursive(Key, SubKeyName$)
            Result = #ERROR_SUCCESS;
          EndIf
        EndIf
      Wend
      
      RegCloseKey_(Key)
    EndIf
  EndIf
    
  If Result = #ERROR_SUCCESS
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf 

EndProcedure


; Test here... WARNING, it could be dangerous !
;
RegDeleteKeysRecursive(#HKEY_CURRENT_USER, "Software\YourFavoriteSoftwareHere")

GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Short copy out of my IncludePack1:

Code: Select all

Procedure Reg_DeleteKeyWithAllSub(topKey,sKeyName.s,ComputerName.s)
  i=0
  a$=""
  Repeat
    b$=a$
    a$=Reg_ListSubKey(topKey,sKeyName,0,"")
    If a$<>""
      Reg_DeleteKeyWithAllSub(topKey,sKeyName+"\"+a$,"")
    EndIf
  Until a$=b$
  Reg_DeleteKey(topKey, sKeyName, ComputerName) 
EndProcedure
But this function need the rest of the registry.pbi

GPI
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

:) simplest

Code: Select all

If OpenLibrary(0, "SHLWAPI.DLL")

  *F = IsFunction(0, "SHDeleteKeyA")
  If *F
    CallFunctionFast(*F,#HKEY_CURRENT_USER, "Software\test")
  EndIf
  
  CloseLibrary(0)
EndIf
why don't support "API in SHLWAPI.DLL"?
EDIT: dlls usually used is better :oops: (as Fred did)
Fred
Administrator
Administrator
Posts: 18234
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

sec wrote: why don't support "API in SHLWAPI.DLL"?
EDIT: dlls usually used is better :oops: (as Fred did)
Because of this:

Minimum DLL Version shlwapi.dll version 4.71 or later
Custom Implementation No
Header shlwapi.h
Import library shlwapi.lib
Minimum operating systems Windows 2000, Windows NT 4.0 with Internet Explorer 4.0, Windows 98, Windows 95 with Internet Explorer 4.0

:)
Post Reply