-
Notifications
You must be signed in to change notification settings - Fork 522
/
WslApiLoader.cpp
82 lines (69 loc) · 2.78 KB
/
WslApiLoader.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
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//
#include "stdafx.h"
#include "WslApiLoader.h"
WslApiLoader::WslApiLoader(const std::wstring& distributionName) :
_distributionName(distributionName)
{
_wslApiDll = LoadLibraryEx(L"wslapi.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (_wslApiDll != nullptr) {
_isDistributionRegistered = (WSL_IS_DISTRIBUTION_REGISTERED)GetProcAddress(_wslApiDll, "WslIsDistributionRegistered");
_registerDistribution = (WSL_REGISTER_DISTRIBUTION)GetProcAddress(_wslApiDll, "WslRegisterDistribution");
_configureDistribution = (WSL_CONFIGURE_DISTRIBUTION)GetProcAddress(_wslApiDll, "WslConfigureDistribution");
_launchInteractive = (WSL_LAUNCH_INTERACTIVE)GetProcAddress(_wslApiDll, "WslLaunchInteractive");
_launch = (WSL_LAUNCH)GetProcAddress(_wslApiDll, "WslLaunch");
}
}
WslApiLoader::~WslApiLoader()
{
if (_wslApiDll != nullptr) {
FreeLibrary(_wslApiDll);
}
}
BOOL WslApiLoader::WslIsOptionalComponentInstalled()
{
return ((_wslApiDll != nullptr) &&
(_isDistributionRegistered != nullptr) &&
(_registerDistribution != nullptr) &&
(_configureDistribution != nullptr) &&
(_launchInteractive != nullptr) &&
(_launch != nullptr));
}
BOOL WslApiLoader::WslIsDistributionRegistered()
{
return _isDistributionRegistered(_distributionName.c_str());
}
HRESULT WslApiLoader::WslRegisterDistribution()
{
HRESULT hr = _registerDistribution(_distributionName.c_str(), L"install.tar.gz");
if (FAILED(hr)) {
Helpers::PrintMessage(MSG_WSL_REGISTER_DISTRIBUTION_FAILED, hr);
}
return hr;
}
HRESULT WslApiLoader::WslConfigureDistribution(ULONG defaultUID, WSL_DISTRIBUTION_FLAGS wslDistributionFlags)
{
HRESULT hr = _configureDistribution(_distributionName.c_str(), defaultUID, wslDistributionFlags);
if (FAILED(hr)) {
Helpers::PrintMessage(MSG_WSL_CONFIGURE_DISTRIBUTION_FAILED, hr);
}
return hr;
}
HRESULT WslApiLoader::WslLaunchInteractive(PCWSTR command, BOOL useCurrentWorkingDirectory, DWORD *exitCode)
{
HRESULT hr = _launchInteractive(_distributionName.c_str(), command, useCurrentWorkingDirectory, exitCode);
if (FAILED(hr)) {
Helpers::PrintMessage(MSG_WSL_LAUNCH_INTERACTIVE_FAILED, command, hr);
}
return hr;
}
HRESULT WslApiLoader::WslLaunch(PCWSTR command, BOOL useCurrentWorkingDirectory, HANDLE stdIn, HANDLE stdOut, HANDLE stdErr, HANDLE *process)
{
HRESULT hr = _launch(_distributionName.c_str(), command, useCurrentWorkingDirectory, stdIn, stdOut, stdErr, process);
if (FAILED(hr)) {
Helpers::PrintMessage(MSG_WSL_LAUNCH_FAILED, command, hr);
}
return hr;
}