I was following https://stackoverflow.com/a/57599016/8800839 to register the window to the filepicker, because I ran into the same problem.
While doing so, I get an "Access Denied" error while calling Initialize
on IInitializeWithWindow
Output right before the window closes:
[src\main.rs:22] interop.WindowHandle() = Ok(
HWND(
12259034,
),
)
[src\main.rs:23] file_picker_i.Initialize(hwnd) = Err(
Error {
code: 0x80070005,
message: Access is denied.
,
},
main.rs
:
#![allow(non_snake_case)]
use windows::core::*;
use windows::ApplicationModel::Activation::LaunchActivatedEventArgs;
use windows::ApplicationModel::Package;
use windows::Storage::Pickers::{FileOpenPicker, PickerLocationId, PickerViewMode};
use windows::UI::Xaml::*;
use windows::Win32::Foundation::HWND;
use windows::Win32::System::Com::{COINIT_MULTITHREADED, CoInitializeEx};
use windows::Win32::System::WinRT::ICoreWindowInterop;
use windows::Win32::UI::WindowsAndMessaging::{MB_ICONSTOP, MB_OK, MessageBoxW};
use windows::Win32::UI::Shell::IInitializeWithWindow;
#[implement(IApplicationOverrides)]
struct App ();
impl IApplicationOverrides_Impl for App {
fn OnLaunched(&self, _: &Option<LaunchActivatedEventArgs>) -> Result<()> {
let window = Window::Current()?;
let interop = window.CoreWindow()?.cast::<ICoreWindowInterop>()?;
let file_picker = FileOpenPicker::new()?;
let file_picker_i = file_picker.cast::<IInitializeWithWindow>()?;
unsafe {
let hwnd = dbg!(interop.WindowHandle())?;
dbg!(file_picker_i.Initialize(hwnd))
}
}
}
fn main() -> Result<()> {
unsafe {
CoInitializeEx(std::ptr::null(), COINIT_MULTITHREADED)?;
if let Err(result) = Package::Current() {
MessageBoxW(
HWND::default(),
"This sample must be registered (via register.cmd) and launched from Start.",
"Error",
MB_ICONSTOP | MB_OK,
);
return Err(result);
}
}
Application::Start(ApplicationInitializationCallback::new(move |_| {
Application::compose(App())?;
Ok(())
}))
}
Cargo.toml
:
...
[dependencies.windows]
version = "0.35.0"
features = [
"alloc",
"implement",
"ApplicationModel_Activation",
"UI_Core",
"UI_Xaml_Controls",
"UI_Xaml",
"Storage",
"Storage_Pickers",
"System",
"Win32_System_WinRT",
"Foundation_Collections",
"UI_Xaml_Controls_Primitives",
"Win32_Foundation",
"Win32_Storage_Packaging_Appx",
"Win32_System_Com",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
]
...
What is the cause of this error, and how can I overcome this error (to successfully use the FileOpenPicker)?