REGISTRY read write delete

Just starting out? Need help? Post your questions and find answers here.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

REGISTRY read write delete

Post by NoahPhense »

If this is how to read and write:

Code: Select all

;read a key from the registry 
Procedure.s ReadRegKey(OpenKey.l, SubKey.s, ValueName.s) 
    hKey.l = 0 
    KeyValue.s = Space(255) 
    Datasize.l = 255 
    If RegOpenKeyEx_(OpenKey, SubKey, 0, #KEY_READ, @hKey) 
        KeyValue = "Error Opening Key" 
    Else 
        If RegQueryValueEx_(hKey, ValueName, 0, 0, @KeyValue, @Datasize) 
            KeyValue = "Error Reading Key" 
        Else  
            KeyValue = Left(KeyValue, Datasize - 1) 
        EndIf 
        RegCloseKey_(hKey) 
    EndIf 
    ProcedureReturn KeyValue 
EndProcedure 

;write a key to the registry 
Procedure.l WriteRegKey(OpenKey.l, SubKey.s, KeySet.s, KeyValue.s) 
    hKey.l = 0  
    If RegCreateKey_(OpenKey, SubKey, @hKey) = 0 
        Result = 1 
        Datasize.l = Len(KeyValue) 
        If RegSetValueEx_(hKey, KeySet, 0, #REG_SZ, @KeyValue, Datasize) = 0 
            Result = 2 
        EndIf 
        RegCloseKey_(hKey) 
    EndIf 
    ProcedureReturn Result 
EndProcedure


WriteRegKey(#HKEY_CURRENT_USER, "Software\PureBasic Screensaver", "Resolution", "800x600") 
RegistryString.s = ReadRegKey(#HKEY_CURRENT_USER, "Software\PureBasic Screensaver", "Resolution") 
Debug RegistryString
Then how do I get the delete going?? For both SubKey and full key?

Code: Select all

; LONG RegDeleteKey(
; 
;     HKEY hKey, // handle of open key 
;     LPCTSTR lpSubKey  // address of name of subkey To delete 
;    );
- np
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: REGISTRY read write delete

Post by NoahPhense »

anyone?
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

You can't delete a key that has subkeys, so you must recurse down and delete all subkeys one by one.. To be able to do this you need to use the 'RegEnumKey_()' or 'RegEnumKeyEx_()' (preferable the later i think) functions.
Last edited by Pupil on Wed Feb 23, 2005 1:30 pm, edited 1 time in total.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Pupil wrote:You can't delete a key that has subkeys, so you must recurse down and delete all subkeys one by one..
Understood, but how do I write the code for my procedures above to do that?

That's my real question. ;)

- np
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Post by sverson »

I found this in the ..\include folder of my jaPBe.
Don't know whom it is from...

Code: Select all

Procedure Reg_DeleteKey(topKey, sKeyName.s, ComputerName.s) 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegDeleteKey_(topKey, @sKeyName) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegDeleteKey_(lhRemoteRegistry, @sKeyName) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    DeleteKey = #True 
  Else 
    DeleteKey = #False 
  EndIf 
  ProcedureReturn DeleteKey 
EndProcedure 

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
It's a part of Registry.pbi

Code: Select all

;info: Read and change the Registry / FileAssociate
Procedure Reg_SetValue(topKey, sKeyName.s, sValueName.s, vValue.s, lType, ComputerName.s) 
  lpData.s 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    lpcbData = 255 
    lpData = Space(255) 
    
    Select lType 
      Case #REG_SZ 
        GetHandle = RegSetValueEx_(hKey, sValueName, 0, #REG_SZ, @vValue, Len(vValue) + 1) 
      Case #REG_DWORD 
        lValue = Val(vValue) 
        GetHandle = RegSetValueEx_(hKey, sValueName, 0, #REG_DWORD, @lValue, 4) 
    EndSelect 
    
    RegCloseKey_(hKey) 
    ergebnis = 1 
    ProcedureReturn ergebnis 
  Else 
    MessageRequester("Fehler", "Ein Fehler ist aufgetreten", 0) 
    RegCloseKey_(hKey) 
    ergebnis = 0 
    ProcedureReturn ergebnis 
  EndIf 
EndProcedure 
Procedure.s Reg_GetValue(topKey, sKeyName.s, sValueName.s, ComputerName.s) 
  lpData.s 
  GetValue.s =""
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    lpcbData = 255 
    lpData = Space(255) 
    
    GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData) 
    
    If GetHandle = #ERROR_SUCCESS 
      Select lType 
        Case #REG_SZ 
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData) 
          
          If GetHandle = 0 
            GetValue = Left(lpData, lpcbData - 1) 
          Else 
            GetValue = "" 
          EndIf 
          
        Case #REG_DWORD 
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lpType, @lpDataDWORD, @lpcbData) 
          
          If GetHandle = 0 
            GetValue = Str(lpDataDWORD) 
          Else 
            GetValue = "0" 
          EndIf 
          
      EndSelect 
    EndIf 
  EndIf 
  RegCloseKey_(hKey) 
  ProcedureReturn GetValue 
EndProcedure 
Procedure.s Reg_ListSubKey(topKey, sKeyName.s, Index, ComputerName.s) 
  lpName.s 
  lpftLastWriteTime.FILETIME 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    lpcbName = 255 
    lpName = Space(255) 
    
    GetHandle = RegEnumKeyEx_(hKey, Index, @lpName, @lpcbName, 0, 0, 0, @lpftLastWriteTime) 
    
    If GetHandle = #ERROR_SUCCESS 
      ListSubKey.s = Left(lpName, lpcbName) 
    Else 
      ListSubKey.s = "" 
    EndIf 
  EndIf 
  RegCloseKey_(hKey) 
  ProcedureReturn ListSubKey 
EndProcedure 
Procedure Reg_DeleteValue(topKey, sKeyName.s, sValueName.s, ComputerName.s) 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    GetHandle = RegDeleteValue_(hKey, @sValueName) 
    If GetHandle = #ERROR_SUCCESS 
      DeleteValue = #True 
    Else 
      DeleteValue = #False 
    EndIf 
  EndIf 
  RegCloseKey_(hKey) 
  ProcedureReturn DeleteValue 
EndProcedure 
Procedure Reg_CreateKey(topKey, sKeyName.s, ComputerName.s) 
  lpSecurityAttributes.SECURITY_ATTRIBUTES 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegCreateKeyEx_(topKey, sKeyName, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @lpSecurityAttributes, @hNewKey, @GetHandle) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegCreateKeyEx_(lhRemoteRegistry, sKeyName, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @lpSecurityAttributes, @hNewKey, @GetHandle) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    GetHandle = RegCloseKey_(hNewKey) 
    CreateKey = #True 
  Else 
    CreateKey = #False 
  EndIf 
  ProcedureReturn CreateKey 
EndProcedure 
Procedure Reg_DeleteKey(topKey, sKeyName.s, ComputerName.s) 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegDeleteKey_(topKey, @sKeyName) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegDeleteKey_(lhRemoteRegistry, @sKeyName) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    DeleteKey = #True 
  Else 
    DeleteKey = #False 
  EndIf 
  ProcedureReturn DeleteKey 
EndProcedure 
Procedure.s Reg_ListSubValue(topKey, sKeyName.s, Index, ComputerName.s) 
  lpName.s 
  lpftLastWriteTime.FILETIME 
  ListSubValue.s 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  EndIf 
  
  
  If GetHandle = #ERROR_SUCCESS 
    lpcbName = 255 
    lpName = Space(255) 
    
    GetHandle = RegEnumValue_(hKey, Index, @lpName, @lpcbName, 0, 0, 0, 0) 
    
    If GetHandle = #ERROR_SUCCESS 
      ListSubValue = Left(lpName, lpcbName) 
    Else 
      ListSubValue = "" 
    EndIf 
    RegCloseKey_(hKey) 
  EndIf 
  ProcedureReturn ListSubValue 
EndProcedure 
Procedure Reg_KeyExists(topKey, sKeyName.s, ComputerName.s) 
  
  If Left(sKeyName, 1) = "\" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    KeyExists = #True 
  Else 
    KeyExists = #False 
  EndIf 
  ProcedureReturn KeyExists 
EndProcedure 
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
Procedure Reg_CreateKeyValue(topKey,sKeyName.s,sValueName.s,vValue.s,lType,ComputerName.s)
  Reg_CreateKey(topKey,sKeyName,ComputerName)
  ProcedureReturn Reg_SetValue(topKey,sKeyName,sValueName,vValue,lType,ComputerName)
EndProcedure

Procedure AssociateFileEx(ext$,ext_description$,programm$,icon$,prgkey$,cmd_description$,cmd_key$) 
  cmd$=Chr(34)+programm$+Chr(34)+" "+Chr(34)+"%1"+Chr(34) 
  If GetVersion_() & $FF0000 ; Windows NT/XP 
    Reg_CreateKeyValue(#HKEY_CLASSES_ROOT, "Applications\"+prgkey$+"\shell\"+cmd_description$+"\command","",cmd$,#REG_SZ,"") 
    If ext_description$ 
      Key$=ext$+"_auto_file" 
      Reg_CreateKeyValue(#HKEY_CLASSES_ROOT  ,"."+ext$           ,"",Key$            ,#REG_SZ,"") 
      Reg_CreateKeyValue(#HKEY_CLASSES_ROOT  ,Key$               ,"",ext_description$,#REG_SZ,"") 
      If icon$ 
        Reg_CreateKeyValue(#HKEY_CLASSES_ROOT,Key$+"\DefaultIcon","",icon$           ,#REG_SZ,"") 
      EndIf 
    EndIf 
    Reg_CreateKeyValue(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ext$,"Application",prgkey$,#REG_SZ,"") 
  Else ;Windows 9x 
    Reg_CreateKeyValue(#HKEY_LOCAL_MACHINE  ,"Software\Classes\."+ext$                     ,"",prgkey$         ,#REG_SZ,"") 
    If ext_description$ 
      Reg_CreateKeyValue(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$                   ,"",ext_description$,#REG_SZ,"") 
    EndIf 
    If icon$ 
      Reg_CreateKeyValue(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\DefaultIcon"    ,"",icon$           ,#REG_SZ,"") 
    EndIf 
    If cmd_description$<>cmd_key$ 
      Reg_CreateKeyValue(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$,"",cmd_description$,#REG_SZ,"") 
    EndIf 
    Reg_CreateKeyValue(#HKEY_LOCAL_MACHINE  ,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$+"\command","",cmd$,#REG_SZ,"") 
  EndIf 
EndProcedure 
Procedure Remove_AssociateFile(ext$,prgkey$) 
  If GetVersion_() & $FF0000 ; Windows NT/XP 
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"Applications\"+prgkey$,"")
    Key$=ext$+"_auto_file" 
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"."+ext$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,Key$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ext$,"") 
  Else ;Windows 9x 
    Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE  ,"Software\Classes\."+ext$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$,"")
  EndIf 
EndProcedure 
Procedure AssociateFile(ext$,ext_description$,programm$,icon$) 
  AssociateFileEx(ext$,ext_description$,programm$,icon$,GetFilePart(programm$),"open","open") 
EndProcedure 
I did not test it but I hope it helps anyway.
:wink: sverson
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

freakin sweet.. is that the whole pbi ?

WHAT!?? you dont want to test the delete feature... lol oh come on..
lets start with deleting your devices.. ;)

Thanks for the file!! just what i needed !!

- np

*edit*

I tested it.. works like a charm.. the deleteall is VERY dangerous..

But just what I need.. <evil chuckle> ... jk

I'm writing a simple app, kinda like msconfig..
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

How do I call this one?

Code: Select all

Reg_SetValue(topKey, sKeyName.s, sValueName.s, vValue.s, lType, ComputerName.s) 
*edit*
Or rather how do I use the lType ?


- np
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

I don't need the lib when I have the source above.

I just need the usage of that procedure ..

- np
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1287
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

I don't need the lib when I have the source above.
I just need the usage of that procedure ..
:lol: You just answered your own question. You have the source, just look at it to see what is needed and what you need to do.

Your source indicated it is looking for lType to be either
#REG_SZ or #REG_DWORD
and by examining further you should understand why ;)
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Paul wrote:
I don't need the lib when I have the source above.
I just need the usage of that procedure ..
:lol: You just answered your own question. You have the source, just look at it to see what is needed and what you need to do.

Your source indicated it is looking for lType to be either
#REG_SZ or #REG_DWORD
and by examining further you should understand why ;)
grrr.. you couldn't let me be lazy for one darn post... :x

thanks..

- np
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1287
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

grrr.. you couldn't let me be lazy for one darn post...
lol @ NoahPhense
Image Image
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

None of these Registry delete tips seem to work for me. :( All I want to do is
delete this key (assume MyApp is a string and the value is MyApp.exe):

#HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\MyApp

Can somebody please show me how, with a minimum of code? Thanks. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1287
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Well, since you asked for the EASIEST way... use the RegINI_Lib from PureProject.net

DeleteRegKey(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run","MyApp")

Returns 1 on success

:)
Image Image
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

A lib is nice but it's like that old saying about giving a man a fish as opposed
to teaching him how to fish. ;) Still, I'll download the lib until I can learn how
to do it myself. Thanks!
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply