As this link suggests, I want replace Notepad.exe
with Notepad2.exe
using "Image File Execution Options" function by run the command
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe"
/v "Debugger" /t REG_SZ /d "\"c:\windows\Notepad2.exe\" /z" /f
开发者_StackOverflowBut when I run notepad it still opens the file
c:\windows\notepad.exe
in notepad2.exe as a text file by default.
Is there a way to avoid that?
I know using this tech Notepad.exe will as the first param passed to Notepad2.exe. but I don't know how to avoid this :(
The purpose of the "debugger" key is to automatically launch a debugger and pass the original commandline to the desired debugger. It also sets a flag on the win32 function CreateProcess that indicates this is a debugging session.
It is implied that the debugger will then call CreateProcess after modifying the arguments appropriately.
>notepad.exe "\document1.txt"
turns into
>mydebugger.exe notepad.exe "\document1.txt"
mydebugger could then call something like this:
BOOL res = CreateProcess( NULL, L"notepad.exe \"\\document1.txt\", NULL, NULL,
FALSE, cFlags, env, NULL, startupInfo, procInfo&);
So the solution to abusing this registry key is to make the fake debugger that can manipulate the commandline the way you desire. It should be a simple process that just parses the commandline and replaces the notepad.exe with notepad2.exe. Then you need to point the registry to that .exe
Notepad Replacer uses this technique, but makes it extremely easy to use and works with editors that are not debugger-aware.
For Notepad++, in order to workaround "notepad.exe" being brought in as a document create a simple launcher batch file (see @Ben's answer for why), and then point the image debugger at that (a la @fenster's answer).
nppLauncher.bat:
if exist "C:\Program Files\Notepad++\notepad++.exe" start "" "C:\Program Files\Notepad++\notepad++.exe" %2 %3 %4 %5 %6 %7 %8 %9
if exist "C:\Program Files (x86)\Notepad++\notepad++.exe" start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" %2 %3 %4 %5 %6 %7 %8 %9
Add to registry:
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v "Debugger" /t REG_SZ /d "\"...\path\to\nppLauncher.bat"" /f
From Here Replacing Windows Notepad with Notepad2 4.1.24 (or newer)
As of version 4.1.24, the official release of Notepad2 supports this method for replacing Windows Notepad, so the steps outlined above will work fine. However, there's no support to perform the Notepad replacement automatically, as the official release of Notepad2 will not modify the system registry. For the same reason, there's no support for accessing recent files through the Windows 7 jump lists, by default (this requires registration of applications in the system registry, first).
Also be aware that automated Notepad replacement could have undesirable effects if Notepad2 was used as a Notepad replacement from a portable device, and the original state was not restored when disconnecting the device.
A batch script to run from the Notepad2 directory and replace Windows Notepad might look like this (requires elevated privileges):
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v "Debugger" /t REG_SZ /d "\"%~dp0Notepad2.exe\" /z" /f
The Windows Notepad can be restored with this command (requires elevated privileges):
reg delete "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /f
Verify the result by opening Regedit and looking at [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe]
. The Debugger key should contain the full path to notepad2.exe and include the trailing /z
, e.g.:
c:\local\bin\Notepad2.exe /z
I use the following batch file to open emacs instead of notepad. I've also included a parameter for bypassing it so that I can still use notepad when I want to. e.g. editing the hosts file from an elevated command prompt:
notepad --NOTEPAD C:\Windows\System32\drivers\etc\hosts
Put this somewhere on path and edit the last line to open your text editor:
@echo off
setlocal enabledelayedexpansion
::Sometimes you really want to use notepad (e.g. edit hosts file from an elevated command prompt)
if "_%~2_" == "_--NOTEPAD_" (
set "REALLY_NOTEPAD=TRUE"
)
:: For testing calling notepad again without removing debugger registry key
:: making sure there's not an infinite loop!
if "_%~2_" == "_STOPSTOPSTOP_" (
start "" echo FAILED TO START NOTEPAD 2
exit /b 1
)
set "NOTEPAD=%~1"
:: SHIFT command will not affect the value of %* which holds all the original arguments %1 %2 %3
:: So need to remove notepad from "%*" all params variable
:: Prepending --START-- in case %NOTEPAD% appears in params
:: Quotes as NOTEPAD will /sometimes/ be in them in all params
set "ALL=%*"
set "ALLPARAMS=--START--%*"
set "ALLPARAMS=!ALLPARAMS:--START--"%NOTEPAD%"=--START--!"
set "ALLPARAMS=!ALLPARAMS:--START--%NOTEPAD%=--START--!"
:: Also remove /multiple/ spaces between NOTEPAD and parameters
set "ALLPARAMS=!ALLPARAMS:--START-- =--START--!"
set "ALLPARAMS=!ALLPARAMS:--START-- =--START--!"
set "ALLPARAMS=!ALLPARAMS:--START-- =!"
:: Also remove --NOTEPAD if present
set "ALLPARAMS=!ALLPARAMS:--START----NOTEPAD=--START--!"
set "ALLPARAMS=!ALLPARAMS:--START-- =--START--!"
set "ALLPARAMS=!ALLPARAMS:--START-- =--START--!"
set "ALLPARAMS=!ALLPARAMS:--START-- =!"
:: and --START--, just in case
set "ALLPARAMS=!ALLPARAMS:--START--=!"
IF "_%ALLPARAMS%_" == "__" (
:: Open a scratch buffer in emacs if no params
set "ALLPARAMS=**SCRATCH**"
)
:: Unicode support (I hope)
chcp 65001
::TEST not removing debugger registry key
::if "_%REALLY_NOTEPAD%_" == "_TRUE_" start "" notepad STOPSTOPSTOP && exit /b
if "_%REALLY_NOTEPAD%_" == "_TRUE_" (
reg DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v "Debugger" /F
start "" notepad !ALLPARAMS!
reg ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v "Debugger" /t REG_SZ /d "%~0"
exit /b 0
)
C:\path\to\emacs\bin\emacsclientw.exe -na C:\path\to\emacs\bin\runemacs.exe "!ALLPARAMS!"
set it to replace notepad using this command:
reg ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v "Debugger" /t REG_SZ /d "c:\path\to\wherever\you\savced\script-above.cmd"
or just call the script like this:
SCRIPT JUNK --NOTEPAD
which should invoke notepad, but replace it with this script thereafter
精彩评论