Add scaffolding for C# projection of the SDK (#40212)
Flor Chacón committed
May 7, 2026 at 14:41 UTC
71d9429140b4aa8dd9b18a44f10bcd4e5e7a69e0
22 files changed
+764
-30
CMakeLists.txt
+3
-4
@@ -158,6 +158,9 @@ find_nuget_package(Microsoft.Windows.SDK.NET.Ref WINDOWS_SDK_DOTNET /)
158
find_nuget_package(Microsoft.Xaml.Behaviors.WinUI.Managed XAML_BEHAVIORS /)
159
find_nuget_package(WinUIEx WINUIEX /)
160
161
+# WSLC C# API packages
162
+find_nuget_package(Microsoft.Windows.CsWinRT CSWINRT /)
163
+
164
set(WSLG_TS_PLUGIN_DLL "WSLDVCPlugin.dll")
165
166
set(SUPPORTED_LANGS cs-CZ;da-DK;de-DE;en-GB;en-US;es-ES;fi-FI;fr-FR;hu-HU;it-IT;ja-JP;ko-KR;nb-NO;nl-NL;pl-PL;pt-BR;pt-PT;ru-RU;sv-SE;tr-TR;zh-CN;zh-TW)
@@ -536,10 +539,6 @@ add_subdirectory(src/windows/wslinstall)
539
add_subdirectory(src/windows/wslc)
540
add_subdirectory(src/windows/WslcSDK)
541
539
-if (WSL_BUILD_SDKCS)
540
- add_subdirectory(src/windows/WslcSDK/csharp)
541
-endif()
542
-
542
if (WSL_BUILD_WSL_SETTINGS)
543
add_subdirectory(src/windows/libwsl)
544
add_subdirectory(src/windows/wslsettings)
cmake/FindCSharp.cmake
-1
@@ -13,7 +13,6 @@ function(configure_csharp_target TARGET)
13
VS_GLOBAL_TargetPlatformMinVersion "${TARGET_PLATFORM_MIN_VERSION}"
14
VS_GLOBAL_WindowsSdkPackageVersion "${WINDOWS_SDK_DOTNET_VERSION}"
15
VS_GLOBAL_AppendRuntimeIdentifierToOutputPath false
16
- VS_GLOBAL_GenerateAssemblyInfo false
16
VS_GLOBAL_TargetLatestRuntimePatch false
17
DOTNET_SDK "Microsoft.NET.Sdk"
18
DOTNET_TARGET_FRAMEWORK "net8.0-windows${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}"
cmake/FindIDL.cmake
+46
-1
@@ -81,4 +81,49 @@ function(add_idl target idl_files_with_proxy idl_files_no_proxy)
81
DEPENDS ${TARGET_OUTPUTS}
82
SOURCES ${idl_files_with_proxy} ${idl_files_no_proxy})
83
84
-endfunction()
\ No newline at end of file
84
+endfunction()
85
+
86
+function(add_idl_winrt target idl_file)
87
+ set(OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PLATFORM}/${CMAKE_BUILD_TYPE})
88
+ file(TO_NATIVE_PATH ${OUTPUT_DIR} OUTPUT_DIR) # midl is picky about path formats
89
+ file(MAKE_DIRECTORY ${OUTPUT_DIR})
90
+
91
+ set(IDL_DEFINITIONS "")
92
+
93
+ get_directory_property(IDL_DEFS COMPILE_DEFINITIONS )
94
+ foreach(e ${IDL_DEFS})
95
+ set(IDL_DEFINITIONS ${IDL_DEFINITIONS} /D${e})
96
+ endforeach()
97
+
98
+ string(TOLOWER ${TARGET_PLATFORM} IDL_ENV)
99
+
100
+ cmake_host_system_information(
101
+ RESULT WINDOWS_SDK_DIR
102
+ QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Microsoft/Windows Kits/Installed Roots"
103
+ VALUE "KitsRoot10")
104
+ set(WINRT_METADATA_DIR "${WINDOWS_SDK_DIR}\\UnionMetadata\\${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
105
+ set(WINRT_REFERENCE "${WINRT_METADATA_DIR}\\Windows.winmd")
106
+ set(WINRT_INCLUDE "${WINDOWS_SDK_DIR}\\Include\\${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}\\winrt")
107
+
108
+ cmake_path(GET idl_file STEM IDL_NAME)
109
+
110
+ set(IDL_WINMD ${OUTPUT_DIR}/${IDL_NAME}.winmd)
111
+
112
+ add_custom_command(
113
+ OUTPUT ${IDL_WINMD}
114
+ COMMAND midl /nologo /nomidl /winrt /metadata_dir "${WINRT_METADATA_DIR}" /reference "${WINRT_REFERENCE}" /I "${WINRT_INCLUDE}" /env "${IDL_ENV}" /h nul /winmd ${IDL_WINMD} ${idl_file} ${IDL_DEFINITIONS}
115
+ COMMAND cppwinrt -input ${IDL_WINMD} -reference "${WINRT_REFERENCE}" -output ${OUTPUT_DIR} -comp ${OUTPUT_DIR}/implementation_base -optimize -pch precomp.h -prefix
116
+ WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
117
+ DEPENDS ${idl_file}
118
+ MAIN_DEPENDENCY ${idl_file}
119
+ VERBATIM
120
+ )
121
+
122
+ set_source_files_properties(${IDL_WINMD} PROPERTIES GENERATED TRUE)
123
+
124
+ add_custom_target(${target}
125
+ COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${target}
126
+ DEPENDS ${IDL_WINMD}
127
+ SOURCES ${idl_file})
128
+
129
+endfunction()
nuget/Microsoft.WSL.Containers.nuspec.in
+9
-5
@@ -17,11 +17,15 @@
17
</metadata>
18
<files>
19
<file src="${CMAKE_SOURCE_DIR_NATIVE}\src\windows\WslcSDK\wslcsdk.h" target="include"/>
20
- <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\x64\Release\wslcsdkcs.dll" target="lib\${WSL_NUGET_TARGET_FRAMEWORK}"/>
21
- <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\x64\Release\wslcsdk.lib" target="runtimes\win-x64"/>
22
- <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\x64\Release\wslcsdk.dll" target="runtimes\win-x64\native"/>
23
- <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\arm64\Release\wslcsdk.lib" target="runtimes\win-arm64"/>
24
- <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\arm64\Release\wslcsdk.dll" target="runtimes\win-arm64\native"/>
20
+
21
+ <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\x64\${CMAKE_BUILD_TYPE}\wslcsdkcs.dll" target="lib\${WSL_NUGET_TARGET_FRAMEWORK}\"/>
22
+
23
+ <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\x64\${CMAKE_BUILD_TYPE}\wslcsdk.lib" target="runtimes\win-x64\"/>
24
+ <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\x64\${CMAKE_BUILD_TYPE}\wslcsdk.dll" target="runtimes\win-x64\native\"/>
25
+
26
+ <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\arm64\${CMAKE_BUILD_TYPE}\wslcsdk.lib" target="runtimes\win-arm64\"/>
27
+ <file src="${CMAKE_SOURCE_DIR_NATIVE}\bin\arm64\${CMAKE_BUILD_TYPE}\wslcsdk.dll" target="runtimes\win-arm64\native\"/>
28
+
29
<file src="${CMAKE_SOURCE_DIR_NATIVE}\nuget\Microsoft.WSL.Containers\**" exclude="**\net\**"/>
30
<file src="${CMAKE_SOURCE_DIR_NATIVE}\nuget\Microsoft.WSL.Containers\build\net\**" target="build\${WSL_NUGET_TARGET_FRAMEWORK}"/>
31
</files>
packages.config
+1
@@ -12,6 +12,7 @@
12
<package id="Microsoft.NETCore.App.Runtime.win-x64" version="10.0.6" />
13
<package id="Microsoft.RemoteDesktop.Client.MSRDC.SessionHost" version="1.2.6676" />
14
<package id="Microsoft.Taef" version="10.100.251104001" targetFramework="native" />
15
+ <package id="Microsoft.Windows.CsWinRT" version="2.2.0" />
16
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.251108.1" targetFramework="native" />
17
<package id="Microsoft.Windows.SDK.NET.Ref" version="10.0.26100.81" />
18
<package id="Microsoft.WindowsAppSDK" version="1.8.251106002" />
src/windows/WslcSDK/CMakeLists.txt
+16
-5
@@ -6,6 +6,7 @@ set(SOURCES
6
WslcsdkPrivate.cpp
7
)
8
set(HEADERS
9
+ Defaults.h
10
IOCallback.h
11
ProgressCallback.h
12
TerminationCallback.h
@@ -14,8 +15,18 @@ set(HEADERS
15
)
16
17
add_library(wslcsdk SHARED ${SOURCES} ${HEADERS} wslcsdk.def)
17
-set_target_properties(wslcsdk PROPERTIES EXCLUDE_FROM_ALL FALSE)
18
-add_dependencies(wslcsdk wslserviceidl)
19
-target_link_libraries(wslcsdk ${COMMON_LINK_LIBRARIES} legacy_stdio_definitions common)
20
-target_precompile_headers(wslcsdk REUSE_FROM common)
21
-set_target_properties(wslcsdk PROPERTIES FOLDER windows)
18
+set_target_properties(wslcsdk
19
+ PROPERTIES
20
+ EXCLUDE_FROM_ALL FALSE
21
+ FOLDER windows
22
+)
23
+
24
+add_subdirectory(winrt)
25
+
26
+if (WSL_BUILD_SDKCS)
27
+ add_subdirectory(csharp)
28
+endif()
29
+
30
+add_dependencies(wslcsdk wslserviceidl wslcsdkwinrt)
31
+target_link_libraries(wslcsdk ${COMMON_LINK_LIBRARIES} legacy_stdio_definitions common wslcsdkwinrt)
32
+target_precompile_headers(wslcsdk REUSE_FROM common)
\ No newline at end of file
src/windows/WslcSDK/Defaults.h
new
+22
@@ -0,0 +1,22 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ Defaults.h
8
+
9
+Abstract:
10
+
11
+ Contains default values for settings used in the WSL Container SDK.
12
+
13
+--*/
14
+
15
+#pragma once
16
+
17
+constexpr uint32_t s_DefaultCPUCount = 2;
18
+constexpr uint32_t s_DefaultMemoryMB = 2000;
19
+// Maximum value per use with HVSOCKET_CONNECT_TIMEOUT_MAX
20
+constexpr ULONG s_DefaultBootTimeout = 300000;
21
+// Default to 1 GB
22
+constexpr UINT64 s_DefaultStorageSize = 1000 * 1000 * 1000;
src/windows/WslcSDK/csharp/CMakeLists.txt
+19
-3
@@ -1,13 +1,29 @@
1
enable_language(CSharp)
2
3
-add_library(wslcsdkcs SHARED "Projection.cs")
3
+add_library(wslcsdkcs SHARED)
4
configure_csharp_target(wslcsdkcs)
5
6
+set(WSLCSDK_WINMD "${CMAKE_CURRENT_BINARY_DIR}/../winrt/${TARGET_PLATFORM}/${CMAKE_BUILD_TYPE}/wslcsdk.winmd")
7
+
8
+target_sources(wslcsdkcs
9
+ PRIVATE
10
+ "${WSLCSDK_WINMD}"
11
+ "${CMAKE_CURRENT_LIST_DIR}/WinRTActivation.cs"
12
+)
13
+
14
+set_source_files_properties(
15
+ "${WSLCSDK_WINMD}"
16
+ PROPERTIES
17
+ GENERATED TRUE
18
+ VS_TOOL_OVERRIDE "CsWinRTInputs"
19
+)
20
+
21
set_target_properties(
22
wslcsdkcs PROPERTIES
23
FOLDER windows
24
LINKER_LANGUAGE CSharp
10
- VS_GLOBAL_PlatformTarget "AnyCPU"
25
+ VS_PACKAGE_REFERENCES "Microsoft.Windows.CsWinRT_${CSWINRT_VERSION}"
26
+ VS_GLOBAL_CsWinRTIncludes "Microsoft.WSL.Containers"
27
)
28
13
-add_dependencies(wslcsdkcs wslcsdk)
29
+add_dependencies(wslcsdkcs wslcsdkwinrt)
src/windows/WslcSDK/csharp/WinRTActivation.cs
new
+99
@@ -0,0 +1,99 @@
1
+// Copyright (C) Microsoft Corporation. All rights reserved.
2
+
3
+using System;
4
+using System.Runtime.CompilerServices;
5
+using System.Runtime.InteropServices;
6
+using WinRT;
7
+
8
+namespace Microsoft.WSL.Containers;
9
+
10
+internal static class WinRTActivation
11
+{
12
+ [UnmanagedFunctionPointer(CallingConvention.StdCall)]
13
+ private delegate int DllGetActivationFactoryFn(IntPtr classId, out IntPtr factory);
14
+
15
+ private static DllGetActivationFactoryFn s_getDllFactory;
16
+
17
+ // Overrides the WinRT activation to route activation of our types through our native DLL.
18
+ #pragma warning disable CA2255
19
+ // CA2255 warns against using ModuleInitializer to discourage using it without thinking through some possible issues.
20
+ // For example, unintuitive timing (it runs the first time a type is used, not when the module is loaded or inspected),
21
+ // and limiting possible optimizations (if the module is loaded, but nothing that depended on the initializer is ever used).
22
+ // In this case, we only need it to run before using any of our types, and everything in the assembly needs this
23
+ // initialization, so it's fine.
24
+ [ModuleInitializer]
25
+ #pragma warning restore CA2255
26
+ internal static void Initialize()
27
+ {
28
+ // Get a pointer to the function in the DLL that creates activation factories.
29
+ s_getDllFactory = Marshal.GetDelegateForFunctionPointer<DllGetActivationFactoryFn>(
30
+ NativeLibrary.GetExport(
31
+ NativeLibrary.Load("wslcsdk.dll", typeof(WinRTActivation).Assembly, DllImportSearchPath.AssemblyDirectory),
32
+ "DllGetActivationFactory"));
33
+
34
+ // Custom WinRT activation handler:
35
+ // If it is one of our types, we resolve it with our native DLL. Otherwise, we defer to the previous handler if one exists.
36
+ var previousHandler = ActivationFactory.ActivationHandler;
37
+ ActivationFactory.ActivationHandler = (typeName, iid) =>
38
+ {
39
+ if (typeName.StartsWith("Microsoft.WSL.Containers.", StringComparison.Ordinal))
40
+ {
41
+ return GetActivationFactory(typeName, iid);
42
+ }
43
+
44
+ if (previousHandler != null)
45
+ {
46
+ return previousHandler(typeName, iid);
47
+ }
48
+
49
+ return IntPtr.Zero;
50
+ };
51
+
52
+ }
53
+
54
+ private static IntPtr GetActivationFactory(string typeName, Guid iid)
55
+ {
56
+ // Convert the type name to HSTRING
57
+ WindowsCreateString(typeName, (uint)typeName.Length, out var hstring);
58
+ try
59
+ {
60
+ if (s_getDllFactory(hstring, out var factory) < 0)
61
+ {
62
+ return IntPtr.Zero;
63
+ }
64
+
65
+ if (iid == IID_IActivationFactory)
66
+ {
67
+ return factory;
68
+ }
69
+
70
+ try
71
+ {
72
+ if (Marshal.QueryInterface(factory, ref iid, out var queried) >= 0)
73
+ {
74
+ return queried;
75
+ }
76
+ else
77
+ {
78
+ return IntPtr.Zero;
79
+ }
80
+ }
81
+ finally
82
+ {
83
+ Marshal.Release(factory);
84
+ }
85
+ }
86
+ finally
87
+ {
88
+ WindowsDeleteString(hstring);
89
+ }
90
+ }
91
+
92
+ private static readonly Guid IID_IActivationFactory = new(0x00000035, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
93
+
94
+ [DllImport("combase.dll", CharSet = CharSet.Unicode)]
95
+ private static extern int WindowsCreateString(string sourceString, uint length, out IntPtr hstring);
96
+
97
+ [DllImport("combase.dll")]
98
+ private static extern int WindowsDeleteString(IntPtr hstring);
99
+}
src/windows/WslcSDK/winrt/CMakeLists.txt
new
+38
@@ -0,0 +1,38 @@
1
+add_idl_winrt(wslcsdkwinrtidl "wslcsdk.idl")
2
+set_target_properties(wslcsdkwinrtidl PROPERTIES FOLDER windows)
3
+
4
+set(SOURCES
5
+ Session.cpp
6
+ SessionSettings.cpp
7
+ VhdRequirements.cpp
8
+)
9
+
10
+set(HEADERS
11
+ Helpers.h
12
+ Session.h
13
+ SessionSettings.h
14
+ VhdRequirements.h
15
+)
16
+
17
+add_library(wslcsdkwinrt STATIC ${SOURCES} ${HEADERS})
18
+add_dependencies(wslcsdkwinrt wslcsdkwinrtidl)
19
+
20
+set(MODULE_G_CPP ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PLATFORM}/${CMAKE_BUILD_TYPE}/module.g.cpp)
21
+target_sources(wslcsdkwinrt PRIVATE ${MODULE_G_CPP})
22
+set_source_files_properties(
23
+ ${MODULE_G_CPP}
24
+ PROPERTIES
25
+ GENERATED TRUE)
26
+
27
+target_precompile_headers(wslcsdkwinrt PRIVATE precomp.h)
28
+target_include_directories(wslcsdkwinrt PRIVATE
29
+ ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PLATFORM}/${CMAKE_BUILD_TYPE}
30
+ ${CMAKE_SOURCE_DIR}/src/windows/WslcSDK
31
+)
32
+
33
+set_target_properties(
34
+ wslcsdkwinrt
35
+ PROPERTIES
36
+ FOLDER windows
37
+ EXCLUDE_FROM_ALL FALSE
38
+)
src/windows/WslcSDK/winrt/Helpers.h
new
+78
@@ -0,0 +1,78 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ Helpers.h
8
+
9
+Abstract:
10
+
11
+ This file contains helpers for the WinRT wrapper of WSLC SDK.
12
+
13
+--*/
14
+
15
+#pragma once
16
+
17
+#define THROW_MSG_IF_FAILED(hr, msg) \
18
+ do \
19
+ { \
20
+ const auto _hr = (hr); \
21
+ auto _msg = (msg).get(); \
22
+ if (_msg) \
23
+ { \
24
+ THROW_HR_IF_MSG(_hr, FAILED(_hr), "%ls", _msg); \
25
+ } \
26
+ else \
27
+ { \
28
+ THROW_IF_FAILED(_hr); \
29
+ } \
30
+ } while (0)
31
+
32
+template <typename T>
33
+struct implementation_type;
34
+
35
+#define DEFINE_TYPE_HELPERS(__type__) \
36
+ template <> \
37
+ struct implementation_type<winrt::Microsoft::WSL::Containers::__type__> \
38
+ { \
39
+ using type = winrt::Microsoft::WSL::Containers::implementation::__type__; \
40
+ };
41
+
42
+namespace winrt::Microsoft::WSL::Containers::implementation {
43
+template <typename T>
44
+auto* GetStructPointer(const T& obj)
45
+{
46
+ return obj ? winrt::get_self<typename implementation_type<T>::type>(obj)->ToStructPointer() : nullptr;
47
+}
48
+
49
+template <typename T>
50
+auto* GetHandlePointer(const T& obj)
51
+{
52
+ return obj ? winrt::get_self<typename implementation_type<T>::type>(obj)->ToHandlePointer() : nullptr;
53
+}
54
+
55
+template <typename T>
56
+auto GetHandle(const T& obj)
57
+{
58
+ return obj ? winrt::get_self<typename implementation_type<T>::type>(obj)->ToHandle() : nullptr;
59
+}
60
+
61
+template <typename T>
62
+auto* GetStructPointer(const winrt::com_ptr<T>& obj)
63
+{
64
+ return obj ? obj->ToStructPointer() : nullptr;
65
+}
66
+
67
+template <typename T>
68
+auto* GetHandlePointer(const winrt::com_ptr<T>& obj)
69
+{
70
+ return obj ? obj->ToHandlePointer() : nullptr;
71
+}
72
+
73
+template <typename T>
74
+auto GetHandle(const winrt::com_ptr<T>& obj)
75
+{
76
+ return obj ? obj->ToHandle() : nullptr;
77
+}
78
+} // namespace winrt::Microsoft::WSL::Containers::implementation
\ No newline at end of file
src/windows/WslcSDK/winrt/Session.cpp
new
+56
@@ -0,0 +1,56 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ Session.cpp
8
+
9
+Abstract:
10
+
11
+ This file contains the implementation of the WinRT wrapper for the WSLC SDK Session class.
12
+
13
+--*/
14
+
15
+#include "precomp.h"
16
+
17
+#include "Session.h"
18
+#include "Microsoft.WSL.Containers.Session.g.cpp"
19
+
20
+namespace WSLC = winrt::Microsoft::WSL::Containers;
21
+
22
+namespace winrt::Microsoft::WSL::Containers {
23
+
24
+implementation::Session::~Session()
25
+{
26
+ if (m_session)
27
+ {
28
+ WslcReleaseSession(m_session);
29
+ m_session = nullptr;
30
+ }
31
+}
32
+
33
+WSLC::Session implementation::Session::Create(WSLC::SessionSettings const& settings)
34
+{
35
+ auto session = winrt::make_self<implementation::Session>();
36
+ wil::unique_cotaskmem_string errorMessage;
37
+ auto hr = WslcCreateSession(implementation::GetStructPointer(settings), implementation::GetHandlePointer(session), &errorMessage);
38
+ THROW_MSG_IF_FAILED(hr, errorMessage);
39
+ return *session;
40
+}
41
+
42
+void implementation::Session::Terminate()
43
+{
44
+ winrt::check_hresult(WslcTerminateSession(m_session));
45
+}
46
+
47
+WslcSession implementation::Session::ToHandle()
48
+{
49
+ return m_session;
50
+}
51
+
52
+WslcSession* implementation::Session::ToHandlePointer()
53
+{
54
+ return &m_session;
55
+}
56
+} // namespace winrt::Microsoft::WSL::Containers
src/windows/WslcSDK/winrt/Session.h
new
+43
@@ -0,0 +1,43 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ Session.h
8
+
9
+Abstract:
10
+
11
+ This file contains the definition of the WinRT wrapper for the WSLC SDK Session class.
12
+
13
+--*/
14
+
15
+#pragma once
16
+#include "Microsoft.WSL.Containers.Session.g.h"
17
+#include "SessionSettings.h"
18
+#include "Helpers.h"
19
+
20
+namespace winrt::Microsoft::WSL::Containers::implementation {
21
+struct Session : SessionT<Session>
22
+{
23
+ Session() = default;
24
+ ~Session();
25
+
26
+ static winrt::Microsoft::WSL::Containers::Session Create(winrt::Microsoft::WSL::Containers::SessionSettings const& settings);
27
+ void Terminate();
28
+
29
+ WslcSession ToHandle();
30
+ WslcSession* ToHandlePointer();
31
+
32
+private:
33
+ WslcSession m_session{nullptr};
34
+};
35
+} // namespace winrt::Microsoft::WSL::Containers::implementation
36
+
37
+namespace winrt::Microsoft::WSL::Containers::factory_implementation {
38
+struct Session : SessionT<Session, implementation::Session>
39
+{
40
+};
41
+} // namespace winrt::Microsoft::WSL::Containers::factory_implementation
42
+
43
+DEFINE_TYPE_HELPERS(Session);
\ No newline at end of file
src/windows/WslcSDK/winrt/SessionSettings.cpp
new
+99
@@ -0,0 +1,99 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ SessionSettings.cpp
8
+
9
+Abstract:
10
+
11
+ This file contains the implementation of the WinRT wrapper for the WSLC SDK SessionSettings class.
12
+
13
+--*/
14
+
15
+#include "precomp.h"
16
+#include "SessionSettings.h"
17
+#include "Microsoft.WSL.Containers.SessionSettings.g.cpp"
18
+
19
+namespace WSLC = winrt::Microsoft::WSL::Containers;
20
+
21
+namespace winrt::Microsoft::WSL::Containers {
22
+implementation::SessionSettings::SessionSettings(hstring const& name, hstring const& storagePath) :
23
+ m_name(name), m_storagePath(storagePath)
24
+{
25
+ winrt::check_hresult(WslcInitSessionSettings(m_name.c_str(), m_storagePath.c_str(), &m_sessionSettings));
26
+}
27
+
28
+hstring implementation::SessionSettings::Name()
29
+{
30
+ return hstring{m_name};
31
+}
32
+
33
+hstring implementation::SessionSettings::StoragePath()
34
+{
35
+ return hstring{m_storagePath};
36
+}
37
+
38
+uint32_t implementation::SessionSettings::CpuCount()
39
+{
40
+ return m_cpuCount;
41
+}
42
+
43
+void implementation::SessionSettings::CpuCount(uint32_t value)
44
+{
45
+ m_cpuCount = value;
46
+ winrt::check_hresult(WslcSetSessionSettingsCpuCount(&m_sessionSettings, m_cpuCount));
47
+}
48
+
49
+uint32_t implementation::SessionSettings::MemoryMb()
50
+{
51
+ return m_memoryMb;
52
+}
53
+
54
+void implementation::SessionSettings::MemoryMb(uint32_t value)
55
+{
56
+ m_memoryMb = value;
57
+ winrt::check_hresult(WslcSetSessionSettingsMemory(&m_sessionSettings, m_memoryMb));
58
+}
59
+
60
+uint32_t implementation::SessionSettings::TimeoutMS()
61
+{
62
+ return m_timeoutMS;
63
+}
64
+
65
+void implementation::SessionSettings::TimeoutMS(uint32_t value)
66
+{
67
+ m_timeoutMS = value;
68
+ winrt::check_hresult(WslcSetSessionSettingsTimeout(&m_sessionSettings, m_timeoutMS));
69
+}
70
+
71
+WSLC::VhdRequirements implementation::SessionSettings::VhdRequirements()
72
+{
73
+ return m_vhdRequirements;
74
+}
75
+
76
+void implementation::SessionSettings::VhdRequirements(WSLC::VhdRequirements const& value)
77
+{
78
+ m_vhdRequirements = value;
79
+ auto vhdRequirements =
80
+ m_vhdRequirements ? winrt::get_self<implementation::VhdRequirements>(m_vhdRequirements)->ToStructPointer() : nullptr;
81
+ winrt::check_hresult(WslcSetSessionSettingsVhd(&m_sessionSettings, vhdRequirements));
82
+}
83
+
84
+WSLC::SessionFeatureFlags implementation::SessionSettings::FeatureFlags()
85
+{
86
+ return m_featureFlags;
87
+}
88
+
89
+void implementation::SessionSettings::FeatureFlags(WSLC::SessionFeatureFlags const& value)
90
+{
91
+ m_featureFlags = value;
92
+ winrt::check_hresult(WslcSetSessionSettingsFeatureFlags(&m_sessionSettings, static_cast<WslcSessionFeatureFlags>(m_featureFlags)));
93
+}
94
+
95
+WslcSessionSettings* implementation::SessionSettings::ToStructPointer()
96
+{
97
+ return &m_sessionSettings;
98
+}
99
+} // namespace winrt::Microsoft::WSL::Containers
src/windows/WslcSDK/winrt/SessionSettings.h
new
+60
@@ -0,0 +1,60 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ SessionSettings.h
8
+
9
+Abstract:
10
+
11
+ This file contains the definition of the WinRT wrapper for the WSLC SDK SessionSettings class.
12
+
13
+--*/
14
+
15
+#pragma once
16
+#include "Microsoft.WSL.Containers.SessionSettings.g.h"
17
+#include "VhdRequirements.h"
18
+#include "Helpers.h"
19
+#include "Defaults.h"
20
+
21
+namespace winrt::Microsoft::WSL::Containers::implementation {
22
+struct SessionSettings : SessionSettingsT<SessionSettings>
23
+{
24
+ SessionSettings() = default;
25
+
26
+ SessionSettings(hstring const& name, hstring const& storagePath);
27
+ hstring Name();
28
+ hstring StoragePath();
29
+ uint32_t CpuCount();
30
+ void CpuCount(uint32_t value);
31
+ uint32_t MemoryMb();
32
+ void MemoryMb(uint32_t value);
33
+ uint32_t TimeoutMS();
34
+ void TimeoutMS(uint32_t value);
35
+ winrt::Microsoft::WSL::Containers::VhdRequirements VhdRequirements();
36
+ void VhdRequirements(winrt::Microsoft::WSL::Containers::VhdRequirements const& value);
37
+ winrt::Microsoft::WSL::Containers::SessionFeatureFlags FeatureFlags();
38
+ void FeatureFlags(winrt::Microsoft::WSL::Containers::SessionFeatureFlags const& value);
39
+
40
+ WslcSessionSettings* ToStructPointer();
41
+
42
+private:
43
+ WslcSessionSettings m_sessionSettings{};
44
+ std::wstring m_name;
45
+ std::wstring m_storagePath;
46
+ uint32_t m_cpuCount{s_DefaultCPUCount};
47
+ uint32_t m_memoryMb{s_DefaultMemoryMB};
48
+ uint32_t m_timeoutMS{s_DefaultBootTimeout};
49
+ winrt::Microsoft::WSL::Containers::VhdRequirements m_vhdRequirements{nullptr};
50
+ winrt::Microsoft::WSL::Containers::SessionFeatureFlags m_featureFlags{winrt::Microsoft::WSL::Containers::SessionFeatureFlags::None};
51
+};
52
+} // namespace winrt::Microsoft::WSL::Containers::implementation
53
+
54
+namespace winrt::Microsoft::WSL::Containers::factory_implementation {
55
+struct SessionSettings : SessionSettingsT<SessionSettings, implementation::SessionSettings>
56
+{
57
+};
58
+} // namespace winrt::Microsoft::WSL::Containers::factory_implementation
59
+
60
+DEFINE_TYPE_HELPERS(SessionSettings);
\ No newline at end of file
src/windows/WslcSDK/winrt/VhdRequirements.cpp
new
+47
@@ -0,0 +1,47 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ VhdRequirements.cpp
8
+
9
+Abstract:
10
+
11
+ This file contains the implementation of the WinRT wrapper for the WSLC SDK VhdRequirements class.
12
+
13
+--*/
14
+
15
+#include "precomp.h"
16
+#include "VhdRequirements.h"
17
+#include "Microsoft.WSL.Containers.VhdRequirements.g.cpp"
18
+
19
+namespace winrt::Microsoft::WSL::Containers::implementation {
20
+VhdRequirements::VhdRequirements(hstring const& name, uint64_t sizeInBytes, winrt::Microsoft::WSL::Containers::VhdType const& type) :
21
+ m_name(winrt::to_string(name))
22
+{
23
+ m_vhdRequirements.name = m_name.c_str();
24
+ m_vhdRequirements.sizeBytes = sizeInBytes;
25
+ m_vhdRequirements.type = static_cast<WslcVhdType>(type);
26
+}
27
+
28
+hstring VhdRequirements::Name()
29
+{
30
+ return winrt::to_hstring(m_name);
31
+}
32
+
33
+uint64_t VhdRequirements::SizeInBytes()
34
+{
35
+ return m_vhdRequirements.sizeBytes;
36
+}
37
+
38
+winrt::Microsoft::WSL::Containers::VhdType VhdRequirements::Type()
39
+{
40
+ return static_cast<winrt::Microsoft::WSL::Containers::VhdType>(m_vhdRequirements.type);
41
+}
42
+
43
+WslcVhdRequirements* VhdRequirements::ToStructPointer()
44
+{
45
+ return &m_vhdRequirements;
46
+}
47
+} // namespace winrt::Microsoft::WSL::Containers::implementation
\ No newline at end of file
src/windows/WslcSDK/winrt/VhdRequirements.h
new
+43
@@ -0,0 +1,43 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ VhdRequirements.h
8
+
9
+Abstract:
10
+
11
+ This file contains the definition of the WinRT wrapper for the WSLC SDK VhdRequirements class.
12
+
13
+--*/
14
+
15
+#pragma once
16
+#include "Microsoft.WSL.Containers.VhdRequirements.g.h"
17
+#include "Helpers.h"
18
+
19
+namespace winrt::Microsoft::WSL::Containers::implementation {
20
+struct VhdRequirements : VhdRequirementsT<VhdRequirements>
21
+{
22
+ VhdRequirements() = default;
23
+
24
+ VhdRequirements(hstring const& name, uint64_t sizeInBytes, winrt::Microsoft::WSL::Containers::VhdType const& type);
25
+ hstring Name();
26
+ uint64_t SizeInBytes();
27
+ winrt::Microsoft::WSL::Containers::VhdType Type();
28
+
29
+ WslcVhdRequirements* ToStructPointer();
30
+
31
+private:
32
+ std::string m_name;
33
+ WslcVhdRequirements m_vhdRequirements{nullptr};
34
+};
35
+} // namespace winrt::Microsoft::WSL::Containers::implementation
36
+
37
+namespace winrt::Microsoft::WSL::Containers::factory_implementation {
38
+struct VhdRequirements : VhdRequirementsT<VhdRequirements, implementation::VhdRequirements>
39
+{
40
+};
41
+} // namespace winrt::Microsoft::WSL::Containers::factory_implementation
42
+
43
+DEFINE_TYPE_HELPERS(VhdRequirements);
\ No newline at end of file
src/windows/WslcSDK/winrt/precomp.h
new
+22
@@ -0,0 +1,22 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ precomp.h
8
+
9
+Abstract:
10
+
11
+ The precompiled header for WslcSDK/winrt.
12
+
13
+--*/
14
+
15
+#pragma once
16
+
17
+#include "wslcsdk.h"
18
+
19
+#include <wil/winrt.h>
20
+#include <wil/resource.h>
21
+
22
+#include <winrt/Windows.Foundation.h>
src/windows/WslcSDK/winrt/wslcsdk.idl
new
+59
@@ -0,0 +1,59 @@
1
+/*++
2
+
3
+Copyright (c) Microsoft. All rights reserved.
4
+
5
+Module Name:
6
+
7
+ wslcsdk.idl
8
+
9
+Abstract:
10
+
11
+ This file contains the definition of a WinRT projection of the WSL Container SDK API.
12
+
13
+--*/
14
+
15
+namespace Microsoft.WSL.Containers
16
+{
17
+ runtimeclass SessionSettings
18
+ {
19
+ SessionSettings(String name, String storagePath);
20
+
21
+ String Name { get; };
22
+ String StoragePath { get; };
23
+
24
+ UInt32 CpuCount { get; set; };
25
+ UInt32 MemoryMb { get; set; };
26
+ UInt32 TimeoutMS { get; set; };
27
+ VhdRequirements VhdRequirements { get; set; };
28
+ SessionFeatureFlags FeatureFlags { get; set; };
29
+ };
30
+
31
+ runtimeclass Session
32
+ {
33
+ static Session Create(SessionSettings settings);
34
+
35
+ void Terminate();
36
+ };
37
+
38
+ [flags]
39
+ enum SessionFeatureFlags
40
+ {
41
+ None = 0x00000000,
42
+ EnableGpu = 0x00000004
43
+ };
44
+
45
+ enum VhdType
46
+ {
47
+ Dynamic = 0,
48
+ Fixed = 1,
49
+ };
50
+
51
+ runtimeclass VhdRequirements
52
+ {
53
+ VhdRequirements(String name, UInt64 sizeInBytes, VhdType type);
54
+
55
+ String Name { get; };
56
+ UInt64 SizeInBytes { get; };
57
+ VhdType Type { get; };
58
+ };
59
+}
\ No newline at end of file
src/windows/WslcSDK/wslcsdk.cpp
+1
-6
@@ -15,6 +15,7 @@ Abstract:
15
16
#include "wslcsdk.h"
17
#include "WslcsdkPrivate.h"
18
+#include "Defaults.h"
19
#include "ProgressCallback.h"
20
#include "TerminationCallback.h"
21
#include "Localization.h"
@@ -26,12 +27,6 @@ using namespace std::string_view_literals;
27
using namespace wsl::windows::common::wslutil;
28
29
namespace {
29
-constexpr uint32_t s_DefaultCPUCount = 2;
30
-constexpr uint32_t s_DefaultMemoryMB = 2000;
31
-// Maximum value per use with HVSOCKET_CONNECT_TIMEOUT_MAX
32
-constexpr ULONG s_DefaultBootTimeout = 300000;
33
-// Default to 1 GB
34
-constexpr UINT64 s_DefaultStorageSize = 1000 * 1000 * 1000;
30
31
#define WSLC_FLAG_VALUE_ASSERT(_wlsc_name_, _wslc_name_) \
32
static_assert(_wlsc_name_ == _wslc_name_, "Flag values differ: " #_wlsc_name_ " != " #_wslc_name_);
src/windows/WslcSDK/wslcsdk.def
+2
-5
@@ -70,8 +70,5 @@ WslcGetMissingComponents
70
WslcGetVersion
71
WslcInstallWithDependencies
72
73
-
74
-
75
-
76
-
77
-
73
+DllGetActivationFactory = WINRT_GetActivationFactory PRIVATE
74
+DllCanUnloadNow = WINRT_CanUnloadNow PRIVATE
src/windows/wslsettings/CMakeLists.txt
+1
@@ -233,6 +233,7 @@ set_target_properties(
233
VS_GLOBAL_PlatformTarget "${TARGET_PLATFORM}"
234
VS_GLOBAL_ImplicitUsings enable
235
VS_GLOBAL_Nullable enable
236
+ VS_GLOBAL_GenerateAssemblyInfo false
237
)
238
239
configure_file(