Registry.pbi (Überarbeitet)
Verfasst: 18.11.2007 16:52
betreffend des Thread http://www.purebasic.fr/german/viewtopic.php?t=14893 habe ich den Code mal überarbeitet.
Registry.pbi
Test
FF 
Registry.pbi
Code: Alles auswählen
;-TOP
; Kommentar : Read and change the Registry / FileAssociate
; Author : Unknown
; Second Author : mk-soft
; Datei : Registry.pbi
; Version : 1.01
; Erstellt : ???
; Geändert : 18.11.2007
;
; Compilermode :
;
; ***************************************************************************************
;#PB_Lit2Msg stop
;EnableExplicit
; ***************************************************************************************
Global RegLasterror.l
Global RegLastMessage.s
; ***************************************************************************************
CompilerIf Defined(FormatMessage, #PB_Procedure) = #False
Procedure.s FormatMessage(ErrorNumber.l)
Protected *Buffer, len, result.s
len = FormatMessage_(#FORMAT_MESSAGE_ALLOCATE_BUFFER|#FORMAT_MESSAGE_FROM_SYSTEM,0,ErrorNumber,0,@*Buffer,0,0)
If len
result = PeekS(*Buffer, len - 2)
LocalFree_(*Buffer)
ProcedureReturn result
Else
ProcedureReturn "Errorcode: " + Hex(ErrorNumber)
EndIf
EndProcedure
CompilerEndIf
; ***************************************************************************************
Procedure Reg_SetValue(topKey, sKeyName.s, sValueName.s, vValue.s, lType, ComputerName.s = "")
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, Result.l
Protected lpData.s{256}
Protected lpcbData.l, lValue.l
RegLastError = 0
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn #False
EndIf
r1 = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
EndIf
If r1 = #ERROR_SUCCESS
lpcbData = 255
Select lType
Case #REG_SZ
r1 = RegSetValueEx_(hKey, sValueName, 0, #REG_SZ, @vValue, Len(vValue) + 1)
Case #REG_DWORD
lValue = Val(vValue)
r1 = RegSetValueEx_(hKey, sValueName, 0, #REG_DWORD, @lValue, 4)
EndSelect
If r1 = #ERROR_SUCCESS
Result = #True
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
Result = #False
EndIf
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
Result = #False
EndIf
; Close Key
RegCloseKey_(hKey)
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn Result
EndProcedure
; ***************************************************************************************
Procedure.s Reg_GetValue(topKey, sKeyName.s, sValueName.s, ComputerName.s = "")
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, Result.l
Protected lpData.s{256}, GetValue.s
Protected lType.l, lpcbData.l, lpDataDWORD.l
RegLastError = 0
GetValue.s = ""
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn ""
EndIf
r1 = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
EndIf
If r1 = #ERROR_SUCCESS
lpcbData = 255
r1 = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData)
If r1 = #ERROR_SUCCESS
Select lType
Case #REG_SZ
r1 = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData)
If r1 = #ERROR_SUCCESS
GetValue = Left(lpData, lpcbData - 1)
Else
GetValue = ""
EndIf
Case #REG_DWORD
r1 = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpDataDWORD, @lpcbData)
If r1 = #ERROR_SUCCESS
GetValue = Str(lpDataDWORD)
Else
GetValue = ""
EndIf
EndSelect
If r1 = #ERROR_SUCCESS
Result = #True
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
Result = #False
EndIf
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
EndIf
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
EndIf
; Close Key
RegCloseKey_(hKey)
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn GetValue
EndProcedure
; ***************************************************************************************
Procedure.s Reg_ListSubKey(topKey, sKeyName.s, Index, ComputerName.s = "")
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, Result.l
Protected lpName.s{256}, ListSubKey.s
Protected lpcbName.l
Protected lpftLastWriteTime.FILETIME
RegLastError = 0
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn ""
EndIf
r1 = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
EndIf
If r1 = #ERROR_SUCCESS
lpcbName = 255
r1 = RegEnumKeyEx_(hKey, Index, @lpName, @lpcbName, 0, 0, 0, @lpftLastWriteTime)
If r1 = #ERROR_SUCCESS
ListSubKey.s = Left(lpName, lpcbName)
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ListSubKey.s = ""
EndIf
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ListSubKey.s = ""
EndIf
; Close Key
RegCloseKey_(hKey)
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn ListSubKey
EndProcedure
; ***************************************************************************************
Procedure Reg_DeleteValue(topKey, sKeyName.s, sValueName.s, ComputerName.s = "")
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, DeleteValue.l
RegLastError = 0
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn #False
EndIf
r1 = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
EndIf
If r1 = #ERROR_SUCCESS
r1 = RegDeleteValue_(hKey, @sValueName)
If r1 = #ERROR_SUCCESS
DeleteValue = #True
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
DeleteValue = #False
EndIf
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
DeleteValue = #False
EndIf
; Close Key
RegCloseKey_(hKey)
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn DeleteValue
EndProcedure
; ***************************************************************************************
Procedure Reg_CreateKey(topKey, sKeyName.s, ComputerName.s = "")
; Result #REG_CREATED_NEW_KEY = 1
; Result #REG_OPENED_EXISTING_KEY = 2
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, CreateKey.l
Protected lpSecurityAttributes.SECURITY_ATTRIBUTES
RegLastError = 0
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegCreateKeyEx_(topKey, sKeyName, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @lpSecurityAttributes, @hKey, @CreateKey)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn #False
EndIf
r1 = RegCreateKeyEx_(lhRemoteRegistry, sKeyName, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @lpSecurityAttributes, @hKey, @CreateKey)
EndIf
If r1 = #ERROR_SUCCESS
;
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
EndIf
; Close Key
RegCloseKey_(hKey)
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn CreateKey
EndProcedure
; ***************************************************************************************
Procedure Reg_DeleteKey(topKey, sKeyName.s, ComputerName.s = "")
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, DeleteKey.l
RegLastError = 0
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegDeleteKey_(topKey, @sKeyName)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn #False
EndIf
r1 = RegDeleteKey_(lhRemoteRegistry, @sKeyName)
EndIf
If r1 = #ERROR_SUCCESS
DeleteKey = #True
Else
DeleteKey = #False
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
EndIf
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn DeleteKey
EndProcedure
; ***************************************************************************************
Procedure.s Reg_ListSubValue(topKey, sKeyName.s, Index, ComputerName.s = "")
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, Result.l
Protected lpName.s{256}, ListSubValue.s
Protected lpcbName.l
Protected lpftLastWriteTime.FILETIME
RegLastError = 0
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn ""
EndIf
r1 = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
EndIf
If r1 = #ERROR_SUCCESS
lpcbName = 255
r1 = RegEnumValue_(hKey, Index, @lpName, @lpcbName, 0, 0, 0, 0)
If r1 = #ERROR_SUCCESS
ListSubValue = Left(lpName, lpcbName)
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ListSubValue.s = ""
EndIf
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ListSubValue.s = ""
EndIf
; Close Key
RegCloseKey_(hKey)
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn ListSubValue
EndProcedure
; ***************************************************************************************
Procedure Reg_KeyExists(topKey, sKeyName.s, ComputerName.s = "")
Protected hKey.l, lhRemoteRegistry.l
Protected r1.l, KeyExists.l
RegLastError = 0
If Left(sKeyName, 1) = "\" : sKeyName = Right(sKeyName, Len(sKeyName) - 1) : EndIf
If Right(sKeyName,1)="\" : sKeyName = Left(sKeyName, Len(sKeyName) - 1) : EndIf
If ComputerName = ""
r1 = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
Else
r1 = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
If r1 <> #ERROR_SUCCESS
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
ProcedureReturn #False
EndIf
r1 = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
EndIf
If r1 = #ERROR_SUCCESS
KeyExists = #True
Else
RegLastError = r1
RegLastMessage = FormatMessage(RegLastError)
KeyExists = #False
EndIf
; Close Key
RegCloseKey_(hKey)
; Close Remote
If lhRemoteRegistry
RegCloseKey_(lhRemoteRegistry)
EndIf
ProcedureReturn KeyExists
EndProcedure
; ***************************************************************************************
Procedure Reg_DeleteKeyWithAllSub(topKey,sKeyName.s,ComputerName.s = "")
Protected i.l
Protected a$="", b$
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(AF_Ext$,ext_description$,programm$,icon$,prgkey$,cmd_description$,cmd_key$)
Protected 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$=AF_Ext$+"_auto_file"
Reg_CreateKeyValue(#HKEY_CLASSES_ROOT ,"."+AF_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\."+AF_Ext$,"Application",prgkey$,#REG_SZ,"")
Else ;Windows 9x
Reg_CreateKeyValue(#HKEY_LOCAL_MACHINE ,"Software\Classes\."+AF_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(AF_Ext$,prgkey$)
Protected key$
If GetVersion_() & $FF0000 ; Windows NT/XP
Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"Applications\"+prgkey$,"")
key$=AF_Ext$+"_auto_file"
Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"."+AF_Ext$,"")
Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,key$,"")
Reg_DeleteKeyWithAllSub(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+AF_Ext$,"")
Else ;Windows 9x
Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE ,"Software\Classes\."+AF_Ext$,"")
Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$,"")
EndIf
EndProcedure
; ***************************************************************************************
Procedure AssociateFile(AF_Ext$,ext_description$,programm$,icon$)
AssociateFileEx(AF_Ext$,ext_description$,programm$,icon$,GetFilePart(programm$),"open","open")
EndProcedure
;#PB_Lit2Msg start
; ***************************************************************************************
Code: Alles auswählen
IncludeFile "Registry.pbi"
Debug "Fehler Read"
Debug Reg_GetValue(#HKEY_CURRENT_USER, "Control Panel\Desktop", "Any")
If RegLastError
Debug RegLastMessage
EndIf
Debug "Read Wallpaper"
Debug Reg_GetValue(#HKEY_CURRENT_USER, "Control Panel\Desktop", "Wallpaper")
If RegLastError
Debug RegLastMessage
EndIf
Debug "Create Key"
Debug Reg_CreateKey(#HKEY_LOCAL_MACHINE, "Software\MyTestKey")
If RegLastError
Debug RegLastMessage
EndIf
Debug "SetValue Text"
Debug Reg_SetValue(#HKEY_LOCAL_MACHINE, "Software\MyTestKey", "TextValue", "Hallo Welt", #REG_SZ)
If RegLastError
Debug RegLastMessage
EndIf
Debug "SetValue DWORD"
Debug Reg_SetValue(#HKEY_LOCAL_MACHINE, "Software\MyTestKey", "DWordValue", Str(12345678), #REG_DWORD)
If RegLastError
Debug RegLastMessage
EndIf
Debug "GetValue Text"
Debug Reg_GetValue(#HKEY_LOCAL_MACHINE, "Software\MyTestKey", "TextValue")
If RegLastError
Debug RegLastMessage
EndIf
Debug "GetValue DWORD"
Debug Reg_GetValue(#HKEY_LOCAL_MACHINE, "Software\MyTestKey", "DWordValue")
If RegLastError
Debug RegLastMessage
EndIf
Debug "List SubValue"
i = 0
Repeat
result.s = Reg_ListSubValue(#HKEY_LOCAL_MACHINE, "Software\MyTestKey", i)
If result = ""
Break
EndIf
Debug Str(i) + " : " + Result
i + 1
ForEver
Debug "List SubKey"
i = 0
Repeat
result.s = Reg_ListSubKey(#HKEY_LOCAL_MACHINE, "Software", i)
If result = ""
Break
EndIf
Debug Str(i) + " : " + Result
i + 1
ForEver
;Reg_DeleteKey(#HKEY_LOCAL_MACHINE, "Software\MyTestKey")
If RegLastError
Debug RegLastMessage
EndIf
