Skip to content

Commit

Permalink
scrunge, qode
Browse files Browse the repository at this point in the history
Not only is it particular about it's cwd and exe name, it's
particular about it's command line arguments. scrunge scringe scrin
  • Loading branch information
falfiya committed May 19, 2021
1 parent 240f0ee commit b6ea201
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ build: clean dist/index.js
move $(build) $@
$(rcedit) $@/qode.exe --set-icon misc/icon.ico

f: build/$(name).exe
./$<

build/$(name).exe: src/stub.c
clang $< -Ofast -o $@

clean:
-rd /s /q dist deploy build

Expand Down
8 changes: 8 additions & 0 deletions src/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
QListWidget,
QListWidgetItem,
QIcon,
QMessageBox,
} = require("@nodegui/nodegui");

const win = new QMainWindow;
Expand Down Expand Up @@ -207,3 +208,10 @@ go_button.addEventListener("clicked", () => {

win.show();
global.win = win;

for (const arg of process.argv) {
console.log(arg);
const qlwi = new QListWidgetItem;
qlwi.setText(arg);
depth1_list.addItem(qlwi);
}
86 changes: 86 additions & 0 deletions src/stub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <wchar.h>
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <windows.h>

#define qode "qode.exe"
#define true 1
#define SETCWDTO "SETCWDTO"

STARTUPINFOW si;

#define LEN(s) (sizeof(s) - 1)

int wmain(int argc, wchar_t *argv[]) {
DWORD name_len = 0;
wchar_t *name = *argv;
int last_backslash = -1;

wchar_t c;
while ((c = name[name_len])) {
if (c == L'\\' || c == L'/') {
last_backslash = name_len;
}
name_len++;
}

// includes null byte
size_t qode_len = last_backslash + sizeof(qode);
for (size_t i = 0; i < sizeof(qode); ++i) {
// + 1 because we want to write starting after the '/'
name[last_backslash + 1 + i] = qode[i];
}

// name is now `${__dirname}/qode.exe`

// includes null byte
DWORD cwd_len = GetCurrentDirectoryW(0, NULL);

// the memory layout is going to look like `${__dirname}\0${qode} ${cwd}`
wchar_t *dirname = _alloca(
sizeof(wchar_t) * (0
+ last_backslash + LEN("\0")
+ qode_len + LEN(" ") + LEN(SETCWDTO) + LEN(" ") + cwd_len
)
);

for (size_t i = 0; i < last_backslash; ++i) {
dirname[i] = name[i];
}
dirname[last_backslash] = '\0';

wchar_t *qode_arg = dirname + last_backslash + 1;
for (size_t i = 0; i < qode_len; ++i) {
qode_arg[i] = name[i];
}

wchar_t *cwd_arg = qode_arg + qode_len;
*cwd_arg++ = ' ';

for (size_t i = 0; i < LEN(SETCWDTO); ++i) {
*cwd_arg++ = SETCWDTO[i];
}

GetCurrentDirectoryW(cwd_len, cwd_arg);

wprintf(L"qode: %s\ndirname: %s\nargs: %s\n", name, dirname, qode_arg);

PROCESS_INFORMATION pi;

BOOL success = CreateProcessW(
name, // lpApplicationName
qode_arg, // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
true, // bInheritHandles
0, // dwCreationFlags
NULL, // lpEnvironment
dirname, // lpCurrentDirectory
&si, // lpStartupInfo
&pi // lpProcessInformation
);

{
printf("Error: %lu\n", GetLastError());
}
}

0 comments on commit b6ea201

Please sign in to comment.