| 1 |
// Copyright (c) 2011-2016 Ryan Prichard |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to |
| 5 |
// deal in the Software without restriction, including without limitation the |
| 6 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 |
// IN THE SOFTWARE. |
| 20 |
|
| 21 |
#include "AgentLocation.h" |
| 22 |
|
| 23 |
#include <windows.h> |
| 24 |
|
| 25 |
#include <string> |
| 26 |
|
| 27 |
#include "../shared/WinptyAssert.h" |
| 28 |
|
| 29 |
#include "LibWinptyException.h" |
| 30 |
|
| 31 |
#define AGENT_EXE L"winpty-agent.exe" |
| 32 |
|
| 33 |
static HMODULE getCurrentModule() { |
| 34 |
HMODULE module; |
| 35 |
if (!GetModuleHandleExW( |
| 36 |
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 37 |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 38 |
reinterpret_cast<LPCWSTR>(getCurrentModule), |
| 39 |
&module)) { |
| 40 |
ASSERT(false && "GetModuleHandleEx failed"); |
| 41 |
} |
| 42 |
return module; |
| 43 |
} |
| 44 |
|
| 45 |
static std::wstring getModuleFileName(HMODULE module) { |
| 46 |
const int bufsize = 4096; |
| 47 |
wchar_t path[bufsize]; |
| 48 |
int size = GetModuleFileNameW(module, path, bufsize); |
| 49 |
ASSERT(size != 0 && size != bufsize); |
| 50 |
return std::wstring(path); |
| 51 |
} |
| 52 |
|
| 53 |
static std::wstring dirname(const std::wstring &path) { |
| 54 |
std::wstring::size_type pos = path.find_last_of(L"\\/"); |
| 55 |
if (pos == std::wstring::npos) { |
| 56 |
return L""; |
| 57 |
} else { |
| 58 |
return path.substr(0, pos); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static bool pathExists(const std::wstring &path) { |
| 63 |
return GetFileAttributesW(path.c_str()) != 0xFFFFFFFF; |
| 64 |
} |
| 65 |
|
| 66 |
std::wstring findAgentProgram() { |
| 67 |
std::wstring progDir = dirname(getModuleFileName(getCurrentModule())); |
| 68 |
std::wstring ret = progDir + (L"\\" AGENT_EXE); |
| 69 |
if (!pathExists(ret)) { |
| 70 |
throw LibWinptyException( |
| 71 |
WINPTY_ERROR_AGENT_EXE_MISSING, |
| 72 |
(L"agent executable does not exist: '" + ret + L"'").c_str()); |
| 73 |
} |
| 74 |
return ret; |
| 75 |
} |