使用hwnd初始化fileopenpicker时访问被拒绝
我正在关注 https://stackoverflow.com/a/57599016/8800839 为我注册窗口遇到了同样的问题。 在这样做的过程中,我在调用初始化
on iinitializewithwindow
in
in 在窗口关闭之前的输出:
[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.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",
]
...
此错误的原因是什么?如何克服此错误(成功使用FileOpenpicker)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论