diff --git a/src/os.rs b/src/os.rs index b27a01e..d46c25e 100644 --- a/src/os.rs +++ b/src/os.rs @@ -6,6 +6,7 @@ mod os_defs { }; pub use winapi::um::combaseapi::CoTaskMemFree; + pub use winapi::um::oleauto::SysFreeString; } #[cfg(not(windows))] @@ -26,6 +27,12 @@ mod os_defs { // https://github.com/microsoft/DirectXShaderCompiler/blob/a8d9780046cb64a1cea842fa6fc28a250e3e2c09/include/dxc/Support/WinAdapter.h#L46 libc::free(p) } + + #[allow(non_snake_case)] + pub unsafe fn SysFreeString(p: BSTR) { + // https://github.com/microsoft/DirectXShaderCompiler/blob/a8d9780046cb64a1cea842fa6fc28a250e3e2c09/include/dxc/Support/WinAdapter.h#L48-L50 + libc::free(p as _) + } } pub use os_defs::*; diff --git a/src/utils.rs b/src/utils.rs index e473f77..db504cd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,9 @@ -use crate::os::{BSTR, HRESULT, LPSTR, LPWSTR, WCHAR}; +use crate::os::{SysFreeString, BSTR, HRESULT, LPSTR, LPWSTR, WCHAR}; use crate::wrapper::*; use thiserror::Error; #[cfg(windows)] -use winapi::um::oleauto::{SysFreeString, SysStringLen}; +use winapi::um::oleauto::SysStringLen; pub(crate) fn to_wide(msg: &str) -> Vec { widestring::WideCString::from_str(msg).unwrap().into_vec() @@ -33,7 +33,9 @@ pub(crate) fn from_bstr(string: BSTR) -> String { // TODO (Marijn): This does NOT cover embedded NULLs: // https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-sysstringlen#remarks // Fortunately BSTRs are only used in names currently, which _likely_ don't include NULL characters (like binary data) - from_lpstr(string as LPSTR) + let result = from_lpstr(string as LPSTR); + unsafe { SysFreeString(string) }; + result } pub(crate) fn from_lpstr(string: LPSTR) -> String {