-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wsl2exe.c
71 lines (57 loc) · 1.64 KB
/
wsl2exe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2017-2020 yuk7
* Author: yuk7 <[email protected]>
*
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "wsl2exe_common.h"
#include "wsld.h"
#define ARRAY_LENGTH(a) (sizeof(a)/sizeof(a[0]))
int main()
{
wchar_t **wargv;
int wargc;
wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
wchar_t *modName;
modName = GetModuleName();
wchar_t *distName;
distName = WslGetDefaultDistroName();
wchar_t **convdWargv;
convdWargv = malloc(sizeof(wchar_t*) * wargc);
wchar_t *modNameEscd;
modNameEscd = WcEscapeQuote(modName);
//prepare convdWargv
size_t modNameEscdSize = wcslen(modNameEscd);
convdWargv[0] = (wchar_t*)malloc(sizeof(wchar_t) * (modNameEscdSize + 1));
wcscpy_s(convdWargv[0], modNameEscdSize + 1, modNameEscd);
for (int i = 1; i < wargc; i++) {
convdWargv[i] = WcEscapeQuote(wargv[i]);
}
//launcher
size_t total = 0;
for (int i = 0; i < wargc; i++) {
total++; //for space
total += wcslen(convdWargv[i]);
}
total--;
size_t totalSize = (sizeof(wchar_t) * (total + 1));
wchar_t *command = (wchar_t*)malloc(totalSize);
wcscpy_s(command, totalSize, convdWargv[0]);
for (int i = 1; i < wargc; i++) {
wcscat_s(command, totalSize, L" ");
wcscat_s(command, totalSize, convdWargv[i]);
}
HRESULT hr;
DWORD exitCode = 1;
hr = WslLaunchInteractive(distName, command, true, &exitCode);
if (hr == S_OK) {
return exitCode;
} else {
return hr;
}
}