Fixed a crash when trying to launch a secondary instance with string commandline arguments

Fix #4621, close #4622
This commit is contained in:
Silent 2018-07-02 20:48:40 +02:00 committed by Don HO
parent c4d4428847
commit 388e874bfc
5 changed files with 51 additions and 12 deletions

View File

@ -5291,7 +5291,7 @@ void Notepad_plus::notifyBufferActivated(BufferID bufid, int view)
_linkTriggered = true; _linkTriggered = true;
} }
void Notepad_plus::loadCommandlineParams(const TCHAR * commandLine, CmdLineParams * pCmdParams) void Notepad_plus::loadCommandlineParams(const TCHAR * commandLine, const CmdLineParamsDTO * pCmdParams)
{ {
if (!commandLine || ! pCmdParams) if (!commandLine || ! pCmdParams)
return; return;

View File

@ -572,7 +572,12 @@ private:
bool dumpFiles(const TCHAR * outdir, const TCHAR * fileprefix = TEXT("")); //helper func bool dumpFiles(const TCHAR * outdir, const TCHAR * fileprefix = TEXT("")); //helper func
void drawTabbarColoursFromStylerArray(); void drawTabbarColoursFromStylerArray();
void loadCommandlineParams(const TCHAR * commandLine, CmdLineParams * pCmdParams); void loadCommandlineParams(const TCHAR * commandLine, const CmdLineParams * pCmdParams)
{
const CmdLineParamsDTO dto = CmdLineParamsDTO::FromCmdLineParams(*pCmdParams);
loadCommandlineParams(commandLine, &dto);
}
void loadCommandlineParams(const TCHAR * commandLine, const CmdLineParamsDTO * pCmdParams);
bool noOpenedDoc() const; bool noOpenedDoc() const;
bool goToPreviousIndicator(int indicID2Search, bool isWrap = true) const; bool goToPreviousIndicator(int indicID2Search, bool isWrap = true) const;
bool goToNextIndicator(int indicID2Search, bool isWrap = true) const; bool goToNextIndicator(int indicID2Search, bool isWrap = true) const;

View File

@ -558,9 +558,9 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
{ {
case COPYDATA_PARAMS: case COPYDATA_PARAMS:
{ {
CmdLineParams *cmdLineParam = static_cast<CmdLineParams *>(pCopyData->lpData); // CmdLineParams object from another instance const CmdLineParamsDTO *cmdLineParam = static_cast<const CmdLineParamsDTO *>(pCopyData->lpData); // CmdLineParams object from another instance
auto cmdLineParamsSize = static_cast<size_t>(pCopyData->cbData); // CmdLineParams size from another instance const DWORD cmdLineParamsSize = pCopyData->cbData; // CmdLineParams size from another instance
if (sizeof(CmdLineParams) == cmdLineParamsSize) // make sure the structure is the same if (sizeof(CmdLineParamsDTO) == cmdLineParamsSize) // make sure the structure is the same
{ {
pNppParam->setCmdlineParam(*cmdLineParam); pNppParam->setCmdlineParam(*cmdLineParam);
} }
@ -579,7 +579,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
case COPYDATA_FILENAMESA: case COPYDATA_FILENAMESA:
{ {
char *fileNamesA = static_cast<char *>(pCopyData->lpData); char *fileNamesA = static_cast<char *>(pCopyData->lpData);
CmdLineParams & cmdLineParams = pNppParam->getCmdLineParams(); const CmdLineParamsDTO & cmdLineParams = pNppParam->getCmdLineParams();
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
const wchar_t *fileNamesW = wmc->char2wchar(fileNamesA, CP_ACP); const wchar_t *fileNamesW = wmc->char2wchar(fileNamesA, CP_ACP);
loadCommandlineParams(fileNamesW, &cmdLineParams); loadCommandlineParams(fileNamesW, &cmdLineParams);
@ -589,7 +589,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
case COPYDATA_FILENAMESW: case COPYDATA_FILENAMESW:
{ {
wchar_t *fileNamesW = static_cast<wchar_t *>(pCopyData->lpData); wchar_t *fileNamesW = static_cast<wchar_t *>(pCopyData->lpData);
CmdLineParams & cmdLineParams = pNppParam->getCmdLineParams(); const CmdLineParamsDTO & cmdLineParams = pNppParam->getCmdLineParams();
loadCommandlineParams(fileNamesW, &cmdLineParams); loadCommandlineParams(fileNamesW, &cmdLineParams);
break; break;
} }

View File

@ -235,6 +235,38 @@ struct CmdLineParams
} }
}; };
// A POD class to send CmdLineParams through WM_COPYDATA and to Notepad_plus::loadCommandlineParams
struct CmdLineParamsDTO
{
bool _isReadOnly;
bool _isNoSession;
bool _isSessionFile;
bool _isRecursive;
int _line2go;
int _column2go;
int _pos2go;
LangType _langType;
static CmdLineParamsDTO FromCmdLineParams(const CmdLineParams& params)
{
CmdLineParamsDTO dto;
dto._isReadOnly = params._isReadOnly;
dto._isNoSession = params._isNoSession;
dto._isSessionFile = params._isSessionFile;
dto._isRecursive = params._isRecursive;
dto._line2go = params._line2go;
dto._column2go = params._column2go;
dto._pos2go = params._pos2go;
dto._langType = params._langType;
return dto;
}
};
struct FloatingWindowInfo struct FloatingWindowInfo
{ {
int _cont; int _cont;
@ -1423,11 +1455,11 @@ public:
void removeTransparent(HWND hwnd); void removeTransparent(HWND hwnd);
void setCmdlineParam(const CmdLineParams & cmdLineParams) void setCmdlineParam(const CmdLineParamsDTO & cmdLineParams)
{ {
_cmdLineParams = cmdLineParams; _cmdLineParams = cmdLineParams;
} }
CmdLineParams & getCmdLineParams() {return _cmdLineParams;}; const CmdLineParamsDTO & getCmdLineParams() const {return _cmdLineParams;};
void setFileSaveDlgFilterIndex(int ln) {_fileSaveDlgFilterIndex = ln;}; void setFileSaveDlgFilterIndex(int ln) {_fileSaveDlgFilterIndex = ln;};
int getFileSaveDlgFilterIndex() const {return _fileSaveDlgFilterIndex;}; int getFileSaveDlgFilterIndex() const {return _fileSaveDlgFilterIndex;};
@ -1639,7 +1671,7 @@ private:
ExternalLangContainer *_externalLangArray[NB_MAX_EXTERNAL_LANG]; ExternalLangContainer *_externalLangArray[NB_MAX_EXTERNAL_LANG];
int _nbExternalLang = 0; int _nbExternalLang = 0;
CmdLineParams _cmdLineParams; CmdLineParamsDTO _cmdLineParams;
int _fileSaveDlgFilterIndex = -1; int _fileSaveDlgFilterIndex = -1;

View File

@ -483,10 +483,12 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
if (params.size() > 0) //if there are files to open, use the WM_COPYDATA system if (params.size() > 0) //if there are files to open, use the WM_COPYDATA system
{ {
CmdLineParamsDTO dto = CmdLineParamsDTO::FromCmdLineParams(cmdLineParams);
COPYDATASTRUCT paramData; COPYDATASTRUCT paramData;
paramData.dwData = COPYDATA_PARAMS; paramData.dwData = COPYDATA_PARAMS;
paramData.lpData = &cmdLineParams; paramData.lpData = &dto;
paramData.cbData = sizeof(cmdLineParams); paramData.cbData = sizeof(dto);
COPYDATASTRUCT fileNamesData; COPYDATASTRUCT fileNamesData;
fileNamesData.dwData = COPYDATA_FILENAMES; fileNamesData.dwData = COPYDATA_FILENAMES;