Skip to content

Commit

Permalink
Allow shim's dirname in path and args #1
Browse files Browse the repository at this point in the history
Co-Authored-By: felipecrs <felipecassiors@gmail.com>
  • Loading branch information
kiennq and felipecrs committed Mar 6, 2024
1 parent e0fb559 commit af19af4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compile_flags.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-std=c++17
-std=c++20
37 changes: 32 additions & 5 deletions shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#define ERROR_ELEVATION_REQUIRED 740
#endif

using namespace std::string_view_literals;

BOOL WINAPI CtrlHandler(DWORD ctrlType)
{
switch (ctrlType)
Expand Down Expand Up @@ -52,7 +54,31 @@ namespace std
typedef optional<wstring> wstring_p;
}

std::tuple<std::wstring_p, std::wstring_p> GetShimInfo()
struct ShimInfo
{
std::wstring_p path;
std::wstring_p args;
};

std::wstring_view GetDirectory(std::wstring_view exe)
{
auto pos = exe.find_last_of(L"\\/");
return exe.substr(0, pos);
}

std::wstring_p NormalizeArgs(std::wstring_p& args, std::wstring_view curDir)
{
static constexpr auto s_dirPlaceHolder = L"%~dp0"sv;
if (!args)
{
return args;
}

args->replace(args->find(s_dirPlaceHolder), s_dirPlaceHolder.size(), curDir.data(), curDir.size());
return args;
}

ShimInfo GetShimInfo()
{
// Find filename of current executable.
wchar_t filename[MAX_PATH + 2];
Expand Down Expand Up @@ -109,7 +135,7 @@ std::tuple<std::wstring_p, std::wstring_p> GetShimInfo()
{
path.emplace(line_substr.data(), line_substr.size() - (line.back() == L'\n' ? 1 : 0));
}

continue;
}

Expand All @@ -120,15 +146,16 @@ std::tuple<std::wstring_p, std::wstring_p> GetShimInfo()
}
}

return {path, args};
return {path, NormalizeArgs(args, GetDirectory(filename))};
}

std::tuple<std::unique_handle, std::unique_handle> MakeProcess(const std::wstring_p& path, const std::wstring_p& args)
std::tuple<std::unique_handle, std::unique_handle> MakeProcess(ShimInfo const& info)
{
// Start subprocess
STARTUPINFOW si = {};
PROCESS_INFORMATION pi = {};

auto&& [path, args] = info;
std::vector<wchar_t> cmd(path->size() + args->size() + 2);
wmemcpy(cmd.data(), path->c_str(), path->size());
cmd[path->size()] = L' ';
Expand Down Expand Up @@ -244,7 +271,7 @@ int wmain(int argc, wchar_t* argv[])
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;
SetInformationJobObject(jobHandle.get(), JobObjectExtendedLimitInformation, &jeli, sizeof(jeli));

auto [processHandle, threadHandle] = MakeProcess(std::move(path), std::move(args));
auto [processHandle, threadHandle] = MakeProcess({path, args});
if (processHandle && !isWindowsApp)
{
AssignProcessToJobObject(jobHandle.get(), processHandle.get());
Expand Down

0 comments on commit af19af4

Please sign in to comment.