使用hwnd初始化fileopenpicker时访问被拒绝

发布于 2025-01-22 19:34:12 字数 2940 浏览 0 评论 0原文

我正在关注 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文