Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Interface cast crashes on Windows 10 Builds < 20H2 #1464

Closed
qdot opened this issue Jan 26, 2022 · 8 comments
Closed

Bug: Interface cast crashes on Windows 10 Builds < 20H2 #1464

qdot opened this issue Jan 26, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@qdot
Copy link

qdot commented Jan 26, 2022

Which crate is this about?

windows

Crate version

0.28

Summary

First off: This is in 0.28, because I'm still waiting for some fixes in 0.31. If this is invalid for the new library, feel free to close and yell at me to upgrade. :)

We've been getting reports of .unwrap()'s panicking on Windows 10 builds older than 20H2 (which I realize are past lifetime, though we've got someone on an LTS complaining now too).

Specifically, this line in the generated code.

            let this =
              &::windows::Interface::cast::<IBluetoothLEAdvertisementReceivedEventArgs2>(self)
                .unwrap();

Expected behavior

Either no crashes for users on Windows 10 < 20H2, or else some sort of warning/error versus a full panic.

Actual behavior

Crashes on 20H2

Additional comments

No response

@qdot qdot added the bug Something isn't working label Jan 26, 2022
@kennykerr
Copy link
Collaborator

Whether the cast succeeds depends on whether the object implements the requested interface. The windows crate has nothing to do with that. That's up to the particular OS you're running on.

Whether it crashes is also up to you - you can handle it more gracefully by avoiding the unwrap, which is designed to panic, and instead query the Result, perhaps with something like if let Ok(value) = object.cast::<IBlaBla>() { ... }.

@qdot
Copy link
Author

qdot commented Jan 26, 2022

The code block above isn't our code. That's from the out/windows.rs file created by the windows-rs crate, hence the inability to avoid the .unwrap(). That's why I was saying this might also be invalid, because I know the code gen stuff changed in v0.29.

@qdot
Copy link
Author

qdot commented Jan 26, 2022

Here's the full context, from the generated windows.rs file:

          pub fn BluetoothAddressType(&self) -> ::windows::Result<super::BluetoothAddressType> {
            let this =
              &::windows::Interface::cast::<IBluetoothLEAdvertisementReceivedEventArgs2>(self)
                .unwrap();
            unsafe {
              let mut result__: <super::BluetoothAddressType as ::windows::Abi>::Abi =
                ::std::mem::zeroed();
              (::windows::Interface::vtable(this).6)(::windows::Abi::abi(this), &mut result__)
                .from_abi::<super::BluetoothAddressType>(result__)
            }
          }

@kennykerr
Copy link
Collaborator

Ah! 😄

Yes, that has been fixed. You should already see the fix if you switch from the build macro to the pre-generated APIs using the Devices_Bluetooth_Advertisement crate feature in the current 0.30 version.

Notably, the build macro will no longer be available in 0.31.

Sorry for the confusion.

@kennykerr
Copy link
Collaborator

kennykerr commented Jan 26, 2022

Here's what that looks like in the pre-generated bindings:

#[doc = "*Required features: 'Devices_Bluetooth_Advertisement'*"]
pub fn BluetoothAddressType(&self) -> ::windows::core::Result<super::BluetoothAddressType> {
let this = &::windows::core::Interface::cast::<IBluetoothLEAdvertisementReceivedEventArgs2>(self)?;
unsafe {
let mut result__: super::BluetoothAddressType = ::core::mem::zeroed();
(::windows::core::Interface::vtable(this).BluetoothAddressType)(::core::mem::transmute_copy(this), &mut result__).from_abi::<super::BluetoothAddressType>(result__)
}
}

@qdot
Copy link
Author

qdot commented Jan 26, 2022

Awesome, thanks! I'll go ahead and close this out then, and will get to testing against master while waiting for 0.31 ('cause I need #1438).

Thanks for answering questions this late. :)

@qdot qdot closed this as completed Jan 26, 2022
@107295472
Copy link

windows::core::Interface::cast

windows::core::Interface::cast Cast can't be found

@robmikh
Copy link
Member

robmikh commented Oct 14, 2023

You can find it in the ComInterface trait now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants