Registryfunktionen - Erweiterbar

  • Hallo,

    ich habe eben eine Art UDF für Registryfunktionen geschrieben. Bisher ist noch nicht allzu viel drin (Beachtet bitte auch die Kommentare), aber ich hoffe, dass sich im Laufe der Zeit noch einiges zusammenfindet.
    Gedacht ist es hauptsächlich zur Vereinfachung, wenn mal wieder ein Eintrag ansteht um das Programm z.B. in den Autostart zu setzen oder unter "Software" einzutragen, etc.
    Ich hoffe euch gefällt mein Ansatz und helft mir ihn zu erweitern :)

    Aquaplant

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    AutoIt Version: 3.3.6.0
    Author: Aquaplant

    [/autoit] [autoit][/autoit] [autoit]

    Script Function:
    See description below

    [/autoit] [autoit][/autoit] [autoit]

    #ce ----------------------------------------------------------------------------
    #RequireAdmin

    [/autoit] [autoit][/autoit] [autoit]

    ; Name..........: _RegisterExtension
    ; Description...: Register your wanted file-extension
    ; Syntax........: _RegisterExtension($sAbbrev[, $sExtName[, $sOpenPath[, $sEditPath[, $sIconPath, $iIconNr]]]])
    ; Parameters....:
    ; $sAbbrev = the abbreviation you want to use/register, e.g. ".exa" or ".hi"; Don't use standard abbrev. like ".exe"!
    ; $sExtName = the name which is used to create the substructure; Don't use blanks!, Default is "" = [your abbrev.]_auto_file
    ; $sOpenPath & $sEditPath = the pathes (to your executable) with which the registered file will opened/edited, Default is "" = no path
    ; $sIconPath = a path to a file which contains one or more icons which will be used for your registered files; by using this you also need the last parameter!
    ; $iIconNr = you need this but at the moment i don't know more
    ; Return values: Success - Returns 1
    ; Failure - Returns -1 sets @error
    ;|1 String doesn't contain a "." at the beginning
    ;|2 The abbreviation contains at least one blank (" ")
    ;|3 Unable to write the registry key and sets @extended
    ;|-1 if unable to open requested value
    ;|-2 if value type not supported
    ;|1 if unable to open requested key
    ;|2 if unable to open requested main key
    ;|3 if unable to remote connect to the registry
    ; Author ........: Aquaplant
    ; Modified.......: /
    ; Remarks .......: Not tested now
    ; Example .......; No

    [/autoit] [autoit][/autoit] [autoit]

    Func _RegisterExtension($sAbbrev, $sExtName = "", $sOpenPath = "", $sEditPath = "", $sIconPath = "", $iIconNr = "")
    If StringInStr($sAbbrev, ".") <> 1 Then
    SetError(1)
    Return -1
    ElseIf StringInStr($sAbbrev, " ") <> 0 Then
    SetError(2)
    Return -1
    ElseIf $sExtName = "" Then
    $sExtName = $sAbbrev & "_auto_file"
    Else
    $sExtName = StringReplace($sExtName, " ", "_")
    EndIf
    RegWrite("HKEY_CLASSES_ROOT\" & $sAbbrev, "", "REG_SZ", $sExtName)
    If @error Then
    SetError(3, @error)
    Return -1
    EndIf
    If $sOpenPath <> "" Then
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\", "", "REG_SZ")
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\shell\", "", "REG_SZ")
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\shell\open\", "", "REG_SZ")
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\shell\open\command\", "", "REG_EXPAND_SZ", $sOpenPath & " %1")
    If @error Then
    SetError(3, @error)
    Return -1
    EndIf
    EndIf
    If $sEditPath <> "" Then
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\", "", "REG_SZ")
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\shell\", "", "REG_SZ")
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\shell\edit\", "", "REG_SZ")
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\shell\edit\command\", "", "REG_EXPAND_SZ", $sEditPath & " %1")
    If @error Then
    SetError(3, @error)
    Return -1
    EndIf
    EndIf
    If $sIconPath <> "" And $iIconNr <> "" Then
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\", "", "REG_SZ")
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\DefaultIcon\", "", "REG_SZ", $sIconPath & ", " & $iIconNr)
    If @error Then
    SetError(3, @error)
    Return -1
    EndIf
    EndIf
    Return 1
    EndFunc ;==>_RegisterExtension

    [/autoit] [autoit][/autoit] [autoit]

    ; Name..........: _CreateGenSoftwareEntries
    ; Description...: Sets general or often needed informations to the Windows(C) software menu (start --> control --> software)
    ; Syntax........: _CreateGenSoftwareEntries($sName[, $sVersion[, $sUninstallPath[, $sIconPath[, sPublisher[, $sInstallSourcePath[, $sProgramDir]]]]]])
    ; Parameters....:
    ; $sName = the program name which will be shown (e.g. "Regestry functions"), Default = ""
    ; $sVersion = the (program) version which will be shown (e.g. 1.0 beta), Default = ""
    ; $sUninstallPath = the path to your uninstall.exe, Default = ""
    ; $sIconPath = the path to an icon which will be shown (*.ico), Default = ""
    ; sPublisher = the publisher('s name), Default = ""
    ; $sInstallSourcePath = the path to your standard executive, Default = ""
    ; $sProgramDir = the direction in which your program was installed, Default = ""
    ; Return values: Success - Returns 1
    ; Failure - Returns -1 sets @error
    ;|1 No Name
    ;|2 Unable to write the registry key and sets @extended
    ;|-1 if unable to open requested value
    ;|-2 if value type not supported
    ;|1 if unable to open requested key
    ;|2 if unable to open requested main key
    ;|3 if unable to remote connect to the registry
    ; Author ........: Aquaplant
    ; Modified.......: /
    ; Remarks .......: Automatically writes the actual time as the installation-time
    ; Example .......; No

    [/autoit] [autoit][/autoit] [autoit]

    Func _CreateGenSoftwareEntries($sName, $sVersion = "", $sUninstallPath = "", $sIconPath = "", $sPublisher = "", $sInstallSourcePath = "", $sProgramDir = "")
    If $sName = "" Or $sName = " " Then
    SetError(1)
    Return -1
    Else
    $sName2 = StringReplace($sName, " ", "_")
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sName2 & "\", "DisplayName", "Reg_SZ", $sName)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test\", "InstallDate", "Reg_SZ", @YEAR & @MON & @MDAY) ;Date
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    If $sVersion <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sName2 & "\", "DisplayVersion", "Reg_SZ", $sVersion)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sUninstallPath <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sName2 & "\", "UninstallString", "Reg_SZ", $sUninstallPath)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sIconPath <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sName2 & "\", "DisplayIcon", "Reg_SZ", $sIconPath)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sPublisher <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sName2 & "\", "Publisher", "Reg_SZ", $sPublisher)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sInstallSourcePath <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sName2 & "\", "InstallSource", "Reg_SZ", $sInstallSourcePath)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sProgramDir <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sName2 & "\", "InstallLocation", "Reg_SZ", $sProgramDir)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    Return 1
    EndIf
    EndFunc ;==>_CreateGenSoftwareEntries

    [/autoit] [autoit][/autoit] [autoit]

    ; Name..........: _CreateAdvSoftwareEntries
    ; Description...: Sets advanced informations to the Windows(C) software menu (start --> control --> software)
    ; Syntax........: _CreateAdvSoftwareEntries($sSoftwareName[, $sEMail[, $sURL4Info[, $sURL4Update[, $sURLHelp[, $sComments]]]]])
    ; Parameters....:
    ; $sSoftwareName = the program name which you used by calling _CreateGenSoftwareEntries() beforehand, Default = ""
    ; $sEMail = (your) E-Mail address, Default = ""
    ; $sURL4Info = a link for informations about your program, Default = ""
    ; $sURL4Update = a link for (informations about) an update, Default = ""
    ; $sURLHelp = a link to an online-help, Default = ""
    ; $sComments = a comment, Default = ""
    ; Return values: Success - Returns 1
    ; Failure - Returns -1 sets @error
    ;|1 No Name
    ;|2 Unable to write the registry key and sets @extended
    ;|-1 if unable to open requested value
    ;|-2 if value type not supported
    ;|1 if unable to open requested key
    ;|2 if unable to open requested main key
    ;|3 if unable to remote connect to the registry
    ; Author ........: Aquaplant
    ; Modified.......: /
    ; Remarks .......: /
    ; Example .......; No

    [/autoit] [autoit][/autoit] [autoit]

    Func _CreateAdvSoftwareEntries($sSoftwareName, $sEMail = "", $sURL4Info = "", $sURL4Update = "", $sURLHelp = "", $sComments = "")
    If $sSoftwareName = "" Or $sSoftwareName = " " Then
    SetError(1)
    Return -1
    Else
    $sSoftwareName = StringReplace($sSoftwareName, " ", "_")
    EndIf
    If $sEMail <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sSoftwareName & "\", "Contact", "Reg_SZ", $sEMail)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sURL4Info <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sSoftwareName & "\", "URLInfoAbout", "Reg_SZ", $sURL4Info)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sURL4Update <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sSoftwareName & "\", "URLUpdateInfo", "Reg_SZ", $sURL4Update)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sURLHelp <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sSoftwareName & "\", "Helplink", "Reg_SZ", $sURLHelp)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    If $sComments <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sSoftwareName & "\", "Comments", "Reg_SZ", $sComments)
    If @error Then
    SetError(2, @error)
    Return -1
    EndIf
    EndIf
    Return 1
    EndFunc ;==>_CreateAdvSoftwareEntries

    [/autoit] [autoit][/autoit] [autoit]

    ; Name..........: _CreateAutostart
    ; Description...: Sets your program into the autostart (see cmd --> msconfig)
    ; Syntax........: _CreateAutostart($sAutostartName, $sPath)
    ; Parameters....:
    ; $sAutostartName = the name you can see in msconfig (mustn't be the same as your program)
    ; $sPath = the full path to the file
    ; Return values: Success - Returns 1
    ; Failure - Returns -1 sets @error
    ;|1 Name or path doesn't fit
    ;|2 Unable to write the registry key and sets @extended
    ;|-1 if unable to open requested value
    ;|-2 if value type not supported
    ;|1 if unable to open requested key
    ;|2 if unable to open requested main key
    ;|3 if unable to remote connect to the registry
    ; Author ........: Aquaplant
    ; Modified.......: /
    ; Remarks .......: /
    ; Example .......; No

    [/autoit] [autoit][/autoit] [autoit]

    Func _CreateAutostart($sAutostartName, $sPath)
    If $sAutostartName <> "" And StringInStr($sPath, " ") = 0 And $sPath <> "" Then
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", $sAutostartName, "REG_SZ", $sPath)
    If @error Then
    SetError(1, @error)
    Return -1
    EndIf
    Return 1
    Else
    SetError(1)
    Return -1
    EndIf
    EndFunc ;==>_CreateAutostart

    [/autoit] [autoit][/autoit] [autoit]

    ; Name..........: _CreateContextMenu
    ; Description...: Adds a file to the context menu (right click --> New)
    ; Syntax........: _CreateContextMenu($sExtName[, $sStandardName])
    ; Parameters....:
    ; $sExtName = the file-ending (e.g. ".txt" or ".ah5")
    ; $sStandardName = the name for the menu, Default = "" = registered Name
    ; Return values: Success - Returns 1
    ; Failure - Returns -1 sets @error
    ;|1 Name doesn't fit
    ;|2 Registered entry for the $sStandardname wasn't found (only default)
    ;|3 Unable to write the registry key and sets @extended
    ;|-1 if unable to open requested value
    ;|-2 if value type not supported
    ;|1 if unable to open requested key
    ;|2 if unable to open requested main key
    ;|3 if unable to remote connect to the registry
    ; Author ........: Aquaplant
    ; Modified.......: /
    ; Remarks .......: Didn't work on my system - no error ?
    ; Example .......; No

    [/autoit] [autoit][/autoit] [autoit]

    Func _CreateContextMenu($sExtName, $sStandardName = "")
    If $sExtName = "" Or StringLeft($sExtName, 1) <> "." Then
    SetError(1)
    Return -1
    EndIf
    If $sStandardName = "" Then
    $sStandardName = RegRead("HKEY_CLASSES_ROOT\" & $sExtName & "\", "")
    If $sStandardName = "" Then
    SetError(2)
    Return -1
    EndIf
    EndIf
    $sStandardName = StringReplace($sStandardName, " ", ".")
    If StringInStr($sStandardName, "(") <> 0 Then
    $aStandardName = StringSplit($sStandardName, "(")
    $sStandardName = $aStandardName[1]
    If StringRight($sStandardName, 1) = "." Then
    $sStandardName = StringTrimRight($sStandardName, 1)
    EndIf
    EndIf
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\" & $sStandardName & "\", "", "REG_SZ")
    If @error Then
    SetError(3, @error)
    Return -1
    EndIf
    RegWrite("HKEY_CLASSES_ROOT\" & $sExtName & "\" & $sStandardName & "\ShellNew\", "Nullfile", "REG_SZ")
    If @error Then
    SetError(3, @error)
    Return -1
    EndIf
    Return 1
    EndFunc ;==>_CreateContextMenu

    [/autoit] [autoit][/autoit] [autoit]

    ; Name..........: _ChangeDefPrinter
    ; Description...: Changes the default printer
    ; Syntax........: _CreateContextMenu($sNewDefPrinter)
    ; Parameters....:
    ; $sExtName = the new default printer (e.g. "HP Photosmart 7200 Series"); ATTENTION WITH NETWORK PRINTERS (networkpath)
    ; Return values: Success - Returns 1
    ; Failure - Returns -1 sets @error
    ; |1 Unable to write the registry key and sets @extended
    ; |-1 if unable to open requested value
    ; |-2 if value type not supported
    ; |1 if unable to open requested key
    ; |2 if unable to open requested main key
    ; |3 if unable to remote connect to the registry
    ; Author ........: Aquaplant
    ; Modified.......: /
    ; Remarks .......: Attention - see parameters
    ; Example .......; No

    [/autoit] [autoit][/autoit] [autoit]

    Func _ChangeDefPrinter($sNewDefPrinter)
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrenVersion\Windows\", "Device", "REG_SZ", $sNewDefPrinter & ",winspool,Ne02:")
    If @error Then
    SetError(1, @error)
    Return -1
    Else
    Return 1
    EndIf
    EndFunc

    [/autoit]
  • Als kleine Verbesserung solltest die Standards für UDFs einhalten, macht die ganze Sache Userfreundlicher: klick mich

    Andy hat mir ein Schnitzel gebacken aber da war ein Raupi drauf und bevor Oscar das Bugfixen konnte kam Alina und gab mir ein AspirinJunkie.