From 7fe31126d121e810dd93641ae6582e9ab1dac16e Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Mon, 4 Oct 2021 11:59:26 -0400 Subject: Added ability to specify a working directory on opening a terminal. Released memory on calls using the ANSI creation function. --- RadTerminal.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'RadTerminal.cpp') diff --git a/RadTerminal.cpp b/RadTerminal.cpp index 5be8fce..5dae48e 100644 --- a/RadTerminal.cpp +++ b/RadTerminal.cpp @@ -122,6 +122,7 @@ struct RadTerminalCreate COORD szCon; int sb; std::tstring strCommand; + std::tstring workDirectory; }; void LoadRegistry(RadTerminalCreate& rtc, LPCWSTR strSubKey) @@ -193,6 +194,7 @@ RadTerminalCreate GetTerminalCreate(bool bParseCmdLine, std::tstring profile) rtc.sb = 1000; //rtc.strCommand = _T("%COMSPEC%"); rtc.strCommand = _T("cmd"); + rtc.workDirectory = _T(""); LoadRegistry(rtc, _T("Default")); LoadRegistry(rtc, profile.c_str()); @@ -205,7 +207,7 @@ RadTerminalCreate GetTerminalCreate(bool bParseCmdLine, std::tstring profile) #ifdef BUILD_AS_DLL extern "C" { - __declspec(dllexport) HWND WINAPI CreateTerminalWindowExW(HINSTANCE hInstance, HWND hParent, LPWSTR fontFace, int fontSize, BOOL darkMode) + __declspec(dllexport) HWND WINAPI CreateTerminalWindowExW(HINSTANCE hInstance, HWND hParent, LPWSTR command, LPWSTR working_directory, LPWSTR fontFace, int fontSize, BOOL darkMode) { HWND hRet; @@ -217,6 +219,12 @@ extern "C" { if (fontFace != NULL) rtc.strFontFace = std::tstring(fontFace); + if (command != NULL) + rtc.strCommand = std::tstring(command); + + if (working_directory != NULL) + rtc.workDirectory = std::tstring(working_directory); + hRet = CreateWindowEx( WS_EX_ACCEPTFILES, MAKEINTATOM(GetRadTerminalAtom(hInstance)), @@ -238,10 +246,10 @@ extern "C" { return hRet; } - __declspec(dllexport) HWND WINAPI CreateTerminalWindowExA(HINSTANCE hInstance, HWND hParent, LPCSTR fontFace, int fontSize, BOOL darkMode) + __declspec(dllexport) HWND WINAPI CreateTerminalWindowExA(HINSTANCE hInstance, HWND hParent, LPCSTR command, LPCSTR working_directory, LPCSTR fontFace, int fontSize, BOOL darkMode) { HWND hRet; - LPWSTR fontFaceW; + LPWSTR fontFaceW, commandW, workDirW; fontFaceW = NULL; if (fontFace != NULL) { @@ -252,12 +260,41 @@ extern "C" { } } - return CreateTerminalWindowExW(hInstance, hParent, fontFaceW, fontSize, darkMode); + commandW = NULL; + if (command != NULL) { + int fflen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, command, strlen(command), NULL, 0); + commandW = (LPWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (fflen + 1) * sizeof(WCHAR)); + if (commandW != NULL) { + MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, command, strlen(command), commandW, fflen + 1); + } + } + + workDirW = NULL; + if (working_directory != NULL) { + int fflen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, working_directory, strlen(working_directory), NULL, 0); + workDirW = (LPWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (fflen + 1) * sizeof(WCHAR)); + if (workDirW != NULL) { + MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, working_directory, strlen(working_directory), workDirW, fflen + 1); + } + } + + hRet = CreateTerminalWindowExW(hInstance, hParent, commandW, workDirW, fontFaceW, fontSize, darkMode); + + if (fontFaceW != NULL) + HeapFree(GetProcessHeap(), 0, fontFaceW); + + if (commandW != NULL) + HeapFree(GetProcessHeap(), 0, commandW); + + if (workDirW != NULL) + HeapFree(GetProcessHeap(), 0, workDirW); + + return hRet; } __declspec(dllexport) HWND WINAPI CreateTerminalWindow(HINSTANCE hInstance, HWND hParent, BOOL darkMode) { - return CreateTerminalWindowExW(hInstance, hParent, NULL, -1, darkMode); + return CreateTerminalWindowExW(hInstance, hParent, NULL, NULL, NULL, -1, darkMode); } } /* extern "C" */ @@ -870,7 +907,7 @@ BOOL RadTerminalWindowOnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct) ZeroMemory(data, sizeof(RadTerminalData)); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); - data->spd = CreateSubProcess(rtc->strCommand.c_str(), rtc->szCon, true); + data->spd = CreateSubProcess(rtc->strCommand.c_str(), rtc->workDirectory.length() > 0 ? rtc->workDirectory.c_str() : nullptr, rtc->szCon, true); if (data->spd.hr != S_OK) { ShowError(hWnd, _T("CreateSubProcess"), data->spd.hr); -- cgit v1.2.3