-
Notifications
You must be signed in to change notification settings - Fork 0
/
wslwrap.cpp
149 lines (133 loc) · 5.74 KB
/
wslwrap.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This file is part of wslman.
*
* wslman is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* wslman is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wslman. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wslwrap.h"
#include <wslapi.h>
typedef HRESULT(STDAPICALLTYPE *WslConfigureDistribution_fn)(PCWSTR, ULONG,
WSL_DISTRIBUTION_FLAGS);
typedef HRESULT(STDAPICALLTYPE *WslGetDistributionConfiguration_fn)(PCWSTR, ULONG *,
ULONG *, WSL_DISTRIBUTION_FLAGS *, PSTR **, ULONG *);
typedef BOOL(STDAPICALLTYPE *WslIsDistributionRegistered_fn)(PCWSTR);
typedef HRESULT(STDAPICALLTYPE *WslLaunch_fn)(PCWSTR, PCWSTR, BOOL, HANDLE, HANDLE,
HANDLE, HANDLE *);
typedef HRESULT(STDAPICALLTYPE *WslLaunchInteractive_fn)(PCWSTR, PCWSTR, BOOL, DWORD *);
typedef HRESULT(STDAPICALLTYPE *WslRegisterDistribution_fn)(PCWSTR, PCWSTR);
typedef HRESULT(STDAPICALLTYPE *WslUnregisterDistribution_fn)(PCWSTR);
#define BIND_API(fname, hmodule) \
m_##fname = reinterpret_cast<fname##_fn>(GetProcAddress(hmodule, #fname))
struct WslApiBind
{
HMODULE m_wslAPIDll;
WslConfigureDistribution_fn m_WslConfigureDistribution;
WslGetDistributionConfiguration_fn m_WslGetDistributionConfiguration;
WslIsDistributionRegistered_fn m_WslIsDistributionRegistered;
WslLaunch_fn m_WslLaunch;
WslLaunchInteractive_fn m_WslLaunchInteractive;
WslRegisterDistribution_fn m_WslRegisterDistribution;
WslUnregisterDistribution_fn m_WslUnregisterDistribution;
WslApiBind()
{
m_wslAPIDll = LoadLibraryExW(L"wslapi.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (m_wslAPIDll) {
BIND_API(WslConfigureDistribution, m_wslAPIDll);
BIND_API(WslGetDistributionConfiguration, m_wslAPIDll);
BIND_API(WslIsDistributionRegistered, m_wslAPIDll);
BIND_API(WslLaunch, m_wslAPIDll);
BIND_API(WslLaunchInteractive, m_wslAPIDll);
BIND_API(WslRegisterDistribution, m_wslAPIDll);
BIND_API(WslUnregisterDistribution, m_wslAPIDll);
}
}
~WslApiBind()
{
if (m_wslAPIDll)
FreeLibrary(m_wslAPIDll);
}
static WslApiBind *instance()
{
static WslApiBind s_instance;
return &s_instance;
}
};
HRESULT WslApi::ConfigureDistribution(PCWSTR distributionName, ULONG defaultUID,
DistributionFlags wslDistributionFlags)
{
WslApiBind *api = WslApiBind::instance();
if (api->m_WslConfigureDistribution) {
return api->m_WslConfigureDistribution(distributionName, defaultUID,
static_cast<WSL_DISTRIBUTION_FLAGS>(wslDistributionFlags));
}
throw WslApiUnavailable();
}
HRESULT WslApi::GetDistributionConfiguration(PCWSTR distributionName,
ULONG *distributionVersion, ULONG *defaultUID,
DistributionFlags *wslDistributionFlags,
PSTR **defaultEnvironmentVariables,
ULONG *defaultEnvironmentVariableCount)
{
WslApiBind *api = WslApiBind::instance();
if (api->m_WslGetDistributionConfiguration) {
WSL_DISTRIBUTION_FLAGS rFlags;
auto result = api->m_WslGetDistributionConfiguration(distributionName,
distributionVersion, defaultUID, &rFlags,
defaultEnvironmentVariables, defaultEnvironmentVariableCount);
if (wslDistributionFlags)
*wslDistributionFlags = static_cast<DistributionFlags>(rFlags);
return result;
}
throw WslApiUnavailable();
}
BOOL WslApi::IsDistributionRegistered(PCWSTR distributionName)
{
WslApiBind *api = WslApiBind::instance();
if (api->m_WslIsDistributionRegistered)
return api->m_WslIsDistributionRegistered(distributionName);
throw WslApiUnavailable();
}
HRESULT WslApi::Launch(PCWSTR distributionName, PCWSTR command,
BOOL useCurrentWorkingDirectory, HANDLE stdIn, HANDLE stdOut,
HANDLE stdErr, HANDLE *process)
{
WslApiBind *api = WslApiBind::instance();
if (api->m_WslLaunch) {
return api->m_WslLaunch(distributionName, command, useCurrentWorkingDirectory,
stdIn, stdOut, stdErr, process);
}
throw WslApiUnavailable();
}
HRESULT WslApi::LaunchInteractive(PCWSTR distributionName, PCWSTR command,
BOOL useCurrentWorkingDirectory, DWORD *exitCode)
{
WslApiBind *api = WslApiBind::instance();
if (api->m_WslLaunchInteractive) {
return api->m_WslLaunchInteractive(distributionName, command,
useCurrentWorkingDirectory, exitCode);
}
throw WslApiUnavailable();
}
HRESULT WslApi::RegisterDistribution(PCWSTR distributionName, PCWSTR tarGzFilename)
{
WslApiBind *api = WslApiBind::instance();
if (api->m_WslRegisterDistribution)
return api->m_WslRegisterDistribution(distributionName, tarGzFilename);
throw WslApiUnavailable();
}
HRESULT WslApi::UnregisterDistribution(PCWSTR distributionName)
{
WslApiBind *api = WslApiBind::instance();
if (api->m_WslUnregisterDistribution)
return api->m_WslUnregisterDistribution(distributionName);
throw WslApiUnavailable();
}