Windows Registry

Just starting out? Need help? Post your questions and find answers here.
Waussie
User
User
Posts: 24
Joined: Fri Nov 11, 2005 12:14 pm

Windows Registry

Post by Waussie »

Can anyone tell me how to retrieve, edit, delete registryentries?
I really don't have a clue, cos there don't seem to be any commands for it...

Thanx in advance...
Currently coding the unbelievable WFS program...
You'll know it, when it gets released...
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

you can try the Droopy Lib
Waussie
User
User
Posts: 24
Joined: Fri Nov 11, 2005 12:14 pm

Registry

Post by Waussie »

hmm, although the library might be usefull, i was looking for some code to retrieve it. (Never worked with libraries before...)
Currently coding the unbelievable WFS program...
You'll know it, when it gets released...
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Japbe Registry Include File wrote:

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 
--Kale

Image
Waussie
User
User
Posts: 24
Joined: Fri Nov 11, 2005 12:14 pm

Such code...

Post by Waussie »

I was thinking more of using the windows reg-command.
Something like this...

Parameter$="query HKLM\SOFTWARE >c:\test.txt"
RunProgram("C:\windows\system32\reg.exe", Parameter$, "", 1)


And after that readout test.txt to get the value.
For some reason above doesn't create the testfile, why?

And is there a way to skip the file making and capture it directly into a variable?
Currently coding the unbelievable WFS program...
You'll know it, when it gets released...
Post Reply