Showing posts with label regedit. Show all posts
Showing posts with label regedit. Show all posts

Sunday, April 12, 2009

Run custom application or script as a Windows service

If you need your application or script to be started, whenever Windows is booted, you need to install it as a service (so  it will even be started when you didn’t login yet). Microsoft offers a little tool to easily create your own service and make it run some batch file / application as a part of their Windows Server Resource Kit Tools. From this package, you only need srvany.exe and instsrv.exe to install your custom service, but you still need to perform all this manual configuration before your service will be running.

I created the following batch script to easily install or uninstall your custom application as a service. All required information will be requested interactively to the user step by step. No further manual configuration will be needed anymore. Errors will be shown with colors and attention points will be flashed.

@echo off
echo.
echo.
echo Use this script to install / uninstall your custom applications or scripts as a service and make sure they will be started whenever Windows is booted.
SET DFAULT_BACKGROUND_COLOR=0
SET DEFAULT_TEXT_COLOR=7
SET ERROR_BACKGROUND_COLOR=4
SET ERROR_TEXT_COLOR=F
SET ATTENTION_BACKGROUND_COLOR=0
SET ATTENTION_TEXT_COLOR=F
rem 0 = Black
rem 1 = Blue
rem 2 = Green
rem 3 = Aqua
rem 4 = Red
rem 5 = Purple
rem 6 = Yellow
rem 7 = White
rem 8 = Gray
rem 9 = Light Blue
rem A = Light Green
rem B = Light Aqua
rem C = Light Red
rem D = Light Purple
rem E = Light Yellow
rem F = Bright White
:MAIN_MENU
  COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  SET RETURN_DRAW_ATTENTION_POINT=MAIN_MENU_INPUT
  GOTO DRAW_ATTENTION
  :MAIN_MENU_INPUT
    echo.
    echo.
    SET INPUT=
    echo To Install a new custom service, press I.
    echo To Uninstall a previously created custom service, press U.
    echo To open the Windows Services application, press S.
    set /p INPUT=Press E to Exit: 
    if /i "%INPUT%" == "i" goto START_INSTALL
    if /i "%INPUT%" == "u" goto START_UNINSTALL
    if /i "%INPUT%" == "s" goto START_SERVICES
    if /i "%INPUT%" == "e" goto EXIT
    GOTO MAIN_MENU
  :END_MAIN_MENU_INPUT
  echo.     
:END_MAIN_MENU
  
:START_INSTALL
  rem INTERACTIVE MODE is used, lines below kept for reference
  :HARD_CODED_PATHS_AND_NAMES
  rem APP_NAME=REPLACE_THIS_WITH_YOUR_CUSTOM_APP_NAME
  rem APP_PATH=FULL_PATH_TO_CUSTOM_APP
  rem one can use %CD% in the path, which will be replace by the current directory
  rem uncomment following 2 lines if you want to use the folder name (where this bat file is run) as service name APP_NAME
  rem SET APP_NAME=
  rem for %%* in (.) do set APP_NAME=%%~n*
  
  :INTERACTIVE_INPUT_OF_PATHS_AND_NAMES
    :INTERACTIVE_INPUT_APP_NAME
      echo.
      SET APP_NAME=
      set /p APP_NAME=Provide the name of your custom application (will be used as the name of the service): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_NAME
    
    :INTERACTIVE_INPUT_OF_APP_PATH
      echo. 
      SET APP_PATH=
      set /p APP_PATH=Provide the full path of your custom application to run as a service (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_OF_APP_PATH
    
    :CHECK_CUSTOM_APP_PATH
      IF EXIST "%APP_PATH%" GOTO END_CHECK_CUSTOM_APP_PATH
      echo.
      echo Your custom application %APP_NAME% could not be found at %APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_CUSTOM_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
      
    :INTERACTIVE_INPUT_APP_FOLDER
      echo.
      SET APP_FOLDER=
      set /p APP_FOLDER=Provide the folder to be used as working directory for your custom application (press ENTER if not required) (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_PARAM
      
    :INTERACTIVE_INPUT_APP_PARAM
      echo.
      SET APP_PARAM=
      set /p APP_PARAM=Provide the parameters of your custom application (press ENTER if no parameters are required) (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_PARAM
    
    :INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
      echo. 
      SET INST_SERVICE_APP_PATH=
      set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe): 
    :END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
    
    :CHECK_INST_SERVICE_APP_PATH_INPUT
      if "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH
    :END_CHECK_INST_SERVICE_APP_PATH_INPUT
    
    :CHECK_INST_SERVICE_APP_PATH
      IF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATH
      echo.
      echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_INST_SERVICE_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
    
    :INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
      echo. 
      SET SERVICE_APP_PATH=
      set /p SERVICE_APP_PATH=Provide the full path to srvany.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\srvany.exe): 
    :END_INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
    
    :CHECK_SERVICE_APP_PATH_INPUT
      if "%SERVICE_APP_PATH%"=="" goto DEFAULT_SERVICE_APP_PATH
    :END_CHECK_SERVICE_APP_PATH_INPUT
    
    :CHECK_SERVICE_APP_PATH
      IF EXIST "%SERVICE_APP_PATH%" GOTO END_CHECK_SERVICE_APP_PATH
      echo.
      echo srvany.exe could not be found at %SERVICE_APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_SERVICE_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  :END_INTERACTIVE_INPUT_OF_PATHS_AND_NAMES  
  
  :SHOW_CONFIG
    echo.
    echo Ready to install your custom application, using the following configuration:
    echo.
    echo   Custom application name: %APP_NAME%
    echo   Custom application full path: %APP_PATH%
    echo   Custom application parameters: %APP_PARAM%
    echo   Custom application working directory: %APP_FOLDER%
    echo   srvany.exe full path: %SERVICE_APP_PATH%
    echo   instsrv.exe full path: %INST_SERVICE_APP_PATH%
    echo.
    SET RETURN_DRAW_ATTENTION_POINT=SHOW_CONFIG_WAIT
    GOTO DRAW_ATTENTION
    :SHOW_CONFIG_WAIT
      COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
      set INPUT=
      set /p INPUT=Press ENTER to continue installation, all other will return to main menu: 
      if /i not "%INPUT%" == "" GOTO MAIN_MENU
      COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
    :END_SHOW_CONFIG_WAIT
  :END_SHOW_CONFIG
  
  :INSTALL_SERVICE
    "%INST_SERVICE_APP_PATH%" "%APP_NAME%" "%SERVICE_APP_PATH%"
    echo.
    echo Service installed : %APP_NAME%
    echo.
  :END_INSTALL_SERVICE
  
  :UPDATE_REGISTRY
    REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "Application" /t REG_SZ /d "%APP_PATH%" /f
    IF NOT "%APP_FOLDER%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppDirectory" /t REG_SZ /d "%APP_FOLDER%" /f
    IF NOT "%APP_PARAM%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppParameters" /t REG_SZ /d "%APP_PARAM%" /f
    echo.
    echo Registry updated to run %APP_PATH% with service %APP_NAME%
    echo.
  :END_UPDATE_REGISTRY
  
  :START_SERVICE
    SET RETURN_DRAW_ATTENTION_POINT=START_SERVICE_INPUT_REQUEST
    GOTO DRAW_ATTENTION
    :START_SERVICE_INPUT_REQUEST
      echo The service is now installed successfully (if no errors are logged above!)
      set INPUT=
      set /p INPUT=To start the service now, press Y. All others will return to main menu: 
      if /i not "%INPUT%" == "y" goto END_START_SERVICE
    :END_START_SERVICE_INPUT_REQUEST
    NET START "%APP_NAME%"
    echo.
    echo Service %APP_NAME% started
  :END_START_SERVICE
  
  GOTO END_START_INSTALL
  
  :DEFAULT_INST_SERVICE_APP_PATH
    echo Using default path for instsrv.exe %INST_SERVICE_APP_PATH%
    SET INST_SERVICE_APP_PATH=%CD%\instsrv.exe
    GOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT
  :END_DEFAULT_INST_SERVICE_APP_PATH
  
  :DEFAULT_SERVICE_APP_PATH
    echo Using default path for srvany.exe %SERVICE_APP_PATH%
    SET SERVICE_APP_PATH=%CD%\srvany.exe
    GOTO END_CHECK_SERVICE_APP_PATH_INPUT
  :END_DEFAULT_SERVICE_APP_PATH
:END_START_INSTALL
  GOTO MAIN_MENU
:START_SERVICES
  start "Services" services.msc
:END_START_SERVICES
  GOTO MAIN_MENU
:START_UNINSTALL
  :INTERACTIVE_INPUT_APP_NAME_UNINSTALL
    echo.
    SET APP_NAME=
    set /p APP_NAME=Provide the name of your custom application (the name of the service to uninstall): 
    echo. 
  :END_INTERACTIVE_INPUT_APP_NAME_UNINSTALL
    
  :INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
    echo. 
    SET INST_SERVICE_APP_PATH=
    set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe): 
  :END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
  
  :CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
    if "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
  :END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
  
  :CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    IF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    echo.
    echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!
    echo Please try again!
    SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
    GOTO DRAW_ERROR_ATTENTION
  :END_CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  :STOP_SERVICE
    NET STOP %APP_NAME%
    echo.
    echo Service %APP_NAME% stopped
    echo.
  :END_STOP_SERVICE
  
  :REMOVE_SERVICE
    "%INST_SERVICE_APP_PATH%" %APP_NAME% REMOVE
    echo.
    echo Service %APP_NAME% removed
    echo.
  :END_REMOVE_SERVICE
  
  :UPDATE_REGISTRY_UNINSTALL
    REG DELETE HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\ /va /f
    echo.
    echo Registry cleaned up for service %APP_NAME%
    echo.
  :END_UPDATE_REGISTRY_UNINSTALL
  
  GOTO END_START_UNINSTALL
  
  :DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
    echo Using default path for instsrv.exe %INST_SERVICE_APP_PATH%
    SET INST_SERVICE_APP_PATH=%CD%\instsrv.exe
    GOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
  :END_DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
:END_START_UNINSTALL
  GOTO MAIN_MENU
  
:EXIT_WITH_PAUSE
  echo The end. Comments and updates: http://myTselection.blogspot.com, Copyright MightyMouse. Copyright srvany and instsrv Microsoft.
  pause
  GOTO EXIT
:END_EXIT_WITH_PAUSE
  
:EXIT
  exit
:END_EXIT
:DRAW_ATTENTION
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
:END_DRAW_ATTENTION
  GOTO %RETURN_DRAW_ATTENTION_POINT%
:DRAW_ERROR_ATTENTION
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
:END_ERROR_DRAW_ATTENTION
  GOTO %RETURN_DRAW_ERROR_ATTENTION_POINT%
@echo on


A full package to easily install your own custom application as a service can be downloaded here, sources (zip).



Update (26/04/09): Added possibility to specify application working directory and command line parameters. (AppDirectory and AppParameter registry keys.)

Update (09/10/2011): New links

Friday, March 27, 2009

Windows File Association fixes

On Windows it can happen that some (system) files can not be opened anymore with their default build in application. Such can happen for example after a failed installation of some software application. To easily fix the default file associations, one could use the registry files provided on the site of Doug Knox (for Windows XP).

He provides registry files to fix following file types:

If your EXE file associations are corrupted, it can be difficult to open REGEDIT, or to even import REG files.  To work around this, press CTRL-ALT-DEL and open Task Manager.  Once there, click File, then hold down the CTRL key and click New Task (Run).  This will open a Command Prompt window.  Enter REGEDIT.EXE and press Enter.

Monday, February 23, 2009

My "Windows Explorer extensions" selection

While Using Windows XP (Pro), I installed a lot of extra extensions, to tweak it and make it work as I prefer. Below, one can find a selection of my preferred Windows Explorer tweaks:
  • Copy Path: provides extra options to copy the path of a selected file
  • QTTabBar: provides a tabbed interface within Windows Explorer
  • FindTarget: adds a context menu option when right clicking on a shortcut file and allows you to open the folder were the real file resides (batch file .bat)
  • Pretty Print: adds a context menu option when right clicking on an .xml file and allows you to easily PrettyPrint the file (apply correct indentations, see previous post in this blog)
  • ResHack: adds a context menu option when right clicking on a .exe file and allows you to easily open the file with ResHacker. This little tool can be used to change/extract resource used by the executable file.
  • Grep this folder, Tail this file: extra context menu options to easily grep within all files in a folder (and subfolders), and context menu option to easily tail the selected file (see previous post in this blog)
  • Unlocker: application that can detect when a file is locked, and allows you to release the lock so one can delete/move/rename/overwrite/... the file or release your external disk/stick.
  • New folder button: extra button in the explorer toolbar (and F12) to easily create a new folder within the current folder (press shift + enter to open the folder immediately after creating)
    NOT needed for Windows 7: press Ctrl+Shift+N
  • Open command window here: extra context menu option when right clicking on a file or a folder to easily open a command (cmd) windows at the current folder. (batch file .bat)
  • Aero cursor: better looking mouse cursor, the same as used in Windows Vista
  • Junctions: junctions / symbolic links / hard links context menu. Most programs see junctions as standard folders, while these are links to folders at other locations.
Download all required files for all these extensions at once here.

Update 27/03/2009: Easy-setup exe files create for aero cursor and copy path. Small registry mistake solved in ‘Open command window here’ script. Updated package with all files.

Update 12/03/2010: Updated to Dropbox links.

Update 12/05/2012: New copy path tool, Junctions added.

Thursday, February 19, 2009

Easy PrettyPrint XML files


Xml files are much more readable, when they are 'Pretty Printed', using correct indentation.
I made a little tool which will provide a new context menu when right-clicking on an xml file: 'Pretty Print'. When clicking on it, all xml indentation will be set correctly and the original file will be overwritten with a pretty printed version of it.

Just run this setup to install this little add-on, tested in Windows XP. One can check what's inside the setup with WinRar, since you should never trust an exe coming from the net.

Update 16/02/2012: dropbox link to setup file.

Windows XP Theme color during login / logout

Using Windows XP, I prefer the "Silver" or "Olive" windows theme colors, since I get sick of the strong blue colors used by default. Changing the theme is very easy for normal situations (right click desktop -> 'Properties' -> 'Appearance' -> 'Color scheme').
But during login and logout of Windows XP, the default colors are still used.

Using the Windows PowerToy TweakUI, one is able to easily copy some configuration out of the current desktop configuration into the default desktop configuration used during login (like settings for screensaver, mouse, etc) but the Color scheme is not copied with the PowerToy.
To easily change this setting, one can open it's registry editor and change the values manually:
Start -> 'Run' -> type 'regedit' -> 'Ok' -> Navigate to the following key: 'HKEY_CURRENT_USER\ Software\ Microsoft\ Windows\ CurrentVersion\ ThemeManager' -> Double click on the 'ColorName' element -> change the value into 'Metallic' (for Silver theme colors) or 'HomeStead' (for Olive green theme colors).