2012-04-15 16:54:38 +00:00
// This file is part of Notepad++ project
2020-04-04 21:03:18 +00:00
// Copyright (C)2020 Don HO <don.h@free.fr>
2009-04-24 23:34:47 +00:00
//
2012-04-15 16:54:38 +00:00
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
2009-04-24 23:34:47 +00:00
//
2012-04-15 16:54:38 +00:00
// Note that the GPL places important restrictions on "derived works", yet
2015-08-04 11:36:22 +00:00
// it does not provide a detailed definition of that term. To avoid
// misunderstandings, we consider an application to constitute a
2012-04-15 16:54:38 +00:00
// "derivative work" for the purpose of this license if it does any of the
2015-08-04 11:36:22 +00:00
// following:
2012-04-15 16:54:38 +00:00
// 1. Integrates source code from Notepad++.
// 2. Integrates/includes/aggregates Notepad++ into a proprietary executable
// installer, such as those produced by InstallShield.
// 3. Links to a library or executes a program that does any of the above.
2009-04-24 23:34:47 +00:00
//
2012-04-15 16:54:38 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2010-03-26 00:22:14 +00:00
# include "Notepad_plus_Window.h"
2016-01-23 01:24:37 +00:00
# include "Processus.h"
2009-04-24 23:34:47 +00:00
# include "Win32Exception.h" //Win32 exception
# include "MiniDumper.h" //Write dump files
2016-03-20 01:25:19 +00:00
# include "verifySignedfile.h"
2009-04-24 23:34:47 +00:00
2017-07-27 10:44:06 +00:00
typedef std : : vector < generic_string > ParamVector ;
2009-04-24 23:34:47 +00:00
2012-11-16 00:28:00 +00:00
2017-03-08 16:53:57 +00:00
namespace
{
2019-08-14 20:13:24 +00:00
void allowWmCopydataMessages ( Notepad_plus_Window & notepad_plus_plus , const NppParameters & nppParameters , winVer ver )
2017-03-08 16:53:57 +00:00
{
# ifndef MSGFLT_ADD
const DWORD MSGFLT_ADD = 1 ;
# endif
# ifndef MSGFLT_ALLOW
const DWORD MSGFLT_ALLOW = 1 ;
# endif
// Tell UAC that lower integrity processes are allowed to send WM_COPYDATA messages to this process (or window)
// This allows opening new files to already opened elevated Notepad++ process via explorer context menu.
if ( ver > = WV_VISTA | | ver = = WV_UNKNOWN )
{
HMODULE hDll = GetModuleHandle ( TEXT ( " user32.dll " ) ) ;
if ( hDll )
{
// According to MSDN ChangeWindowMessageFilter may not be supported in future versions of Windows,
// that is why we use ChangeWindowMessageFilterEx if it is available (windows version >= Win7).
2019-08-14 20:13:24 +00:00
if ( nppParameters . getWinVersion ( ) = = WV_VISTA )
2017-03-08 16:53:57 +00:00
{
typedef BOOL ( WINAPI * MESSAGEFILTERFUNC ) ( UINT message , DWORD dwFlag ) ;
MESSAGEFILTERFUNC func = ( MESSAGEFILTERFUNC ) : : GetProcAddress ( hDll , " ChangeWindowMessageFilter " ) ;
if ( func )
func ( WM_COPYDATA , MSGFLT_ADD ) ;
}
else
{
typedef BOOL ( WINAPI * MESSAGEFILTERFUNCEX ) ( HWND hWnd , UINT message , DWORD action , VOID * pChangeFilterStruct ) ;
MESSAGEFILTERFUNCEX func = ( MESSAGEFILTERFUNCEX ) : : GetProcAddress ( hDll , " ChangeWindowMessageFilterEx " ) ;
if ( func )
func ( notepad_plus_plus . getHSelf ( ) , WM_COPYDATA , MSGFLT_ALLOW , NULL ) ;
}
}
}
}
2009-04-24 23:34:47 +00:00
//commandLine should contain path to n++ executable running
2018-07-02 15:24:39 +00:00
ParamVector parseCommandLine ( const TCHAR * commandLine )
2017-07-15 12:53:37 +00:00
{
2018-07-02 15:24:39 +00:00
ParamVector result ;
2019-07-04 21:18:25 +00:00
if ( commandLine [ 0 ] ! = ' \0 ' )
2017-07-23 08:10:26 +00:00
{
2019-07-04 21:18:25 +00:00
int numArgs ;
LPWSTR * tokenizedCmdLine = CommandLineToArgvW ( commandLine , & numArgs ) ;
if ( tokenizedCmdLine ! = nullptr )
2018-07-02 15:41:59 +00:00
{
2019-07-04 21:18:25 +00:00
result . assign ( tokenizedCmdLine , tokenizedCmdLine + numArgs ) ;
LocalFree ( tokenizedCmdLine ) ;
2018-07-02 15:41:59 +00:00
}
}
2019-07-04 21:18:25 +00:00
return result ;
2018-07-02 15:41:59 +00:00
}
2018-10-08 20:49:37 +00:00
// 1. Converts /p to -quickPrint if it exists as the first parameter
// 2. Concatenates all remaining parameters to form a file path, adding appending .txt extension if necessary
// This seems to mirror Notepad's behaviour
2019-07-04 21:18:25 +00:00
ParamVector convertParamsToNotepadStyle ( PWSTR pCmdLine )
2018-10-08 20:49:37 +00:00
{
2019-07-04 21:18:25 +00:00
ParamVector params ;
if ( _tcsnicmp ( TEXT ( " /p " ) , pCmdLine , 2 ) = = 0 ) // Notepad accepts both /p and /P, so compare case insensitively
2018-10-08 20:49:37 +00:00
{
2019-07-04 21:18:25 +00:00
params . emplace_back ( TEXT ( " -quickPrint " ) ) ;
pCmdLine + = 2 ; // Length of "/p"
2018-10-08 20:49:37 +00:00
}
2019-07-04 21:18:25 +00:00
// Advance to the first non-whitespace character
while ( iswspace ( * pCmdLine ) )
2018-10-08 20:49:37 +00:00
{
2019-07-04 21:18:25 +00:00
+ + pCmdLine ;
2018-10-08 20:49:37 +00:00
}
2019-07-04 21:18:25 +00:00
// Now form a file name from the remaining commandline (if any is left)
if ( * pCmdLine ! = ' \0 ' )
2018-10-08 20:49:37 +00:00
{
2019-07-04 21:18:25 +00:00
generic_string str ( pCmdLine ) ;
2018-10-08 20:49:37 +00:00
if ( * PathFindExtension ( str . c_str ( ) ) = = ' \0 ' )
{
str . append ( TEXT ( " .txt " ) ) ; // If joined path has no extension, Notepad adds a .txt extension
}
2019-07-04 21:18:25 +00:00
params . push_back ( std : : move ( str ) ) ;
2018-10-08 20:49:37 +00:00
}
2019-07-04 21:18:25 +00:00
return params ;
2018-10-08 20:49:37 +00:00
}
2018-07-02 15:24:39 +00:00
bool isInList ( const TCHAR * token2Find , ParamVector & params , bool eraseArg = true )
2016-06-05 18:29:21 +00:00
{
2018-07-02 15:24:39 +00:00
for ( auto it = params . begin ( ) ; it ! = params . end ( ) ; + + it )
2009-04-24 23:34:47 +00:00
{
2018-07-02 15:24:39 +00:00
if ( lstrcmp ( token2Find , it - > c_str ( ) ) = = 0 )
2016-06-05 18:29:21 +00:00
{
2018-07-02 15:24:39 +00:00
if ( eraseArg ) params . erase ( it ) ;
2009-04-24 23:34:47 +00:00
return true ;
}
}
return false ;
} ;
2015-02-01 12:16:12 +00:00
bool getParamVal ( TCHAR c , ParamVector & params , generic_string & value )
{
2009-04-24 23:34:47 +00:00
value = TEXT ( " " ) ;
2017-07-27 08:30:53 +00:00
size_t nbItems = params . size ( ) ;
2009-04-24 23:34:47 +00:00
2017-07-27 08:30:53 +00:00
for ( size_t i = 0 ; i < nbItems ; + + i )
2009-04-24 23:34:47 +00:00
{
2017-07-27 10:44:06 +00:00
const TCHAR * token = params . at ( i ) . c_str ( ) ;
2020-01-28 01:20:44 +00:00
if ( token [ 0 ] = = ' - ' & & lstrlen ( token ) > = 2 & & token [ 1 ] = = c ) //dash, and enough chars
{
2009-04-24 23:34:47 +00:00
value = ( token + 2 ) ;
params . erase ( params . begin ( ) + i ) ;
return true ;
}
}
return false ;
}
2015-02-01 12:16:12 +00:00
bool getParamValFromString ( const TCHAR * str , ParamVector & params , generic_string & value )
{
value = TEXT ( " " ) ;
2017-07-27 08:30:53 +00:00
size_t nbItems = params . size ( ) ;
2015-02-01 12:16:12 +00:00
2017-07-27 08:30:53 +00:00
for ( size_t i = 0 ; i < nbItems ; + + i )
2015-02-01 12:16:12 +00:00
{
2017-07-27 10:44:06 +00:00
const TCHAR * token = params . at ( i ) . c_str ( ) ;
2015-02-01 12:16:12 +00:00
generic_string tokenStr = token ;
2016-06-05 18:29:21 +00:00
size_t pos = tokenStr . find ( str ) ;
if ( pos ! = generic_string : : npos & & pos = = 0 )
2015-02-01 12:16:12 +00:00
{
value = ( token + lstrlen ( str ) ) ;
params . erase ( params . begin ( ) + i ) ;
return true ;
}
}
return false ;
}
2013-07-25 17:41:25 +00:00
LangType getLangTypeFromParam ( ParamVector & params )
{
2009-04-24 23:34:47 +00:00
generic_string langStr ;
if ( ! getParamVal ( ' l ' , params , langStr ) )
return L_EXTERNAL ;
return NppParameters : : getLangIDFromStr ( langStr . c_str ( ) ) ;
2013-07-25 17:41:25 +00:00
}
generic_string getLocalizationPathFromParam ( ParamVector & params )
{
generic_string locStr ;
if ( ! getParamVal ( ' L ' , params , locStr ) )
return TEXT ( " " ) ;
return NppParameters : : getLocPathFromStr ( locStr . c_str ( ) ) ;
}
2009-04-24 23:34:47 +00:00
2020-01-28 01:20:44 +00:00
int getNumberFromParam ( char paramName , ParamVector & params , bool & isParamePresent )
{
2009-04-24 23:34:47 +00:00
generic_string numStr ;
if ( ! getParamVal ( paramName , params , numStr ) )
{
isParamePresent = false ;
return - 1 ;
}
isParamePresent = true ;
return generic_atoi ( numStr . c_str ( ) ) ;
} ;
2015-02-01 12:16:12 +00:00
generic_string getEasterEggNameFromParam ( ParamVector & params , unsigned char & type )
2015-01-10 23:41:49 +00:00
{
generic_string EasterEggName ;
2015-02-01 12:16:12 +00:00
if ( ! getParamValFromString ( TEXT ( " -qn " ) , params , EasterEggName ) ) // get internal easter egg
{
if ( ! getParamValFromString ( TEXT ( " -qt " ) , params , EasterEggName ) ) // get user quote from cmdline argument
{
if ( ! getParamValFromString ( TEXT ( " -qf " ) , params , EasterEggName ) ) // get user quote from a content of file
return TEXT ( " " ) ;
else
{
EasterEggName = relativeFilePathToFullFilePath ( EasterEggName . c_str ( ) ) ;
type = 2 ; // quote content in file
}
}
else
type = 1 ; // commandline quote
}
else
type = 0 ; // easter egg
generic_string percentTwentyStr = TEXT ( " %20 " ) ;
generic_string spaceStr = TEXT ( " " ) ;
size_t start_pos = 0 ;
while ( ( start_pos = EasterEggName . find ( percentTwentyStr , start_pos ) ) ! = std : : string : : npos )
{
EasterEggName . replace ( start_pos , percentTwentyStr . length ( ) , spaceStr ) ;
start_pos + = spaceStr . length ( ) ; // Handles case where 'to' is a substring of 'from'
}
2015-01-10 23:41:49 +00:00
return EasterEggName ;
}
2009-04-24 23:34:47 +00:00
2018-03-10 10:30:55 +00:00
int getGhostTypingSpeedFromParam ( ParamVector & params )
{
generic_string speedStr ;
if ( ! getParamValFromString ( TEXT ( " -qSpeed " ) , params , speedStr ) )
return - 1 ;
int speed = std : : stoi ( speedStr , 0 ) ;
if ( speed < = 0 | | speed > 3 )
return - 1 ;
return speed ;
}
2009-04-24 23:34:47 +00:00
const TCHAR FLAG_MULTI_INSTANCE [ ] = TEXT ( " -multiInst " ) ;
const TCHAR FLAG_NO_PLUGIN [ ] = TEXT ( " -noPlugin " ) ;
const TCHAR FLAG_READONLY [ ] = TEXT ( " -ro " ) ;
const TCHAR FLAG_NOSESSION [ ] = TEXT ( " -nosession " ) ;
const TCHAR FLAG_NOTABBAR [ ] = TEXT ( " -notabbar " ) ;
2009-11-11 23:55:18 +00:00
const TCHAR FLAG_SYSTRAY [ ] = TEXT ( " -systemtray " ) ;
2010-02-08 00:44:48 +00:00
const TCHAR FLAG_LOADINGTIME [ ] = TEXT ( " -loadingTime " ) ;
2009-11-11 23:55:18 +00:00
const TCHAR FLAG_HELP [ ] = TEXT ( " --help " ) ;
2010-11-14 01:40:33 +00:00
const TCHAR FLAG_ALWAYS_ON_TOP [ ] = TEXT ( " -alwaysOnTop " ) ;
2013-07-17 22:35:34 +00:00
const TCHAR FLAG_OPENSESSIONFILE [ ] = TEXT ( " -openSession " ) ;
2014-03-05 21:18:05 +00:00
const TCHAR FLAG_RECURSIVE [ ] = TEXT ( " -r " ) ;
2017-08-05 22:03:18 +00:00
const TCHAR FLAG_FUNCLSTEXPORT [ ] = TEXT ( " -export=functionList " ) ;
2017-08-06 22:01:12 +00:00
const TCHAR FLAG_PRINTANDQUIT [ ] = TEXT ( " -quickPrint " ) ;
2018-10-08 20:49:37 +00:00
const TCHAR FLAG_NOTEPAD_COMPATIBILITY [ ] = TEXT ( " -notepadStyleCmdline " ) ;
2019-06-25 11:37:48 +00:00
const TCHAR FLAG_OPEN_FOLDERS_AS_WORKSPACE [ ] = TEXT ( " -openFoldersAsWorkspace " ) ;
2015-08-04 11:36:22 +00:00
2017-03-08 16:53:57 +00:00
void doException ( Notepad_plus_Window & notepad_plus_plus )
2015-08-04 11:36:22 +00:00
{
Win32Exception : : removeHandler ( ) ; //disable exception handler after excpetion, we dont want corrupt data structurs to crash the exception handler
: : MessageBox ( Notepad_plus_Window : : gNppHWND , TEXT ( " Notepad++ will attempt to save any unsaved data. However, dataloss is very likely. " ) , TEXT ( " Recovery initiating " ) , MB_OK | MB_ICONINFORMATION ) ;
TCHAR tmpDir [ 1024 ] ;
GetTempPath ( 1024 , tmpDir ) ;
generic_string emergencySavedDir = tmpDir ;
emergencySavedDir + = TEXT ( " \\ N++RECOV " ) ;
bool res = notepad_plus_plus . emergency ( emergencySavedDir ) ;
if ( res )
{
generic_string displayText = TEXT ( " Notepad++ was able to successfully recover some unsaved documents, or nothing to be saved could be found. \r \n You can find the results at : \r \n " ) ;
displayText + = emergencySavedDir ;
: : MessageBox ( Notepad_plus_Window : : gNppHWND , displayText . c_str ( ) , TEXT ( " Recovery success " ) , MB_OK | MB_ICONINFORMATION ) ;
}
else
: : MessageBox ( Notepad_plus_Window : : gNppHWND , TEXT ( " Unfortunatly, Notepad++ was not able to save your work. We are sorry for any lost data. " ) , TEXT ( " Recovery failure " ) , MB_OK | MB_ICONERROR ) ;
}
2019-07-04 21:18:25 +00:00
PWSTR advanceCmdLine ( PWSTR pCmdLine , const generic_string & string )
{
const size_t len = string . length ( ) ;
while ( true )
{
PWSTR ignoredString = wcsstr ( pCmdLine , string . c_str ( ) ) ;
if ( ignoredString = = nullptr )
{
// Should never happen - tokenized parameters contain string somewhere, so it HAS to match
// This is there just in case
break ;
}
// Match the substring only if it matched an entire substring
2019-10-13 07:19:22 +00:00
if ( ( ignoredString = = pCmdLine | | iswspace ( * ( ignoredString - 1 ) ) ) & & // Check start
( iswspace ( * ( ignoredString + len ) ) | | * ( ignoredString + len ) = = ' \0 ' | | * ( ignoredString + len ) = = ' " ' ) )
2019-07-04 21:18:25 +00:00
{
ignoredString + = len ;
// Advance to the first non-whitespace and not quotation mark character
while ( iswspace ( * ignoredString ) | | * ignoredString = = L ' " ' )
{
+ + ignoredString ;
}
pCmdLine = ignoredString ;
break ;
}
else
{
pCmdLine = ignoredString + len ; // Just skip this match and resume from another
}
}
return pCmdLine ;
}
// Looks for -z arguments and strips command line arguments following those, if any
// Also advances pCmdLine to point after the last ignored parameter
// -notepadStyleCmdline is also considered an ignored parameter here, as we don't want it to be part of the assembled file name
PWSTR stripIgnoredParams ( ParamVector & params , PWSTR pCmdLine )
{
for ( auto it = params . begin ( ) ; it ! = params . end ( ) ; )
{
if ( lstrcmp ( it - > c_str ( ) , TEXT ( " -z " ) ) = = 0 )
{
pCmdLine = advanceCmdLine ( pCmdLine , * it ) ;
auto nextIt = std : : next ( it ) ;
if ( nextIt ! = params . end ( ) )
{
pCmdLine = advanceCmdLine ( pCmdLine , * nextIt ) ;
params . erase ( nextIt ) ;
}
it = params . erase ( it ) ;
}
else if ( lstrcmp ( it - > c_str ( ) , FLAG_NOTEPAD_COMPATIBILITY ) = = 0 )
{
pCmdLine = advanceCmdLine ( pCmdLine , * it + + ) ;
}
else
{
+ + it ;
}
}
return pCmdLine ;
}
2015-08-04 11:36:22 +00:00
2017-03-08 16:53:57 +00:00
} // namespace
2015-08-04 11:36:22 +00:00
2009-04-24 23:34:47 +00:00
2019-07-04 21:18:25 +00:00
int WINAPI wWinMain ( HINSTANCE hInstance , HINSTANCE , PWSTR pCmdLine , int )
2009-04-24 23:34:47 +00:00
{
2019-07-04 21:18:25 +00:00
ParamVector params = parseCommandLine ( pCmdLine ) ;
PWSTR pCmdLineWithoutIgnores = stripIgnoredParams ( params , pCmdLine ) ;
2009-04-24 23:34:47 +00:00
MiniDumper mdump ; //for debugging purposes.
bool TheFirstOne = true ;
: : SetLastError ( NO_ERROR ) ;
: : CreateMutex ( NULL , false , TEXT ( " nppInstance " ) ) ;
if ( : : GetLastError ( ) = = ERROR_ALREADY_EXISTS )
TheFirstOne = false ;
2018-10-08 20:49:37 +00:00
// Convert commandline to notepad-compatible format, if applicable
if ( isInList ( FLAG_NOTEPAD_COMPATIBILITY , params ) )
{
2019-07-04 21:18:25 +00:00
params = convertParamsToNotepadStyle ( pCmdLineWithoutIgnores ) ;
2018-10-08 20:49:37 +00:00
}
2009-04-24 23:34:47 +00:00
bool isParamePresent ;
2009-11-11 23:55:18 +00:00
bool showHelp = isInList ( FLAG_HELP , params ) ;
2009-04-24 23:34:47 +00:00
bool isMultiInst = isInList ( FLAG_MULTI_INSTANCE , params ) ;
2017-08-05 22:03:18 +00:00
bool doFunctionListExport = isInList ( FLAG_FUNCLSTEXPORT , params ) ;
2017-08-06 22:01:12 +00:00
bool doPrintAndQuit = isInList ( FLAG_PRINTANDQUIT , params ) ;
2015-08-04 11:36:22 +00:00
2009-11-11 23:55:18 +00:00
CmdLineParams cmdLineParams ;
2009-04-24 23:34:47 +00:00
cmdLineParams . _isNoTab = isInList ( FLAG_NOTABBAR , params ) ;
cmdLineParams . _isNoPlugin = isInList ( FLAG_NO_PLUGIN , params ) ;
cmdLineParams . _isReadOnly = isInList ( FLAG_READONLY , params ) ;
cmdLineParams . _isNoSession = isInList ( FLAG_NOSESSION , params ) ;
2009-11-11 23:55:18 +00:00
cmdLineParams . _isPreLaunch = isInList ( FLAG_SYSTRAY , params ) ;
2010-11-14 01:40:33 +00:00
cmdLineParams . _alwaysOnTop = isInList ( FLAG_ALWAYS_ON_TOP , params ) ;
2010-02-08 00:44:48 +00:00
cmdLineParams . _showLoadingTime = isInList ( FLAG_LOADINGTIME , params ) ;
2013-07-17 22:35:34 +00:00
cmdLineParams . _isSessionFile = isInList ( FLAG_OPENSESSIONFILE , params ) ;
2014-03-05 21:18:05 +00:00
cmdLineParams . _isRecursive = isInList ( FLAG_RECURSIVE , params ) ;
2019-06-25 11:37:48 +00:00
cmdLineParams . _openFoldersAsWorkspace = isInList ( FLAG_OPEN_FOLDERS_AS_WORKSPACE , params ) ;
2009-04-24 23:34:47 +00:00
cmdLineParams . _langType = getLangTypeFromParam ( params ) ;
2013-07-25 17:41:25 +00:00
cmdLineParams . _localizationPath = getLocalizationPathFromParam ( params ) ;
2017-08-06 22:01:12 +00:00
cmdLineParams . _easterEggName = getEasterEggNameFromParam ( params , cmdLineParams . _quoteType ) ;
2018-03-10 10:30:55 +00:00
cmdLineParams . _ghostTypingSpeed = getGhostTypingSpeedFromParam ( params ) ;
2017-08-06 22:01:12 +00:00
// getNumberFromParam should be run at the end, to not consuming the other params
2009-04-24 23:34:47 +00:00
cmdLineParams . _line2go = getNumberFromParam ( ' n ' , params , isParamePresent ) ;
cmdLineParams . _column2go = getNumberFromParam ( ' c ' , params , isParamePresent ) ;
2016-04-19 18:00:55 +00:00
cmdLineParams . _pos2go = getNumberFromParam ( ' p ' , params , isParamePresent ) ;
2009-04-24 23:34:47 +00:00
cmdLineParams . _point . x = getNumberFromParam ( ' x ' , params , cmdLineParams . _isPointXValid ) ;
cmdLineParams . _point . y = getNumberFromParam ( ' y ' , params , cmdLineParams . _isPointYValid ) ;
2015-08-04 11:36:22 +00:00
2009-11-11 23:55:18 +00:00
if ( showHelp )
: : MessageBox ( NULL , COMMAND_ARG_HELP , TEXT ( " Notepad++ Command Argument Help " ) , MB_OK ) ;
2019-08-14 20:13:24 +00:00
NppParameters & nppParameters = NppParameters : : getInstance ( ) ;
2020-02-15 03:52:46 +00:00
if ( cmdLineParams . _localizationPath ! = TEXT ( " " ) )
{
// setStartWithLocFileName() should be called before parameters are loaded
nppParameters . setStartWithLocFileName ( cmdLineParams . _localizationPath ) ;
}
2020-02-14 00:33:16 +00:00
nppParameters . load ( ) ;
2019-08-14 20:13:24 +00:00
NppGUI & nppGui = const_cast < NppGUI & > ( nppParameters . getNppGUI ( ) ) ;
2020-02-14 00:33:16 +00:00
2019-08-13 21:51:05 +00:00
bool doUpdateNpp = nppGui . _autoUpdateOpt . _doAutoUpdate ;
bool doUpdatePluginList = nppGui . _autoUpdateOpt . _doAutoUpdate ;
2017-08-05 22:03:18 +00:00
2017-08-06 22:01:12 +00:00
if ( doFunctionListExport | | doPrintAndQuit ) // export functionlist feature will serialize fuctionlist on the disk, then exit Notepad++. So it's important to not launch into existing instance, and keep it silent.
2017-08-05 22:03:18 +00:00
{
isMultiInst = true ;
2019-08-13 21:51:05 +00:00
doUpdateNpp = doUpdatePluginList = false ;
2017-08-05 22:03:18 +00:00
cmdLineParams . _isNoSession = true ;
}
2015-08-04 11:36:22 +00:00
2019-08-14 20:13:24 +00:00
nppParameters . setFunctionListExportBoolean ( doFunctionListExport ) ;
nppParameters . setPrintAndExitBoolean ( doPrintAndQuit ) ;
2017-08-05 22:03:18 +00:00
2009-04-24 23:34:47 +00:00
// override the settings if notepad style is present
2019-08-14 20:13:24 +00:00
if ( nppParameters . asNotepadStyle ( ) )
2009-04-24 23:34:47 +00:00
{
isMultiInst = true ;
cmdLineParams . _isNoTab = true ;
cmdLineParams . _isNoSession = true ;
}
2013-07-17 22:35:34 +00:00
// override the settings if multiInst is choosen by user in the preference dialog
2019-08-14 20:13:24 +00:00
const NppGUI & nppGUI = nppParameters . getNppGUI ( ) ;
2013-07-17 22:35:34 +00:00
if ( nppGUI . _multiInstSetting = = multiInst )
{
isMultiInst = true ;
// Only the first launch remembers the session
if ( ! TheFirstOne )
cmdLineParams . _isNoSession = true ;
}
2009-04-24 23:34:47 +00:00
generic_string quotFileName = TEXT ( " " ) ;
// tell the running instance the FULL path to the new files to load
2017-07-27 08:30:53 +00:00
size_t nbFilesToOpen = params . size ( ) ;
2009-04-24 23:34:47 +00:00
2017-07-27 08:30:53 +00:00
for ( size_t i = 0 ; i < nbFilesToOpen ; + + i )
2009-04-24 23:34:47 +00:00
{
2017-07-27 10:44:06 +00:00
const TCHAR * currentFile = params . at ( i ) . c_str ( ) ;
2012-05-01 12:20:18 +00:00
if ( currentFile [ 0 ] )
2009-04-24 23:34:47 +00:00
{
2012-05-01 12:20:18 +00:00
//check if relative or full path. Relative paths dont have a colon for driveletter
2015-08-04 11:36:22 +00:00
2012-05-01 12:20:18 +00:00
quotFileName + = TEXT ( " \" " ) ;
2015-02-01 12:16:12 +00:00
quotFileName + = relativeFilePathToFullFilePath ( currentFile ) ;
2012-05-01 12:20:18 +00:00
quotFileName + = TEXT ( " \" " ) ;
2009-04-24 23:34:47 +00:00
}
}
//Only after loading all the file paths set the working directory
2019-08-14 20:13:24 +00:00
: : SetCurrentDirectory ( NppParameters : : getInstance ( ) . getNppPath ( ) . c_str ( ) ) ; //force working directory to path of module, preventing lock
2009-04-24 23:34:47 +00:00
if ( ( ! isMultiInst ) & & ( ! TheFirstOne ) )
{
2010-03-26 00:22:14 +00:00
HWND hNotepad_plus = : : FindWindow ( Notepad_plus_Window : : getClassName ( ) , NULL ) ;
2013-07-08 00:12:50 +00:00
for ( int i = 0 ; ! hNotepad_plus & & i < 5 ; + + i )
2009-07-04 11:33:17 +00:00
{
2009-04-24 23:34:47 +00:00
Sleep ( 100 ) ;
2010-03-26 00:22:14 +00:00
hNotepad_plus = : : FindWindow ( Notepad_plus_Window : : getClassName ( ) , NULL ) ;
2009-07-04 11:33:17 +00:00
}
2009-04-24 23:34:47 +00:00
if ( hNotepad_plus )
{
2017-07-27 08:30:53 +00:00
// First of all, destroy static object NppParameters
2019-08-14 20:13:24 +00:00
nppParameters . destroyInstance ( ) ;
2009-04-24 23:34:47 +00:00
2017-07-27 08:30:53 +00:00
int sw = 0 ;
2009-04-24 23:34:47 +00:00
2017-07-27 08:30:53 +00:00
if ( : : IsZoomed ( hNotepad_plus ) )
sw = SW_MAXIMIZE ;
else if ( : : IsIconic ( hNotepad_plus ) )
sw = SW_RESTORE ;
2013-08-06 10:11:49 +00:00
2017-07-27 08:30:53 +00:00
if ( sw ! = 0 )
: : ShowWindow ( hNotepad_plus , sw ) ;
2017-02-04 12:17:40 +00:00
2017-07-27 08:30:53 +00:00
: : SetForegroundWindow ( hNotepad_plus ) ;
2009-04-24 23:34:47 +00:00
2017-07-27 08:30:53 +00:00
if ( params . size ( ) > 0 ) //if there are files to open, use the WM_COPYDATA system
{
2018-07-02 18:48:40 +00:00
CmdLineParamsDTO dto = CmdLineParamsDTO : : FromCmdLineParams ( cmdLineParams ) ;
2017-07-27 08:30:53 +00:00
COPYDATASTRUCT paramData ;
paramData . dwData = COPYDATA_PARAMS ;
2018-07-02 18:48:40 +00:00
paramData . lpData = & dto ;
paramData . cbData = sizeof ( dto ) ;
2017-07-27 08:30:53 +00:00
COPYDATASTRUCT fileNamesData ;
fileNamesData . dwData = COPYDATA_FILENAMES ;
fileNamesData . lpData = ( void * ) quotFileName . c_str ( ) ;
fileNamesData . cbData = long ( quotFileName . length ( ) + 1 ) * ( sizeof ( TCHAR ) ) ;
: : SendMessage ( hNotepad_plus , WM_COPYDATA , reinterpret_cast < WPARAM > ( hInstance ) , reinterpret_cast < LPARAM > ( & paramData ) ) ;
: : SendMessage ( hNotepad_plus , WM_COPYDATA , reinterpret_cast < WPARAM > ( hInstance ) , reinterpret_cast < LPARAM > ( & fileNamesData ) ) ;
}
return 0 ;
2009-04-24 23:34:47 +00:00
}
}
2010-03-26 00:22:14 +00:00
Notepad_plus_Window notepad_plus_plus ;
2015-08-04 11:36:22 +00:00
2019-08-14 20:13:24 +00:00
generic_string updaterDir = nppParameters . getNppPath ( ) ;
2009-04-24 23:34:47 +00:00
updaterDir + = TEXT ( " \\ updater \\ " ) ;
generic_string updaterFullPath = updaterDir + TEXT ( " gup.exe " ) ;
2015-08-04 11:36:22 +00:00
2016-09-19 23:01:40 +00:00
generic_string updaterParams = TEXT ( " -v " ) ;
updaterParams + = VERSION_VALUE ;
2009-04-24 23:34:47 +00:00
bool isUpExist = nppGui . _doesExistUpdater = ( : : PathFileExists ( updaterFullPath . c_str ( ) ) = = TRUE ) ;
2012-11-05 23:13:40 +00:00
2019-08-13 21:51:05 +00:00
if ( doUpdateNpp ) // check more detail
2009-08-12 01:13:29 +00:00
{
Date today ( 0 ) ;
2015-08-04 11:36:22 +00:00
2009-08-12 01:13:29 +00:00
if ( today < nppGui . _autoUpdateOpt . _nextUpdateDate )
2019-08-13 21:51:05 +00:00
doUpdateNpp = false ;
2009-08-12 01:13:29 +00:00
}
2009-04-24 23:34:47 +00:00
2019-08-13 21:51:05 +00:00
if ( doUpdatePluginList )
{
// TODO: detect update frequency
}
2015-05-22 19:11:08 +00:00
// wingup doesn't work with the obsolet security layer (API) under xp since downloadings are secured with SSL on notepad_plus_plus.org
2019-08-14 20:13:24 +00:00
winVer ver = nppParameters . getWinVersion ( ) ;
2015-05-22 19:11:08 +00:00
bool isGtXP = ver > WV_XP ;
2019-01-17 23:40:01 +00:00
2019-02-27 09:14:36 +00:00
SecurityGard securityGard ;
bool isSignatureOK = securityGard . checkModule ( updaterFullPath , nm_gup ) ;
2019-01-17 23:40:01 +00:00
2019-08-13 21:51:05 +00:00
if ( TheFirstOne & & isUpExist & & isGtXP & & isSignatureOK )
2009-04-24 23:34:47 +00:00
{
2019-08-14 20:13:24 +00:00
if ( nppParameters . isx64 ( ) )
2016-09-19 23:01:40 +00:00
{
updaterParams + = TEXT ( " -px64 " ) ;
}
2019-01-17 23:40:01 +00:00
2019-08-13 21:51:05 +00:00
if ( doUpdateNpp )
{
Process updater ( updaterFullPath . c_str ( ) , updaterParams . c_str ( ) , updaterDir . c_str ( ) ) ;
updater . run ( ) ;
// Update next update date
if ( nppGui . _autoUpdateOpt . _intervalDays < 0 ) // Make sure interval days value is positive
nppGui . _autoUpdateOpt . _intervalDays = 0 - nppGui . _autoUpdateOpt . _intervalDays ;
nppGui . _autoUpdateOpt . _nextUpdateDate = Date ( nppGui . _autoUpdateOpt . _intervalDays ) ;
}
// to be removed
doUpdatePluginList = false ;
2015-08-04 11:36:22 +00:00
2019-08-13 21:51:05 +00:00
if ( doUpdatePluginList )
{
// Update Plugin List
2019-10-13 19:48:27 +00:00
generic_string upPlParams = TEXT ( " -v " ) ;
upPlParams + = notepad_plus_plus . getPluginListVerStr ( ) ;
if ( nppParameters . isx64 ( ) )
{
upPlParams + = TEXT ( " -px64 " ) ;
}
upPlParams + = TEXT ( " -upZip " ) ;
2019-08-13 21:51:05 +00:00
// overrided "InfoUrl" in gup.xml
2019-10-13 19:48:27 +00:00
upPlParams + = TEXT ( " https://notepad-plus-plus.org/update/pluginListDownloadUrl.php " ) ;
2019-08-13 21:51:05 +00:00
2019-10-13 19:48:27 +00:00
// indicate the pluginList installation location
upPlParams + = nppParameters . getPluginConfDir ( ) ;
Process updater ( updaterFullPath . c_str ( ) , upPlParams . c_str ( ) , updaterDir . c_str ( ) ) ;
2019-08-13 21:51:05 +00:00
updater . run ( ) ;
// TODO: Update next update date
}
2009-04-24 23:34:47 +00:00
}
MSG msg ;
msg . wParam = 0 ;
Win32Exception : : installHandler ( ) ;
2015-08-04 11:36:22 +00:00
try
{
2009-04-24 23:34:47 +00:00
notepad_plus_plus . init ( hInstance , NULL , quotFileName . c_str ( ) , & cmdLineParams ) ;
2019-08-14 20:13:24 +00:00
allowWmCopydataMessages ( notepad_plus_plus , nppParameters , ver ) ;
2009-04-24 23:34:47 +00:00
bool going = true ;
while ( going )
{
2014-05-10 01:12:44 +00:00
going = : : GetMessageW ( & msg , NULL , 0 , 0 ) ! = 0 ;
2009-04-24 23:34:47 +00:00
if ( going )
{
// if the message doesn't belong to the notepad_plus_plus's dialog
2014-05-10 01:12:44 +00:00
if ( ! notepad_plus_plus . isDlgsMsg ( & msg ) )
2009-04-24 23:34:47 +00:00
{
if ( : : TranslateAccelerator ( notepad_plus_plus . getHSelf ( ) , notepad_plus_plus . getAccTable ( ) , & msg ) = = 0 )
{
: : TranslateMessage ( & msg ) ;
2014-05-10 01:12:44 +00:00
: : DispatchMessageW ( & msg ) ;
2010-03-26 00:22:14 +00:00
}
2009-04-24 23:34:47 +00:00
}
}
}
2015-08-04 11:36:22 +00:00
}
catch ( int i )
{
2010-05-25 23:41:58 +00:00
TCHAR str [ 50 ] = TEXT ( " God Damned Exception : " ) ;
TCHAR code [ 10 ] ;
wsprintf ( code , TEXT ( " %d " ) , i ) ;
2019-02-22 11:57:58 +00:00
wcscat_s ( str , code ) ;
: : MessageBox ( Notepad_plus_Window : : gNppHWND , str , TEXT ( " Int Exception " ) , MB_OK ) ;
2010-05-25 23:41:58 +00:00
doException ( notepad_plus_plus ) ;
2015-08-04 11:36:22 +00:00
}
catch ( std : : runtime_error & ex )
{
2010-05-25 23:41:58 +00:00
: : MessageBoxA ( Notepad_plus_Window : : gNppHWND , ex . what ( ) , " Runtime Exception " , MB_OK ) ;
doException ( notepad_plus_plus ) ;
2015-08-04 11:36:22 +00:00
}
catch ( const Win32Exception & ex )
{
2009-04-24 23:34:47 +00:00
TCHAR message [ 1024 ] ; //TODO: sane number
wsprintf ( message , TEXT ( " An exception occured. Notepad++ cannot recover and must be shut down. \r \n The exception details are as follows: \r \n " )
2018-06-02 23:55:56 +00:00
TEXT ( " Code: \t 0x%08X \r \n Type: \t %S \r \n Exception address: 0x%p " ) , ex . code ( ) , ex . what ( ) , ex . where ( ) ) ;
2010-03-26 00:22:14 +00:00
: : MessageBox ( Notepad_plus_Window : : gNppHWND , message , TEXT ( " Win32Exception " ) , MB_OK | MB_ICONERROR ) ;
2009-04-24 23:34:47 +00:00
mdump . writeDump ( ex . info ( ) ) ;
doException ( notepad_plus_plus ) ;
2015-08-04 11:36:22 +00:00
}
catch ( std : : exception & ex )
{
2010-05-25 23:41:58 +00:00
: : MessageBoxA ( Notepad_plus_Window : : gNppHWND , ex . what ( ) , " General Exception " , MB_OK ) ;
2009-04-24 23:34:47 +00:00
doException ( notepad_plus_plus ) ;
2015-08-04 11:36:22 +00:00
}
catch ( . . . ) // this shouldnt ever have to happen
{
2010-05-25 23:41:58 +00:00
: : MessageBoxA ( Notepad_plus_Window : : gNppHWND , " An exception that we did not yet found its name is just caught " , " Unknown Exception " , MB_OK ) ;
2009-04-24 23:34:47 +00:00
doException ( notepad_plus_plus ) ;
}
2016-08-11 20:20:30 +00:00
return static_cast < int > ( msg . wParam ) ;
2017-03-08 16:53:57 +00:00
}