Skip to content

Commit

Permalink
Bump 3.5.3.1, support wslsl-shell.exe path/to/your/app.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
halx99 committed Mar 13, 2024
1 parent 871332b commit bc081d6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 53 deletions.
57 changes: 39 additions & 18 deletions libwsls/libwsls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,20 @@ namespace wsls {
return error;
}

int make_bridge(const wchar_t* shell, const wchar_t* app)
static void make_cmdline(wchar_t** argv, int argc, int offset, std::wstring& cmdline) {
cmdline.clear();
for(int i = offset; i < argc; ++i) {
if (!cmdline.empty())
cmdline += ' ';
auto arg = argv[i];
if (wcschr(arg, ' '))
(cmdline += '\"').append(arg) += '\"';
else
cmdline += arg;
}
}

int make_bridge(const wchar_t* shell, const wchar_t* app, bool forward_mode)
{
auto lpCmdLine = GetCommandLineW();

Expand All @@ -395,10 +408,7 @@ namespace wsls {
wchar_t currentDir[MAX_PATH];
GetCurrentDirectoryW(MAX_PATH, currentDir);

wchar_t filePath[MAX_PATH];
GetModuleFileName(NULL, filePath, MAX_PATH);

PathRemoveFileSpec(filePath);
static wchar_t appDir[WSLS_MAX_PATH];

std::wstring maketool = lpCmdLine;

Expand All @@ -413,25 +423,36 @@ namespace wsls {
return ERROR_FILE_NOT_FOUND;
}

std::wstring param0 = wargv[0];
if (!do_replace(param0, shell, app))
{
wprintf(L"make bridge failed %s --> %s", shell, app);
return ERROR_OPERATION_ABORTED;
if (forward_mode) {
make_cmdline(wargv, argc, 1, maketool);
si.lpTitle = &maketool.front();
wcscpy(appDir, app);
}
else {
std::wstring param0 = wargv[0];
if (!do_replace(param0, shell, app))
{
wprintf(L"make bridge failed %s --> %s", shell, app);
return ERROR_OPERATION_ABORTED;
}

if (!replace_once(maketool, wargv[0], param0))
{
wprintf(L"make bridge failed %s --> %s", shell, app);
return ERROR_OPERATION_ABORTED;
if (!replace_once(maketool, wargv[0], param0))
{
wprintf(L"make bridge failed %s --> %s", shell, app);
return ERROR_OPERATION_ABORTED;
}

std::wstring title = si.lpTitle;
do_replace(title, shell, app);
si.lpTitle = &title.front();

GetModuleFileName(NULL, appDir, MAX_PATH);
}

LocalFree(wargv);

std::wstring title = si.lpTitle;
do_replace(title, shell, app);
PathRemoveFileSpec(appDir);

si.lpTitle = &title.front();
DWORD flags = CREATE_SUSPENDED;
BOOL succeed = CreateProcessW(nullptr,
&maketool.front(),
Expand Down Expand Up @@ -460,7 +481,7 @@ namespace wsls {

sei.lpFile = wow64helper.c_str();
sei.lpParameters = parameter.c_str();
sei.lpDirectory = filePath;
sei.lpDirectory = appDir;

succeed = ShellExecuteEx(&sei);

Expand Down
4 changes: 3 additions & 1 deletion libwsls/libwsls.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string_view>
#include "ntcvt.hpp"

#define WSLS_MAX_PATH 4096

namespace wsls {
using namespace ntcvt;
/*--- This a C++ universal sprintf in the future.
Expand All @@ -17,7 +19,7 @@ std::wstring sfmt(const wchar_t* format, ...);
bool replace_once(std::string& string, const std::string& replaced_key, const std::string& replacing_key, size_t offset = 0);
bool replace_once(std::wstring& string, const std::wstring& replaced_key, const std::wstring& replacing_key, size_t offset = 0);
int replace(std::wstring& string, const std::wstring& replaced_key, const std::wstring& replacing_key);
int make_bridge(const wchar_t* shell, const wchar_t* app);
int make_bridge(const wchar_t* shell, const wchar_t* app, bool forward_mode);
std::string readFileData(const char* fileName);
int writeFileData(const char* fileName, const std::string& content, bool append = false);
void convertPathToWinStyle(std::string& path, size_t offset = 0);
Expand Down
2 changes: 1 addition & 1 deletion wow64helper/wow64helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ ZwResumeProcessProc ZwResumeProcess;
int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
{
#if _WAITING_DEBUGGER_ATTACH
MessageBox(nullptr, L"waiting for debugger to attach!", L"Tips", MB_OK);
MessageBox(nullptr, L"waiting for debugger to attach!", L"wow64helper.exe", MB_OK);
#endif

int ret = -1;
Expand Down
2 changes: 1 addition & 1 deletion wsls-core/wsls-core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ void InstallHook()
auto hAppModule = GetModuleHandle(nullptr);
GetModuleFileNameW(hAppModule, appName, MAX_PATH * 2); // Kernel32.dll --> KernelBase.dll: GetModuleFileNameA --> GetModuleFileNameW
#if defined(_DEBUG)
if (wcsstr(appName, DEBUG_MODULE)) {
if (*DEBUG_MODULE && wcsstr(appName, DEBUG_MODULE)) {
MessageBoxW(nullptr, wsls::sfmt(L"Install patch: wsls-core.dll for %s succeed.", appName).c_str(), L"Wating for debugger to attaching...", MB_OK | MB_ICONEXCLAMATION);
}
#endif
Expand Down
81 changes: 49 additions & 32 deletions wsls-shell/wsls-shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,77 @@
#include "libwsls.h"

#if defined(_DEBUG)
#define WSLS_WAIT_DEBUGGER 0
#define WSLS_WAIT_DEBUGGER 1
#else
#define WSLS_WAIT_DEBUGGER 0
#endif

int main(int /*argc*/, char** /*argv*/)
int main(int argc, char** argv)
{
char fileName[4096];
auto n = GetModuleFileNameA(GetModuleHandle(nullptr), fileName, sizeof(fileName));
static char my_path[WSLS_MAX_PATH];
auto n = GetModuleFileNameA(GetModuleHandle(nullptr), my_path, sizeof(my_path));

if (n > 0) {
if (stricmp(PathFindFileNameA(fileName), "wsls-shell.exe") == 0) {
MessageBoxA(nullptr, "Please rename wsls-shell.exe to what(such as make.exe, cc1plus.exe, arm-linux-androideabi-g++.exe) do you want to bridge!", "tips", MB_OK | MB_ICONEXCLAMATION);
return ERROR_OPERATION_ABORTED;
}
std::wstring shell = wsls::from_chars(wsls::getFileShortName(my_path));
std::wstring app;

std::wstring shell = wsls::from_chars(wsls::getFileShortName(fileName));
bool forward_mode = stricmp(PathFindFileNameA(my_path), "wsls-shell.exe") == 0;
if (forward_mode)
{ // execute without replacement
if (argc < 2) {
fprintf(stderr, "Missing parameter not exist when runs wsls-shell in as-is mode\nUsage: wsls-shell.exe path/to/app.exe [args...]");
return ERROR_INVALID_PARAMETER;
}

strcpy(fileName + n, ".bridge");
std::wstring app = wsls::from_chars(wsls::readFileData(fileName));
if (app.empty()) // default is the real compile program file name is prefix 'wrl-'
app = L"wrl-" + shell;
app = ntcvt::from_chars(argv[1]);
if (!wsls::isFileExists(app.c_str())) {
fprintf(stderr, "The app %s not exist when runs wsls-shell in as-is mode\n", argv[1]);
return ERROR_NOT_FOUND;
}
}
else {
strcpy(my_path + n, ".bridge");
app = wsls::from_chars(wsls::readFileData(my_path));
if (app.empty()) // default is the real compile program file name is prefix 'wrl-'
app = L"wrl-" + shell;
}

#if WSLS_WAIT_DEBUGGER
MessageBoxW(nullptr, wsls::sfmt(L"%s --> %s", shell.c_str(), app.c_str()).c_str(), L"Waiting debugger to attach...", MB_OK | MB_ICONEXCLAMATION);
#endif

char* psz_wsls_dist = getenv("wsls_dist");
// setup env
char* psz_path = getenv("path");
std::string path{ psz_path ? psz_path : "" };
if (path.find(psz_wsls_dist) == std::string::npos) {
if (psz_wsls_dist) {
std::string_view wsls_dist{ psz_wsls_dist };
path.insert(0, wsls_dist);
if constexpr (sizeof(void*) == 8)
path.insert(wsls_dist.size(), wsls_dist.back() == '\\' ? "x64;" : "\\x64;");
else
path.insert(wsls_dist.size(), wsls_dist.back() == '\\' ? "x86;" : "\\x86;");

SetEnvironmentVariableA("path", path.c_str());
_putenv_s("path", path.c_str());
path = getenv("path");
}
else {
fprintf(stderr, "Missing env var 'wsls_dist', please specify it to wsls_dist root directory\n");
return ERROR_INVALID_ENVIRONMENT;
}
std::string wsls_bin;
char* psz_wsls_dist = getenv("wsls_dist");
if (!psz_wsls_dist) {
PathRemoveFileSpecA(my_path);
wsls_bin = my_path;
}
else {
wsls_bin = psz_wsls_dist;
if constexpr (sizeof(void*) == 8)
wsls_bin.append(wsls_bin.back() == '\\' ? "x64" : "\\x64");
else
wsls_bin.append(wsls_bin.back() == '\\' ? "x86" : "\\x86");
}

if (path.find(wsls_bin) == std::string::npos) {
path.insert(0, wsls_bin);
path.insert(wsls_bin.size(), 1, ';');
SetEnvironmentVariableA("path", path.c_str());
_putenv_s("path", path.c_str());
path = getenv("path");
}

// execute
if (!shell.empty() && !app.empty())
return wsls::make_bridge(shell.c_str(), app.c_str());
return wsls::make_bridge(shell.c_str(), app.c_str(), forward_mode);
else
return ERROR_INVALID_PARAMETER;
}

return ERROR_UNKNOWN_FEATURE;
return GetLastError();
}

0 comments on commit bc081d6

Please sign in to comment.