Advertisement
FlyFar

UDF.au3

Jul 13th, 2023
1,605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 14.73 KB | Cybersecurity | 0 0
  1. #include-once
  2.  
  3. ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
  4. ; #INDEX# =======================================================================================================================
  5. ; Title .........: _Startup
  6. ; AutoIt Version : v3.3.10.0 or higher
  7. ; Language ......: English
  8. ; Description ...: Create startup entries in the startup folder or registry. The registry entries can be Run all the time (Run registry entry) or only once (RunOnce registry entry.)
  9. ; Note ..........:
  10. ; Author(s) .....: guinness
  11. ; Remarks .......: Special thanks to KaFu for EnumRegKeys2Array() which I used as inspiration for enumerating the Registry Keys.
  12. ; ===============================================================================================================================
  13.  
  14. ; #INCLUDES# ====================================================================================================================
  15. #include <StringConstants.au3>
  16.  
  17. ; #GLOBAL VARIABLES# ============================================================================================================
  18. Global Enum $STARTUP_RUN = 0, $STARTUP_RUNONCE, $STARTUP_RUNONCEEX
  19.  
  20. ; #CURRENT# =====================================================================================================================
  21. ; _StartupFolder_Exists: Checks if an entry exits in the 'All Users/Current Users' startup folder.
  22. ; _StartupFolder_Install: Creates an entry in the 'All Users/Current Users' startup folder.
  23. ; _StartupFolder_Uninstall: Deletes an entry in the 'All Users/Current Users' startup folder.
  24. ; _StartupRegistry_Exists: Checks if an entry exits in the 'All Users/Current Users' registry.
  25. ; _StartupRegistry_Install: Creates an entry in the 'All Users/Current Users' registry.
  26. ; _StartupRegistry_Uninstall: Deletes the entry in the 'All Users/Current Users' registry.
  27. ; ===============================================================================================================================
  28.  
  29. ; #INTERNAL_USE_ONLY#============================================================================================================
  30. ; See below.
  31. ; ===============================================================================================================================
  32.  
  33. ; #FUNCTION# ====================================================================================================================
  34. ; Name ..........: _StartupFolder_Exists
  35. ; Description ...: Checks if an entry exits in the 'All Users/Current Users' startup folder.
  36. ; Syntax ........: _StartupFolder_Exists([$sName = @ScriptName[, $bAllUsers = False]])
  37. ; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
  38. ;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
  39. ; Return values .: Success - True
  40. ;                  Failure - False
  41. ; Author ........: guinness
  42. ; Example .......: Yes
  43. ; ===============================================================================================================================
  44. Func _StartupFolder_Exists($sName = @ScriptName, $bAllUsers = False)
  45.     Local $sFilePath = Default
  46.     __Startup_Format($sName, $sFilePath)
  47.     Return FileExists(__StartupFolder_Location($bAllUsers) & '\' & $sName & '.lnk')
  48. EndFunc   ;==>_StartupFolder_Exists
  49.  
  50. ; #FUNCTION# ====================================================================================================================
  51. ; Name ..........: _StartupFolder_Install
  52. ; Description ...: Creates an entry in the 'All Users/Current Users' startup folder.
  53. ; Syntax ........: _StartupFolder_Install([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $sCommandline = ''[,
  54. ;                  $bAllUsers = False]]]])
  55. ; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
  56. ;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
  57. ;                  $sCommandline        - [optional] Commandline arguments to be passed to the application. Default is ''.
  58. ;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
  59. ; Return values .: Success - True
  60. ;                  Failure - False & sets @error to non-zero
  61. ; Author ........: guinness
  62. ; Example .......: Yes
  63. ; ===============================================================================================================================
  64. Func _StartupFolder_Install($sName = @ScriptName, $sFilePath = @ScriptFullPath, $sCommandline = '', $bAllUsers = False)
  65.     Return __StartupFolder_Uninstall(True, $sName, $sFilePath, $sCommandline, $bAllUsers)
  66. EndFunc   ;==>_StartupFolder_Install
  67.  
  68. ; #FUNCTION# ====================================================================================================================
  69. ; Name ..........: _StartupFolder_Uninstall
  70. ; Description ...: Deletes an entry in the 'All Users/Current Users' startup folder.
  71. ; Syntax ........: _StartupFolder_Uninstall([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $bAllUsers = False]]])
  72. ; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
  73. ;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
  74. ;                  $bAllUsers           - [optional] Was it added to Current Users (False) or All Users (True) Default is False.
  75. ; Return values .: Success - True
  76. ;                  Failure - False & sets @error to non-zero
  77. ; Author ........: guinness
  78. ; Example .......: Yes
  79. ; ===============================================================================================================================
  80. Func _StartupFolder_Uninstall($sName = @ScriptName, $sFilePath = @ScriptFullPath, $bAllUsers = False)
  81.     Return __StartupFolder_Uninstall(False, $sName, $sFilePath, Default, $bAllUsers)
  82. EndFunc   ;==>_StartupFolder_Uninstall
  83.  
  84. ; #FUNCTION# ====================================================================================================================
  85. ; Name ..........: _StartupRegistry_Exists
  86. ; Description ...:Checks if an entry exits in the 'All Users/Current Users' registry.
  87. ; Syntax ........: _StartupRegistry_Exists([$sName = @ScriptName[, $bAllUsers = False[, $iRunOnce = $STARTUP_RUN]]])
  88. ; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
  89. ;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
  90. ;                  $iRunOnce            - [optional] Always run at system startup $STARTUP_RUN (0), run only once before explorer is started $STARTUP_RUNONCE (1)
  91. ;                                         or run only once after explorer is started $STARTUP_RUNONCEEX (2). Default is $STARTUP_RUN (0).
  92. ; Return values .: Success - True
  93. ;                  Failure - False
  94. ; Author ........: guinness
  95. ; Example .......: Yes
  96. ; ===============================================================================================================================
  97. Func _StartupRegistry_Exists($sName = @ScriptName, $bAllUsers = False, $iRunOnce = $STARTUP_RUN)
  98.     Local $sFilePath = Default
  99.     __Startup_Format($sName, $sFilePath)
  100.     RegRead(__StartupRegistry_Location($bAllUsers, $iRunOnce) & '\', $sName)
  101.     Return @error = 0
  102. EndFunc   ;==>_StartupRegistry_Exists
  103.  
  104. ; #FUNCTION# ====================================================================================================================
  105. ; Name ..........: _StartupRegistry_Install
  106. ; Description ...: Creates an entry in the 'All Users/Current Users' registry.
  107. ; Syntax ........: _StartupRegistry_Install([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $sCommandline = ''[,
  108. ;                  $bAllUsers = False[, $iRunOnce = $STARTUP_RUN]]]]])
  109. ; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
  110. ;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
  111. ;                  $sCommandline        - [optional] Commandline arguments to be passed to the application. Default is ''.
  112. ;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
  113. ;                  $iRunOnce            - [optional] Always run at system startup $STARTUP_RUN (0), run only once before explorer is started $STARTUP_RUNONCE (1)
  114. ;                                         or run only once after explorer is started $STARTUP_RUNONCEEX (2). Default is $STARTUP_RUN (0).
  115. ; Return values .: Success - True
  116. ;                  Failure - False & sets @error to non-zero
  117. ; Author ........: guinness
  118. ; Example .......: Yes
  119. ; ===============================================================================================================================
  120. Func _StartupRegistry_Install($sName = @ScriptName, $sFilePath = @ScriptFullPath, $sCommandline = '', $bAllUsers = False, $iRunOnce = $STARTUP_RUN)
  121.     Return __StartupRegistry_Uninstall(True, $sName, $sFilePath, $sCommandline, $bAllUsers, $iRunOnce)
  122. EndFunc   ;==>_StartupRegistry_Install
  123.  
  124. ; #FUNCTION# ====================================================================================================================
  125. ; Name ..........: _StartupRegistry_Uninstall
  126. ; Description ...: Deletes the entry in the 'All Users/Current Users' registry.
  127. ; Syntax ........: _StartupRegistry_Uninstall([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $bAllUsers = False[,
  128. ;                  $iRunOnce = Default]]]])
  129. ; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
  130. ;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
  131. ;                  $bAllUsers           - [optional] Was it added to the current users (0) or all users (1). Default is 0.
  132. ;                  $iRunOnce            - [optional] Was it run at system startup $STARTUP_RUN (0), run only once before explorer is started $STARTUP_RUNONCE (1)
  133. ;                                         or run only once after explorer is started $STARTUP_RUNONCEEX (2). Default is $STARTUP_RUN (0).
  134. ; Return values .: Success - True
  135. ;                  Failure - False & sets @error to non-zero
  136. ; Author ........: guinness
  137. ; Example .......: Yes
  138. ; ===============================================================================================================================
  139. Func _StartupRegistry_Uninstall($sName = @ScriptName, $sFilePath = @ScriptFullPath, $bAllUsers = False, $iRunOnce = $STARTUP_RUN)
  140.     Return __StartupRegistry_Uninstall(False, $sName, $sFilePath, Default, $bAllUsers, $iRunOnce)
  141. EndFunc   ;==>_StartupRegistry_Uninstall
  142.  
  143. ; #INTERNAL_USE_ONLY#============================================================================================================
  144. Func __Startup_Format(ByRef $sName, ByRef $sFilePath)
  145.     If $sFilePath = Default Then
  146.         $sFilePath = @ScriptFullPath
  147.     EndIf
  148.     If $sName = Default Then
  149.         $sName = @ScriptName
  150.     EndIf
  151.     $sName = StringRegExpReplace($sName, '\.[^.\\/]*$', '') ; Remove extension.
  152.     Return Not (StringStripWS($sName, $STR_STRIPALL) == '') And FileExists($sFilePath)
  153. EndFunc   ;==>__Startup_Format
  154.  
  155. Func __StartupFolder_Location($bAllUsers)
  156.     Return $bAllUsers ? @StartupCommonDir : @StartupDir
  157. EndFunc   ;==>__StartupFolder_Location
  158.  
  159. Func __StartupFolder_Uninstall($bIsInstall, $sName, $sFilePath, $sCommandline, $bAllUsers)
  160.     If Not __Startup_Format($sName, $sFilePath) Then
  161.         Return SetError(1, 0, False) ; $STARTUP_ERROR_EXISTS
  162.     EndIf
  163.     If $bAllUsers = Default Then
  164.         $bAllUsers = False
  165.     EndIf
  166.     If $sCommandline = Default Then
  167.         $sCommandline = ''
  168.     EndIf
  169.  
  170.     Local Const $sStartup = __StartupFolder_Location($bAllUsers)
  171.     Local Const $hSearch = FileFindFirstFile($sStartup & '\' & '*.lnk')
  172.     Local $vReturn = 0
  173.     If $hSearch > -1 Then
  174.         Local Const $iStringLen = StringLen($sName)
  175.         Local $aFileGetShortcut = 0, _
  176.                 $sFileName = ''
  177.         While 1
  178.             $sFileName = FileFindNextFile($hSearch)
  179.             If @error Then
  180.                 ExitLoop
  181.             EndIf
  182.             If StringLeft($sFileName, $iStringLen) = $sName Then
  183.                 $aFileGetShortcut = FileGetShortcut($sStartup & '\' & $sFileName)
  184.                 If @error Then
  185.                     ContinueLoop
  186.                 EndIf
  187.                 If $aFileGetShortcut[0] = $sFilePath Then
  188.                     $vReturn += FileDelete($sStartup & '\' & $sFileName)
  189.                 EndIf
  190.             EndIf
  191.         WEnd
  192.         FileClose($hSearch)
  193.     ElseIf Not $bIsInstall Then
  194.         Return SetError(2, 0, False) ; $STARTUP_ERROR_EMPTY
  195.     EndIf
  196.  
  197.     If $bIsInstall Then
  198.         $vReturn = FileCreateShortcut($sFilePath, $sStartup & '\' & $sName & '.lnk', $sStartup, $sCommandline) > 0
  199.     Else
  200.         $vReturn = $vReturn > 0
  201.     EndIf
  202.  
  203.     Return $vReturn
  204. EndFunc   ;==>__StartupFolder_Uninstall
  205.  
  206. Func __StartupRegistry_Location($bAllUsers, $iRunOnce)
  207.     If $iRunOnce = Default Then
  208.         $iRunOnce = $STARTUP_RUN
  209.     EndIf
  210.     Local $sRunOnce = ''
  211.     Switch $iRunOnce
  212.         Case $STARTUP_RUNONCE
  213.             $sRunOnce = 'Once'
  214.         Case $STARTUP_RUNONCEEX
  215.             $sRunOnce = 'OnceEx'
  216.         Case Else
  217.             $sRunOnce = ''
  218.     EndSwitch
  219.  
  220.     Return ($bAllUsers ? 'HKEY_LOCAL_MACHINE' : 'HKEY_CURRENT_USER') & _
  221.             ((@OSArch = 'X64') ? '64' : '') & '\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' & $sRunOnce
  222. EndFunc   ;==>__StartupRegistry_Location
  223.  
  224. Func __StartupRegistry_Uninstall($bIsInstall, $sName, $sFilePath, $sCommandline, $bAllUsers, $iRunOnce)
  225.     If Not __Startup_Format($sName, $sFilePath) Then
  226.         Return SetError(1, 0, False) ; $STARTUP_ERROR_EXISTS
  227.     EndIf
  228.     If $bAllUsers = Default Then
  229.         $bAllUsers = False
  230.     EndIf
  231.     If $sCommandline = Default Then
  232.         $sCommandline = ''
  233.     EndIf
  234.  
  235.     Local Const $sRegistryKey = __StartupRegistry_Location($bAllUsers, $iRunOnce)
  236.     Local $iInstance = 1, _
  237.             $sRegistryName = '', _
  238.             $vReturn = 0
  239.     While 1
  240.         $sRegistryName = RegEnumVal($sRegistryKey & '\', $iInstance)
  241.         If @error Then
  242.             ExitLoop
  243.         EndIf
  244.  
  245.         If ($sRegistryName = $sName) And StringInStr(RegRead($sRegistryKey & '\', $sRegistryName), $sFilePath, $STR_NOCASESENSEBASIC) Then
  246.             $vReturn += RegDelete($sRegistryKey & '\', $sName)
  247.         EndIf
  248.         $iInstance += 1
  249.     WEnd
  250.  
  251.     If $bIsInstall Then
  252.         $vReturn = RegWrite($sRegistryKey & '\', $sName, 'REG_SZ', $sFilePath & ' ' & $sCommandline) > 0
  253.     Else
  254.         $vReturn = $vReturn > 0
  255.     EndIf
  256.  
  257.     Return $vReturn
  258. EndFunc   ;==>__StartupRegistry_Uninstall
Tags: startup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement