forked from microsoft/WSL-DistroLauncher
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathWslApiLoader.cpp
More file actions
119 lines (103 loc) · 3.69 KB
/
WslApiLoader.cpp
File metadata and controls
119 lines (103 loc) · 3.69 KB
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
//
// 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, const std::wstring& distributionNameOld) :
_distributionName(distributionName),
_distributionNameOld(distributionNameOld)
{
_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");
_unRegisterDistribution = (WSL_UN_REGISTER_DISTRIBUTION)GetProcAddress(_wslApiDll, "WslUnregisterDistribution");
}
}
WslApiLoader::~WslApiLoader()
{
if (_wslApiDll != nullptr)
{
FreeLibrary(_wslApiDll);
}
}
BOOL WslApiLoader::WslIsOptionalComponentInstalled() const
{
return _wslApiDll != nullptr &&
_isDistributionRegistered != nullptr &&
_registerDistribution != nullptr &&
_configureDistribution != nullptr &&
_launchInteractive != nullptr &&
_launch != nullptr;
}
BOOL WslApiLoader::WslIsDistributionRegistered()
{
if (!_isDistributionRegistered(_distributionName.c_str()))
{
if (!_isDistributionRegistered(_distributionNameOld.c_str()))
{
return FALSE;
}
_distributionName = _distributionNameOld;
return TRUE;
}
return TRUE;
}
HRESULT WslApiLoader::WslRegisterDistribution() const
{
const auto hr = _registerDistribution(_distributionName.c_str(), L"install.tar.gz");
if (FAILED(hr))
{
Helpers::PrintMessage(MSG_WSL_REGISTER_DISTRIBUTION_FAILED, hr);
}
return hr;
}
HRESULT WslApiLoader::WslUnregisterDistribution() const
{
const auto hr = _unRegisterDistribution(_distributionName.c_str());
if (FAILED(hr))
{
wprintf(L"failed");
Helpers::PrintMessage(MSG_WSL_UN_REGISTER_DISTRIBUTION_FAILED, hr);
}
return hr;
}
HRESULT WslApiLoader::WslConfigureDistribution(ULONG defaultUID, WSL_DISTRIBUTION_FLAGS wslDistributionFlags) const
{
const auto 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) const
{
const auto 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) const
{
const auto hr = _launch(_distributionName.c_str(), command, useCurrentWorkingDirectory, stdIn, stdOut, stdErr,
process);
if (FAILED(hr))
{
Helpers::PrintMessage(MSG_WSL_LAUNCH_FAILED, command, hr);
}
return hr;
}
void WslApiLoader::SetDistributionName(const std::wstring_view& distributionName)
{
_distributionName = std::wstring(distributionName);
}