Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add installer, 64-bit support, fix N++ z-positioning and refactor code.

Build in installer triggered by 'install.exe' name that tries to bypass
SFC/WFP before copying itself as 'notepad.exe' to 4 %WINDIR% directories
(\ServicePackFiles\i386, \system32\dllcache, \system32, \) by using
undocumented sfc_os.dll SfcFileException() call.

Add 64-bit support. It was feasible thanks to switching from MinGW to
MinGW-W64. You can still build the 32-bit launcher using normal MinGW,
but it's discouraged.

Fix Notepad++ z-positioning by removing SWP_ASYNCWINDOWPOS flag from the
SetWindowPos() call.

Refactor the code by using more secure string operations (length-aware).

Move the code from C++ (not really needed) to C99.
  • Loading branch information...
commit 672f4597e189da3f6d434a2fac695c1780ed7531 1 parent fbc8d80
@przemoc authored
View
16 NEWS
@@ -1,3 +1,19 @@
+* What's new in version 0.5
+
+- 64-bit Windows are supported now.
+
+- Built-in installer is available. Rename executable file to install.exe if
+ you want to use it. Installer copies .exe as notepad.exe to 4 locations:
+ - %WINDIR%\ServicePackFiles\i386\
+ - %WINDIR%\system32\dllcache\
+ - %WINDIR%\system32\
+ - %WINDIR%\
+ But before, it tries to bypass System File Checker/Windows File Protection.
+
+- Moving Notepad++ window to the top has been fixed.
+
+- Building toolchain has changed from MinGW to MinGW-W64.
+
* What's new in version 0.2
- Always move Notepad++ window to the top.
View
3  build.cmd
@@ -1,3 +0,0 @@
-@set PATH=C:\MinGW\bin;%PATH%
-windres -i notepad.rc --input-format=rc -o notepad.res -O coff
-mingw32-gcc -Wall -O2 notepad.cpp notepad.res -o notepad.exe -mwindows -s -DWAIT_FOR_EXIT=1
View
6 build32.cmd
@@ -0,0 +1,6 @@
+@set ARCH=i686
+@set PATH=%PATH%;D:\MinGW-W64-%ARCH%\bin
+@set PREFIX=%ARCH%-w64-mingw32-
+%PREFIX%windres -i notepad.rc --input-format=rc -o notepad.res -O coff
+%PREFIX%gcc -Wall -O2 -std=c99 notepad.c notepad.res -o %ARCH%/notepad.exe -mwindows -s
+%PREFIX%gcc -Wall -O2 -std=c99 notepad.c notepad.res -o %ARCH%/wait_for_exit/notepad.exe -mwindows -s -DWAIT_FOR_EXIT=1
View
6 build64.cmd
@@ -0,0 +1,6 @@
+@set ARCH=x86_64
+@set PATH=%PATH%;D:\MinGW-W64-%ARCH%\bin
+@set PREFIX=%ARCH%-w64-mingw32-
+%PREFIX%windres -i notepad.rc --input-format=rc -o notepad.res -O coff
+%PREFIX%gcc -Wall -O2 -std=c99 notepad.c notepad.res -o %ARCH%/notepad.exe -mwindows -s
+%PREFIX%gcc -Wall -O2 -std=c99 notepad.c notepad.res -o %ARCH%/wait_for_exit/notepad.exe -mwindows -s -DWAIT_FOR_EXIT=1
View
99 notepad.cpp → notepad.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2005 Stephane Chambrin <superstepho@yahoo.fr>
- * Copyright (C) 2008 Przemyslaw Pawelczyk <przemoc@gmail.com>
+ * Copyright (C) 2008-2009 Przemyslaw Pawelczyk <przemoc@gmail.com>
*
* This software is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2.
@@ -14,78 +14,107 @@
/*
* Notepad++ launcher (for Windows notepad.exe replacement)
- * version 0.2
+ * version 0.5
*/
-/*
- * Copy built notepad.exe to (in given order):
- * - C:\WINDOWS\SERVICEPACKFILES\i386\
- * - C:\WINDOWS\SYSTEM32\dllcache\
- * - C:\WINDOWS\SYSTEM32\
- * - C:\WINDOWS\
- *
- * How-to build (using MinGW):
- * windres -i notepad.rc --input-format=rc -o notepad.res -O coff
- * mingw32-gcc -O2 notepad.cpp notepad.res -o notepad.exe -mwindows -s
- */
+#define _ISOC99_SOURCE
#include <windows.h>
#include <winreg.h>
+#ifndef COPY_FILE_ALLOW_DECRYPTED_DESTINATION
+#define COPY_FILE_ALLOW_DECRYPTED_DESTINATION 0x00000008
+#endif
+
+typedef DWORD(__stdcall *SFE) (DWORD param1, PWCHAR param2, DWORD param3);
+
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
-
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
HKEY hKey;
HWND hWnd;
+ HINSTANCE hInst;
DWORD dwProcessId,
dwExitCode = 1,
dwCmdLineLen = strlen(lpCmdLine);
- char cmd[1024] = "\"C:\\Program Files\\Notepad++\\notepad++.exe\"";
+ WCHAR szPath[MAX_PATH];
+ TCHAR cmd[1024],
+#if __x86_64__
+ szNotepadKey[] = "SOFTWARE\\Wow6432Node\\Notepad++",
+ szProgFilesVar[] = "ProgramFiles(x86)";
+#else
+ szNotepadKey[] = "SOFTWARE\\Notepad++",
+ szProgFilesVar[] = "ProgramFiles";
+#endif
+
+ if (GetModuleFileNameW(NULL, szPath, MAX_PATH) && !wcsicmp(&szPath[wcslen(szPath) - 11], L"install.exe") && (hInst = LoadLibrary("sfc_os.dll"))) {
+ WCHAR *szNotepadPaths[] = {
+ L"\\ServicePackFiles\\i386\\notepad.exe",
+ L"\\system32\\dllcache\\notepad.exe",
+ L"\\system32\\notepad.exe",
+ L"\\notepad.exe",
+ L"\0"
+ };
+ WCHAR szSfc[MAX_PATH];
+ DWORD dwWinDirLen;
+
+ GetEnvironmentVariableW(L"WINDIR", szSfc, MAX_PATH);
+ dwWinDirLen = wcslen(szSfc);
+ SFE SfcFileException = (SFE)GetProcAddress(hInst, (LPCSTR) 5);
+
+ for (WCHAR **szNotepadPath = szNotepadPaths; *szNotepadPath[0] != 0; ++szNotepadPath) {
+ wcsncpy(&szSfc[dwWinDirLen], *szNotepadPath, MAX_PATH - dwWinDirLen);
+ if (SfcFileException)
+ SfcFileException(0, szSfc, (DWORD) -1);
+ CopyFileExW(szPath, szSfc, NULL, NULL, NULL, COPY_FILE_ALLOW_DECRYPTED_DESTINATION);
+ }
+
+ MessageBox(NULL, "Installation is finished! You can remove this file.", "Notepad++ Launcher", MB_ICONINFORMATION);
+ exit(0);
+ }
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
/* Getting path to Notepad++ from registry. */
- if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Notepad++", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
- {
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szNotepadKey, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
DWORD iType,
iDataSize = 1024;
unsigned char sData[1024];
- if (RegQueryValueEx(hKey, "", NULL, &iType, sData, &iDataSize) == ERROR_SUCCESS)
- {
+ if (RegQueryValueEx(hKey, "", NULL, &iType, sData, &iDataSize) == ERROR_SUCCESS) {
strcpy(cmd, "\"");
- strcat(cmd, (char*) sData);
- strcat(cmd, "\\notepad++.exe\"");
+ strncat(cmd, (TCHAR*) sData, sizeof(cmd) - 1);
+ strncat(cmd, "\\notepad++.exe\"", sizeof(cmd) - 1 - iDataSize);
}
RegCloseKey(hKey);
}
+ /* Use the default path if no info in registry. */
+ else {
+ DWORD iVarSize;
+ strcpy(cmd, "\"");
+ iVarSize = GetEnvironmentVariable(szProgFilesVar, &cmd[1], MAX_PATH - 1);
+ strncat(cmd, "\\Notepad++\\notepad++.exe\"", sizeof(cmd) - 1 - iVarSize);
+ }
/* Command-line construction. */
- if (dwCmdLineLen)
- {
- strcat(cmd, " \"");
+ if (dwCmdLineLen) {
+ strncat(cmd, " \"", sizeof(cmd) - strlen(cmd));
if (lpCmdLine[0] != '\"')
- {
- _fullpath(cmd + strlen(cmd), lpCmdLine, 1000);
- }
- else
- {
+ _fullpath(cmd + strlen(cmd), lpCmdLine, sizeof(cmd) - strlen(cmd));
+ else {
if (lpCmdLine[dwCmdLineLen - 1] == '\"')
- {
lpCmdLine[dwCmdLineLen - 1] = '\0';
- }
- _fullpath(cmd + strlen(cmd), lpCmdLine + 1, 1000);
+ _fullpath(cmd + strlen(cmd), lpCmdLine + 1, sizeof(cmd) - strlen(cmd));
}
- strcat(cmd, "\"");
+ strncat(cmd, "\"", sizeof(cmd) - strlen(cmd));
}
/* Notepad++ launching. */
@@ -116,13 +145,13 @@ int WINAPI WinMain(HINSTANCE hInstance,
GetWindowThreadProcessId(hWnd, &dwProcessId);
if (dwProcessId == pi.dwProcessId) {
/* And move it to the top. */
- SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOMOVE | SWP_NOSIZE);
+ SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
break;
}
hWnd = GetNextWindow(hWnd, GW_HWNDNEXT);
}
-#if WAIT_FOR_EXIT == 0
+#if ! WAIT_FOR_EXIT > 0
dwExitCode = 0;
#else
/* Wait until Notepad++ exits. */
Please sign in to comment.
Something went wrong with that request. Please try again.