diff --git a/Cargo.toml b/Cargo.toml index 4c7ea1d..5ae69ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,4 @@ repository = "https://github.com/mveril/wslplugins-rs" description = "A Rust framework for developing WSL plugins using safe and idiomatic Rust." [patch.crates-io] -wslpluginapi-sys = { version = "0.1.0-rc.1", git = "https://github.com/mveril/wslpluginapi-sys.git", branch = "release/0.1.0-rc.1+2.4.4" } +wslpluginapi-sys = { version = "0.1.0-rc.1", git = "https://github.com/mveril/wslpluginapi-sys.git", branch = "develop" } diff --git a/wslplugins-macro-core/src/generator/c_funcs_tokens.rs b/wslplugins-macro-core/src/generator/c_funcs_tokens.rs index 8f97230..1e9cea1 100644 --- a/wslplugins-macro-core/src/generator/c_funcs_tokens.rs +++ b/wslplugins-macro-core/src/generator/c_funcs_tokens.rs @@ -13,99 +13,90 @@ pub(super) fn get_c_func_tokens(hook: Hooks) -> Result> { extern "C" fn #c_method_ident( session: *const ::wslplugins_rs::sys::WSLSessionInformation, settings: *const ::wslplugins_rs::sys::WSLVmCreationSettings, - ) -> ::windows::core::HRESULT { + ) -> ::wslplugins_rs::sys::windows_sys::core::HRESULT { let session_ptr = unsafe { &*session }; let settings_ptr = unsafe { &*settings }; - if let Some(plugin) = PLUGIN.get() { + PLUGIN.get().map(|plugin|{ let result = plugin.#trait_method_ident( session_ptr.as_ref(), settings_ptr.as_ref(), ); - ::wslplugins_rs::plugin::utils::consume_to_win_result(result).into() - } else { - ::windows::Win32::Foundation::E_FAIL - } + ::wslplugins_rs::windows_core::HRESULT::from(::wslplugins_rs::plugin::utils::consume_to_win_result(result)).0 + }).unwrap_or(::wslplugins_rs::sys::windows_sys::Win32::Foundation::E_FAIL) } }), Hooks::OnVMStopping => Some(quote! { extern "C" fn #c_method_ident( session: *const ::wslplugins_rs::sys::WSLSessionInformation - ) -> ::windows::core::HRESULT { + ) -> ::wslplugins_rs::sys::windows_sys::core::HRESULT { let session_ptr = unsafe { &*session }; - if let Some(plugin) = PLUGIN.get() { - plugin.#trait_method_ident(session_ptr.as_ref()).into() - } else { - ::windows::Win32::Foundation::E_FAIL - } + PLUGIN.get() + .map(|plugin| { + let result = plugin.#trait_method_ident(session_ptr.as_ref()); + ::wslplugins_rs::windows_core::HRESULT::from(result).0 + }) + .unwrap_or(::wslplugins_rs::sys::windows_sys::Win32::Foundation::E_FAIL) } }), Hooks::OnDistributionStarted => Some(quote! { extern "C" fn #c_method_ident( session: *const ::wslplugins_rs::sys::WSLSessionInformation, distribution: *const ::wslplugins_rs::sys::WSLDistributionInformation, - ) -> ::windows::core::HRESULT { + ) -> ::wslplugins_rs::sys::windows_sys::core::HRESULT { let session_ptr = unsafe { &*session }; let distribution_ptr = unsafe { &*distribution }; - if let Some(plugin) = PLUGIN.get() { + PLUGIN.get().map(|plugin|{ let result = plugin.#trait_method_ident( session_ptr.as_ref(), distribution_ptr.as_ref(), ); - ::wslplugins_rs::plugin::utils::consume_to_win_result(result).into() - } else { - ::windows::Win32::Foundation::E_FAIL - } + ::wslplugins_rs::windows_core::HRESULT::from(::wslplugins_rs::plugin::utils::consume_to_win_result(result)).0 + }).unwrap_or(::wslplugins_rs::sys::windows_sys::Win32::Foundation::E_FAIL) } }), Hooks::OnDistributionStopping => Some(quote! { extern "C" fn #c_method_ident( session: *const ::wslplugins_rs::sys::WSLSessionInformation, distribution: *const ::wslplugins_rs::sys::WSLDistributionInformation, - ) -> ::windows::core::HRESULT { + ) -> ::wslplugins_rs::sys::windows_sys::core::HRESULT { let session_ptr = unsafe { &*session }; let distribution_ptr = unsafe { &*distribution }; - if let Some(plugin) = PLUGIN.get() { - plugin.#trait_method_ident( + PLUGIN.get().map(|plugin|{ + ::wslplugins_rs::windows_core::HRESULT::from(plugin.#trait_method_ident( session_ptr.as_ref(), distribution_ptr.as_ref(), - ).into() - } else { - ::windows::Win32::Foundation::E_FAIL - } + )).0 + }).unwrap_or(::wslplugins_rs::sys::windows_sys::Win32::Foundation::E_FAIL) } }), Hooks::OnDistributionRegistered => Some(quote! { extern "C" fn #c_method_ident( session: *const ::wslplugins_rs::sys::WSLSessionInformation, distribution: *const ::wslplugins_rs::sys::WSLOfflineDistributionInformation, - ) -> ::windows::core::HRESULT { + ) -> ::wslplugins_rs::sys::windows_sys::core::HRESULT { let session_ptr = unsafe { &*session }; let distribution_ptr = unsafe { &*distribution }; - if let Some(plugin) = PLUGIN.get() { - plugin.#trait_method_ident( + PLUGIN.get().map(|plugin|{ + ::wslplugins_rs::windows_core::HRESULT::from(plugin.#trait_method_ident( session_ptr.as_ref(), distribution_ptr.as_ref(), - ).into() - } else { - ::windows::Win32::Foundation::E_FAIL - } + )).0 + }).unwrap_or(::wslplugins_rs::sys::windows_sys::Win32::Foundation) } }), Hooks::OnDistributionUnregistered => Some(quote! { extern "C" fn #c_method_ident( session: *const ::wslplugins_rs::sys::WSLSessionInformation, distribution: *const ::wslplugins_rs::sys::WSLOfflineDistributionInformation, - ) -> ::windows::core::HRESULT { + ) -> ::wslplugins_rs::sys::windows_sys::core::HRESULT { let session_ptr = unsafe { &*session }; let distribution_ptr = unsafe { &*distribution }; - if let Some(plugin) = PLUGIN.get() { - plugin.#trait_method_ident( + PLUGIN.get().map(|plugin|{ + ::wslplugins_rs::windows_core::HRESULT::from(plugin.#trait_method_ident( session_ptr.as_ref(), distribution_ptr.as_ref(), - ).into() - } else { - ::windows::Win32::Foundation::E_FAIL - } + )).0 + }).unwrap_or(::wslplugins_rs::sys::windows_sys::Win32::Foundation) } }), }; diff --git a/wslplugins-macro-core/src/generator/hook_field_mapping.rs b/wslplugins-macro-core/src/generator/hook_field_mapping.rs index 926c81c..c27844b 100644 --- a/wslplugins-macro-core/src/generator/hook_field_mapping.rs +++ b/wslplugins-macro-core/src/generator/hook_field_mapping.rs @@ -98,7 +98,7 @@ fn generate_entry_point(imp: &ParsedImpl, version: &RequiredVersion) -> Result ::windows::core::HRESULT { + ) -> ::wslplugins_rs::windows_core::HRESULT { unsafe { let api_ref: &'static ::wslplugins_rs::sys::WSLPluginAPIV1 = unsafe { &*api}; let #hooks_ref_name: &mut ::wslplugins_rs::sys::WSLPluginHooksV1 = unsafe{ &mut *hooks }; @@ -109,10 +109,10 @@ fn generate_entry_point(imp: &ParsedImpl, version: &RequiredVersion) -> Result ::windows::core::Result<()> { + ) -> ::wslplugins_rs::windows_core::Result<()> { let plugin: #static_plugin_type = ::wslplugins_rs::plugin::create_plugin_with_required_version(api, #major, #minor, #revision)?; #(#hook_set)* - PLUGIN.set(plugin).map_err(|_| ::windows::core::Error::from(::windows::Win32::Foundation::E_ABORT)) + PLUGIN.set(plugin).map_err(|_| ::wslplugins_rs::windows_core::Error::from(::wslplugins_rs::windows_core::HRESULT(::wslplugins_rs::sys::windows_sys::Win32::Foundation::E_ABORT))) } }) } diff --git a/wslplugins-rs/Cargo.toml b/wslplugins-rs/Cargo.toml index c7520cc..0a94472 100644 --- a/wslplugins-rs/Cargo.toml +++ b/wslplugins-rs/Cargo.toml @@ -7,11 +7,9 @@ repository.workspace = true description.workspace = true edition = "2021" -[dependencies.windows] -workspace = true -features = ["Win32_Foundation", "Win32_System", "Win32_Networking_WinSock"] [dependencies] +windows-core = "0.61.2" bitflags = { version = ">0.1.0", optional = true } enumflags2 = { version = ">0.5", optional = true } flagset = { version = ">0.1.0", optional = true } diff --git a/wslplugins-rs/src/api/api_v1.rs b/wslplugins-rs/src/api/api_v1.rs index 8d8df19..bd2a433 100644 --- a/wslplugins-rs/src/api/api_v1.rs +++ b/wslplugins-rs/src/api/api_v1.rs @@ -18,8 +18,8 @@ use std::os::windows::raw::SOCKET; use std::path::Path; use typed_path::Utf8UnixPath; use widestring::U16CString; -use windows::core::{Result as WinResult, BOOL, GUID, PCSTR, PCWSTR}; -use windows::Win32::Networking::WinSock::SOCKET as WinSocket; +use windows_core::{Result as WinResult, GUID, HRESULT}; +use wslpluginapi_sys::windows_sys::Win32::Networking::WinSock::SOCKET as WinSocket; use wslpluginapi_sys::WSLPluginAPIV1; @@ -104,13 +104,13 @@ impl ApiV1 { let result = unsafe { self.0.MountFolder.unwrap_unchecked()( session.id(), - PCWSTR::from_raw(encoded_windows_path.as_ptr()), - PCWSTR::from_raw(encoded_linux_path.as_ptr()), - BOOL::from(read_only), - PCWSTR::from_raw(encoded_name.as_ptr()), + encoded_windows_path.as_ptr(), + encoded_linux_path.as_ptr(), + read_only as i32, + encoded_name.as_ptr(), ) }; - result.ok() + HRESULT(result).ok() } /// Execute a program in the root namespace. @@ -129,7 +129,7 @@ impl ApiV1 { /// - **Standard Output**: Data output by the process will be readable from the stream. /// /// # Errors - /// This method can return the following a [`windows::core::Error`]: If the underlying Windows API call fails. + /// This method can return the following a [windows_core::Error]: If the underlying Windows API call fails. /// /// # Example /// ```rust,ignore @@ -162,23 +162,23 @@ impl ApiV1 { .iter() .map(|&arg| CString::from_str_truncate(arg)) .collect(); - let mut args_ptrs: Vec = c_args + let mut args_ptrs: Vec<*const u8> = c_args .iter() - .map(|arg| PCSTR::from_raw(arg.as_ptr() as *const u8)) - .chain(Some(PCSTR::null())) + .map(|arg| arg.as_ptr() as *const u8) + .chain(once(std::ptr::null::())) .collect(); let args_ptr = args_ptrs.as_mut_ptr(); let mut socket = MaybeUninit::::uninit(); let stream = unsafe { - self.0.ExecuteBinary.unwrap_unchecked()( + HRESULT(self.0.ExecuteBinary.unwrap_unchecked()( session.id(), - PCSTR::from_raw(c_path.as_ptr()), + c_path.as_ptr(), args_ptr, socket.as_mut_ptr(), - ) + )) .ok()?; let socket = socket.assume_init(); - TcpStream::from_raw_socket(socket.0 as SOCKET) + TcpStream::from_raw_socket(socket as SOCKET) }; Ok(stream) } @@ -187,9 +187,7 @@ impl ApiV1 { #[cfg_attr(feature = "log-instrument", instrument)] pub(crate) fn plugin_error(&self, error: &OsStr) -> WinResult<()> { let error_utf16 = U16CString::from_os_str_truncate(error); - unsafe { - self.0.PluginError.unwrap_unchecked()(PCWSTR::from_raw(error_utf16.as_ptr())).ok() - } + HRESULT(unsafe { self.0.PluginError.unwrap_unchecked()(error_utf16.as_ptr()) }).ok() } /// Execute a program in a user distribution @@ -241,29 +239,30 @@ impl ApiV1 { .copied() .chain(once(0)) .collect(); - let path_ptr = PCSTR::from_raw(c_path.as_ptr()); + let path_ptr = c_path.as_ptr(); let c_args: Vec = args .iter() .map(|&arg| CString::from_str_truncate(arg)) .collect(); - let mut args_ptrs: Vec = c_args + let mut args_ptrs: Vec<_> = c_args .iter() - .map(|arg| PCSTR::from_raw(arg.as_ptr() as *const u8)) - .chain(Some(PCSTR::null())) + .map(|arg| arg.as_ptr() as *const u8) + .chain(once(std::ptr::null())) .collect(); let args_ptr = args_ptrs.as_mut_ptr(); let mut socket = MaybeUninit::::uninit(); let stream = unsafe { - self.0.ExecuteBinaryInDistribution.unwrap_unchecked()( + HRESULT(self.0.ExecuteBinaryInDistribution.unwrap_unchecked()( session.id(), - &distribution_id, + (&distribution_id) as *const GUID + as *const wslpluginapi_sys::windows_sys::core::GUID, path_ptr, args_ptr, socket.as_mut_ptr(), - ) + )) .ok()?; let socket = socket.assume_init(); - TcpStream::from_raw_socket(socket.0 as SOCKET) + TcpStream::from_raw_socket(socket as SOCKET) }; Ok(stream) } diff --git a/wslplugins-rs/src/api/errors.rs b/wslplugins-rs/src/api/errors.rs index c2a6eee..eb21e36 100644 --- a/wslplugins-rs/src/api/errors.rs +++ b/wslplugins-rs/src/api/errors.rs @@ -7,7 +7,7 @@ use thiserror::Error; pub mod require_update_error; pub use require_update_error::Error as RequireUpdateError; -use windows::core::{Error as WinError, HRESULT}; +use windows_core::{Error as WinError, HRESULT}; use wslpluginapi_sys::WSL_E_PLUGIN_REQUIRES_UPDATE; /// A comprehensive error type for WSL plugins. @@ -52,7 +52,7 @@ impl From for WinError { /// A `WinError` representing the error. fn from(value: Error) -> Self { match value { - Error::RequiresUpdate { .. } => WSL_E_PLUGIN_REQUIRES_UPDATE.into(), + Error::RequiresUpdate { .. } => HRESULT(WSL_E_PLUGIN_REQUIRES_UPDATE).into(), Error::WinError(error) => error, } } diff --git a/wslplugins-rs/src/api/errors/require_update_error.rs b/wslplugins-rs/src/api/errors/require_update_error.rs index c243d47..e8ea012 100644 --- a/wslplugins-rs/src/api/errors/require_update_error.rs +++ b/wslplugins-rs/src/api/errors/require_update_error.rs @@ -6,7 +6,7 @@ use crate::WSLVersion; use thiserror::Error; -use windows::core::HRESULT; +use windows_core::HRESULT; use wslpluginapi_sys::WSL_E_PLUGIN_REQUIRES_UPDATE; /// Represents an error when the current WSL version is unsupported. @@ -51,7 +51,7 @@ impl From for HRESULT { /// # Returns /// - `[WSL_E_PLUGIN_REQUIRES_UPDATE]: Indicates the WSL version is insufficient for the plugin. fn from(_: Error) -> Self { - WSL_E_PLUGIN_REQUIRES_UPDATE + HRESULT(WSL_E_PLUGIN_REQUIRES_UPDATE) } } diff --git a/wslplugins-rs/src/core_distribution_information.rs b/wslplugins-rs/src/core_distribution_information.rs index 56c3f38..2fbd479 100644 --- a/wslplugins-rs/src/core_distribution_information.rs +++ b/wslplugins-rs/src/core_distribution_information.rs @@ -11,7 +11,7 @@ use crate::api::errors::require_update_error::Result; use std::ffi::OsString; -use windows::core::GUID; +use windows_core::GUID; /// A trait representing the core information of a WSL distribution. /// diff --git a/wslplugins-rs/src/distribution_information.rs b/wslplugins-rs/src/distribution_information.rs index d98a927..3ad801f 100644 --- a/wslplugins-rs/src/distribution_information.rs +++ b/wslplugins-rs/src/distribution_information.rs @@ -12,7 +12,6 @@ //! - Process ID (PID) of the init process (requires API version 2.0.5 or higher) //! - PID namespace -extern crate wslpluginapi_sys; #[cfg(doc)] use crate::api::errors::require_update_error::Error; use crate::api::{ @@ -24,8 +23,9 @@ use crate::WSLVersion; use std::ffi::OsString; use std::fmt::{Debug, Display}; use std::hash::Hash; +use std::mem; use std::os::windows::ffi::OsStringExt; -use windows::core::GUID; +use windows_core::{GUID, PCWSTR}; /// Represents detailed information about a WSL distribution. /// @@ -91,11 +91,11 @@ impl DistributionInformation { impl CoreDistributionInformation for DistributionInformation { fn id(&self) -> GUID { - self.0.Id + unsafe { mem::transmute_copy(&self.0.Id) } } fn name(&self) -> OsString { - unsafe { OsString::from_wide(self.0.Name.as_wide()) } + unsafe { OsString::from_wide(PCWSTR::from_raw(self.0.Name).as_wide()) } } fn package_family_name(&self) -> Option { @@ -104,7 +104,7 @@ impl CoreDistributionInformation for DistributionInformation { if ptr.is_null() { None } else { - Some(OsString::from_wide(ptr.as_wide())) + Some(OsString::from_wide(PCWSTR::from_raw(ptr).as_wide())) } } } @@ -119,7 +119,7 @@ impl CoreDistributionInformation for DistributionInformation { if ptr.is_null() { Ok(None) } else { - Ok(Some(OsString::from_wide(ptr.as_wide()))) + Ok(Some(OsString::from_wide(PCWSTR::from_raw(ptr).as_wide()))) } } } @@ -134,7 +134,7 @@ impl CoreDistributionInformation for DistributionInformation { if ptr.is_null() { Ok(None) } else { - Ok(Some(OsString::from_wide(ptr.as_wide()))) + Ok(Some(OsString::from_wide(PCWSTR::from_raw(ptr).as_wide()))) } } } @@ -159,7 +159,14 @@ impl Hash for DistributionInformation { impl Display for DistributionInformation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - unsafe { write!(f, "{:} {{{:?}}}", self.0.Name.display(), self.0.Id) } + unsafe { + write!( + f, + "{:} {{{:?}}}", + PCWSTR::from_raw(self.0.Name).display(), + self.id() + ) + } } } diff --git a/wslplugins-rs/src/lib.rs b/wslplugins-rs/src/lib.rs index f868287..370a4ed 100644 --- a/wslplugins-rs/src/lib.rs +++ b/wslplugins-rs/src/lib.rs @@ -23,7 +23,7 @@ //! ```rust //! #[cfg(feature = "macro")] //! use wslplugins_rs::{plugin::WSLPluginV1, WSLContext}; -//! use windows::core::Result as WinResult; +//! use windows_core::Result as WinResult; //! use wslplugins_rs::wsl_plugin_v1; //! pub(crate) struct MyPlugin { //! context: &'static WSLContext, @@ -42,6 +42,7 @@ pub mod api; // Internal modules for managing specific WSL features. mod core_distribution_information; pub(crate) mod cstring_ext; +pub extern crate windows_core; mod distribution_information; mod offline_distribution_information; mod utils; @@ -52,6 +53,7 @@ pub use wsl_user_configuration::WSLUserConfiguration; mod wsl_vm_creation_settings; #[cfg(doc)] use crate::plugin::WSLPluginV1; +pub extern crate typed_path; /// Tools and utilities for creating custom WSL plugins. pub mod plugin; diff --git a/wslplugins-rs/src/offline_distribution_information.rs b/wslplugins-rs/src/offline_distribution_information.rs index 4122694..c128ae8 100644 --- a/wslplugins-rs/src/offline_distribution_information.rs +++ b/wslplugins-rs/src/offline_distribution_information.rs @@ -15,9 +15,10 @@ use std::{ ffi::OsString, fmt::{Debug, Display}, hash::Hash, + mem, os::windows::ffi::OsStringExt, }; -use windows::core::GUID; +use windows_core::{GUID, PCWSTR}; /// A wrapper around `WslOfflineDistributionInformation` providing a safe interface. /// @@ -56,12 +57,12 @@ impl AsRef for wslpluginapi_sys::WslOfflineDistr impl CoreDistributionInformation for OfflineDistributionInformation { /// Retrieves the [GUID] of the offline distribution. fn id(&self) -> GUID { - self.0.Id + unsafe { mem::transmute_copy(&self.0.Id) } } /// Retrieves the name of the offline distribution as an [OsString]. fn name(&self) -> OsString { - unsafe { OsString::from_wide(self.0.Name.as_wide()) } + unsafe { OsString::from_wide(PCWSTR::from_raw(self.0.Name).as_wide()) } } /// Retrieves the package family name of the offline distribution, if available. @@ -71,7 +72,7 @@ impl CoreDistributionInformation for OfflineDistributionInformation { /// - `None`: If the package family name is null or empty. fn package_family_name(&self) -> Option { unsafe { - let ptr = self.0.PackageFamilyName; + let ptr = PCWSTR::from_raw(self.0.PackageFamilyName); if ptr.is_null() || ptr.is_empty() { None } else { @@ -86,7 +87,7 @@ impl CoreDistributionInformation for OfflineDistributionInformation { &WSLVersion::new(2, 4, 4), )?; unsafe { - let ptr = self.0.Flavor; + let ptr = PCWSTR::from_raw(self.0.Flavor); if ptr.is_null() || ptr.is_empty() { Ok(None) } else { @@ -101,7 +102,7 @@ impl CoreDistributionInformation for OfflineDistributionInformation { &WSLVersion::new(2, 4, 4), )?; unsafe { - let ptr = self.0.Flavor; + let ptr = PCWSTR::from_raw(self.0.Flavor); if ptr.is_null() || ptr.is_empty() { Ok(None) } else { @@ -130,7 +131,14 @@ impl Hash for OfflineDistributionInformation { impl Display for OfflineDistributionInformation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - unsafe { write!(f, "{:} {{{:?}}}", self.0.Name.display(), self.0.Id) } + unsafe { + write!( + f, + "{:} {{{:?}}}", + PCWSTR::from_raw(self.0.Name).display(), + self.id() + ) + } } } diff --git a/wslplugins-rs/src/plugin/error.rs b/wslplugins-rs/src/plugin/error.rs index 5cb1110..44a8ece 100644 --- a/wslplugins-rs/src/plugin/error.rs +++ b/wslplugins-rs/src/plugin/error.rs @@ -10,7 +10,7 @@ use log::debug; use std::ffi::{OsStr, OsString}; use std::num::NonZeroI32; use thiserror::Error; -use windows::core::{Error as WinError, HRESULT}; +use windows_core::{Error as WinError, HRESULT}; /// A specialized result type for operations that may return a WSL plugin error. /// diff --git a/wslplugins-rs/src/plugin/utils.rs b/wslplugins-rs/src/plugin/utils.rs index 613920e..d9509af 100644 --- a/wslplugins-rs/src/plugin/utils.rs +++ b/wslplugins-rs/src/plugin/utils.rs @@ -3,11 +3,8 @@ //! This module provides utility functions for creating WSL plugins and handling results, //! enabling smooth integration with the WSL Plugin API. -use windows::{ - core::{Error as WinError, Result as WinResult}, - Win32::Foundation::ERROR_ALREADY_INITIALIZED, -}; -use wslpluginapi_sys::WSLPluginAPIV1; +use windows_core::{Error as WinError, Result as WinResult, HRESULT}; +use wslpluginapi_sys::{windows_sys::Win32::Foundation::ERROR_ALREADY_INITIALIZED, WSLPluginAPIV1}; use crate::WSLContext; @@ -47,22 +44,26 @@ pub fn create_plugin_with_required_version( required_revision: u32, ) -> WinResult { unsafe { - wslpluginapi_sys::require_version(required_major, required_minor, required_revision, api) - .ok()?; - } - if let Some(context) = WSLContext::init(api.as_ref()) { - let plugin = T::try_new(context)?; - Ok(plugin) - } else { - Err(WinError::from(ERROR_ALREADY_INITIALIZED)) + HRESULT(wslpluginapi_sys::require_version( + required_major, + required_minor, + required_revision, + api, + )) + .ok()?; } + WSLContext::init(api.as_ref()) + .ok_or(WinError::from_hresult(HRESULT::from_win32( + ERROR_ALREADY_INITIALIZED, + ))) + .and_then(T::try_new) } /// Converts a generic `Result` using the custom `Error` type into a `WinResult`. /// /// This function simplifies the interoperability between the custom error handling /// in the WSL plugin system and the Windows error system by mapping the plugin [Error] -/// into a [windows::core::Error] using the `consume_error_message_unwrap` method. +/// into a [windows_core::Error] using the `consume_error_message_unwrap` method. /// /// # Arguments /// - `result`: A [`Result`] using the custom [Error] type defined in this crate. @@ -70,13 +71,13 @@ pub fn create_plugin_with_required_version( /// # Returns /// A `WinResult` where: /// - `Ok(value)` contains the successful result `T`. -/// - `Err(error)` contains a [windows::core::Error] converted from the plugin [Error]. +/// - `Err(error)` contains a [windows_core::Error] converted from the plugin [Error]. /// /// # Behavior /// - If the `result` is `Ok`, it is returned as-is. /// - If the `result` is `Err`, the error is consumed /// and sent to WSL and is then -/// converted into a [windows::core::Error]. +/// converted into a [windows_core::Error]. /// /// # Usage /// This utility is intended to facilitate the transition between idiomatic Rust diff --git a/wslplugins-rs/src/plugin/wsl_plugin_v1.rs b/wslplugins-rs/src/plugin/wsl_plugin_v1.rs index 0b49426..e6336ac 100644 --- a/wslplugins-rs/src/plugin/wsl_plugin_v1.rs +++ b/wslplugins-rs/src/plugin/wsl_plugin_v1.rs @@ -5,15 +5,14 @@ //! for managing the state of the WSL VM, distributions, and related settings. use super::error::Result; -use crate::WSLContext; use crate::{ distribution_information::DistributionInformation, offline_distribution_information::OfflineDistributionInformation, wsl_session_information::WSLSessionInformation, - wsl_vm_creation_settings::WSLVmCreationSettings, + wsl_vm_creation_settings::WSLVmCreationSettings, WSLContext, }; use std::marker::Sized; -use windows::core::Result as WinResult; +use windows_core::Result as WinResult; /// Trait defining synchronous notifications sent to the plugin. /// @@ -27,7 +26,7 @@ use windows::core::Result as WinResult; /// # Example /// ```rust /// use wslplugins_rs::{plugin::{WSLPluginV1, Result}, WSLContext, WSLSessionInformation, WSLVmCreationSettings}; -/// use windows::core::Result as WinResult; +/// use wslplugins_rs::windows_core::Result as WinResult; /// /// struct MyPlugin; /// diff --git a/wslplugins-rs/src/wsl_session_information.rs b/wslplugins-rs/src/wsl_session_information.rs index 85e976b..a33bb0f 100644 --- a/wslplugins-rs/src/wsl_session_information.rs +++ b/wslplugins-rs/src/wsl_session_information.rs @@ -5,9 +5,8 @@ extern crate wslpluginapi_sys; use core::hash; -use std::fmt; -use windows::Win32::Foundation::*; -use windows::Win32::Security::PSID; +use std::{fmt, os::windows::raw::HANDLE}; +use wslpluginapi_sys::windows_sys::Win32::Security::PSID; /// Represents session information for a WSL instance. /// @@ -28,7 +27,10 @@ impl WSLSessionInformation { /// /// # Returns /// A [HANDLE] representing the user token. - pub fn user_token(&self) -> HANDLE { + /// # Safety + /// This function returns a raw handle to the user token. + /// The handle should be used only during the life of the session and must not be closed + pub unsafe fn user_token(&self) -> HANDLE { self.0.UserToken } @@ -36,7 +38,10 @@ impl WSLSessionInformation { /// /// # Returns /// A [PSID] representing the user SID. - pub fn user_sid(&self) -> PSID { + /// # Safety + /// This function returns a raw pointer to the user SID. + /// This pointer should be used only during the life of the session and must not be freed or modified. + pub unsafe fn user_sid(&self) -> PSID { self.0.UserSid } }