diff --git a/src/libwinpty/AgentLocation.cc b/src/libwinpty/AgentLocation.cc new file mode 100755 index 0000000..0bdb942 --- /dev/null +++ b/src/libwinpty/AgentLocation.cc @@ -0,0 +1,69 @@ +// Copyright (c) 2011-2016 Ryan Prichard +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include "AgentLocation.h" + +#include + +#include + +#include "../shared/WinptyAssert.h" + +#define AGENT_EXE L"winpty-agent.exe" + +static HMODULE getCurrentModule() { + HMODULE module; + if (!GetModuleHandleExW( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(getCurrentModule), + &module)) { + ASSERT(false && "GetModuleHandleEx failed"); + } + return module; +} + +static std::wstring getModuleFileName(HMODULE module) { + const int bufsize = 4096; + wchar_t path[bufsize]; + int size = GetModuleFileNameW(module, path, bufsize); + ASSERT(size != 0 && size != bufsize); + return std::wstring(path); +} + +static std::wstring dirname(const std::wstring &path) { + std::wstring::size_type pos = path.find_last_of(L"\\/"); + if (pos == std::wstring::npos) { + return L""; + } else { + return path.substr(0, pos); + } +} + +static bool pathExists(const std::wstring &path) { + return GetFileAttributesW(path.c_str()) != 0xFFFFFFFF; +} + +std::wstring findAgentProgram() { + std::wstring progDir = dirname(getModuleFileName(getCurrentModule())); + std::wstring ret = progDir + (L"\\" AGENT_EXE); + ASSERT(pathExists(ret)); + return ret; +} diff --git a/src/libwinpty/AgentLocation.h b/src/libwinpty/AgentLocation.h new file mode 100755 index 0000000..a96b854 --- /dev/null +++ b/src/libwinpty/AgentLocation.h @@ -0,0 +1,28 @@ +// Copyright (c) 2011-2016 Ryan Prichard +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#ifndef LIBWINPTY_AGENT_LOCATION_H +#define LIBWINPTY_AGENT_LOCATION_H + +#include + +std::wstring findAgentProgram(); + +#endif // LIBWINPTY_AGENT_LOCATION_H diff --git a/src/libwinpty/subdir.mk b/src/libwinpty/subdir.mk index 5dfaf09..af0e425 100644 --- a/src/libwinpty/subdir.mk +++ b/src/libwinpty/subdir.mk @@ -23,6 +23,7 @@ ALL_TARGETS += build/winpty.dll $(eval $(call def_mingw_target,libwinpty,-DCOMPILING_WINPTY_DLL)) LIBWINPTY_OBJECTS = \ + build/libwinpty/libwinpty/AgentLocation.o \ build/libwinpty/libwinpty/winpty.o \ build/libwinpty/shared/Buffer.o \ build/libwinpty/shared/DebugClient.o \ diff --git a/src/libwinpty/winpty.cc b/src/libwinpty/winpty.cc index d32ae25..c7555f0 100644 --- a/src/libwinpty/winpty.cc +++ b/src/libwinpty/winpty.cc @@ -43,9 +43,9 @@ #include "../shared/WinptyException.h" #include "../shared/WinptyVersion.h" -// TODO: Error handling, handle out-of-memory. +#include "AgentLocation.h" -#define AGENT_EXE L"winpty-agent.exe" +// TODO: Error handling, handle out-of-memory. struct winpty_s { winpty_s(); @@ -57,50 +57,6 @@ winpty_s::winpty_s() : controlPipe(NULL), dataPipe(NULL) { } -static HMODULE getCurrentModule() -{ - HMODULE module; - if (!GetModuleHandleExW( - GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | - GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - reinterpret_cast(getCurrentModule), - &module)) { - ASSERT(false && "GetModuleHandleEx failed"); - } - return module; -} - -static std::wstring getModuleFileName(HMODULE module) -{ - const int bufsize = 4096; - wchar_t path[bufsize]; - int size = GetModuleFileNameW(module, path, bufsize); - ASSERT(size != 0 && size != bufsize); - return std::wstring(path); -} - -static std::wstring dirname(const std::wstring &path) -{ - std::wstring::size_type pos = path.find_last_of(L"\\/"); - if (pos == std::wstring::npos) - return L""; - else - return path.substr(0, pos); -} - -static bool pathExists(const std::wstring &path) -{ - return GetFileAttributesW(path.c_str()) != 0xFFFFFFFF; -} - -static std::wstring findAgentProgram() -{ - std::wstring progDir = dirname(getModuleFileName(getCurrentModule())); - std::wstring ret = progDir + (L"\\" AGENT_EXE); - ASSERT(pathExists(ret)); - return ret; -} - // Call ConnectNamedPipe and block, even for an overlapped pipe. If the // pipe is overlapped, create a temporary event for use connecting. static bool connectNamedPipe(HANDLE handle, bool overlapped) diff --git a/src/winpty.gyp b/src/winpty.gyp index 4f3beca..ac31197 100644 --- a/src/winpty.gyp +++ b/src/winpty.gyp @@ -122,6 +122,8 @@ }, 'sources' : [ 'include/winpty.h', + 'libwinpty/AgentLocation.cc', + 'libwinpty/AgentLocation.h', 'libwinpty/winpty.cc', 'shared/AgentMsg.h', 'shared/Buffer.h',