Problem whlie Drag and Drop in c++/winrt
I have made a WinUI3 application using c++. I have one filepicker and one drag and drop region in my application which picks and drags images respectively. While I am picking the images it is able to pick the file count the number of images and then I have a button , on click of that button it is getting stored in the local file. But when I am dragging and dropping the images it is able to count the number of images dragged and dropped but when I am clicking the button to save it throws an exception in output: Exception thrown at 0x00007FF9A0EACF19 in App2.exe: Microsoft C++ exception: winrt::hresult_invalid_argument at memory location 0x0000008DB3D9BF28.
so I try to apply try and catch block for it and i got the error message as : Error in SaveLocalFileAsync: The specified token ({244C833C-9A76-49C7-BC0A-EC2FFFF40A3A}) is not associated with an item in the access list.onecore\com\combase\dcomrem\preventrundownbias.cpp(1310)\combase.dll!00007FF9A1EBBF8C: (caller: 00007FF9A1EBBDB9) LogHr(2) tid(295c) 80070005 Access is denied.
But it is working fine in the case of filepicker only showing this exception while i dragging and dropping the images.
Any pointer on this, would be appreciated.
Windows App SDK
C++
XAML
-
Jeanine Zhang-MSFT 9,351 Reputation points • Microsoft Vendor
2023-09-01T01:13:43.58+00:00 Could you please tell us what's the
SaveLocalFileAsync
? And could you please provide a minimum sample to help us reproduce the issue? -
Vivek Rao 20 Reputation points
2023-09-01T05:04:36.4233333+00:00 SaveLocalFileAsync is method in which I have written my logic to save the images .
I have made a drag and drop logic and then calling CountFileAsync method to count the number of images upto that it is working fine but after that it is not able to save the images saying the exception that I mentioned earlier
-
Jeanine Zhang-MSFT 9,351 Reputation points • Microsoft Vendor
2023-09-01T06:32:55.8533333+00:00 Could you please provide a complete reproducible sample in the blank project? And provide the detailed steps to help us reproduce the issue. Where do you want to drag and drop the image? Drag and drop is an intuitive way to transfer data within an application or between applications on the Windows desktop. In my opinion, once you've dragged and dropped the image to the path you want, you don't need to press the save button. Why you want to save the image after drag and drop the image?
-
Vivek Rao 20 Reputation points
2023-09-01T06:37:43.42+00:00 I want to save the images which i have dragged locally in the user picture library. To perform other operations on those images I have to save it.
-
Jeanine Zhang-MSFT 9,351 Reputation points • Microsoft Vendor
2023-09-01T06:54:01.5133333+00:00 Could you please provide a complete reproducible sample in the blank project? And provide the detailed steps to help us reproduce the issue.
-
Vivek Rao 20 Reputation points
2023-09-01T07:07:12.9+00:00 ok i will try to make a blank project and I will provide it here
-
Vivek Rao 20 Reputation points
2023-09-01T09:52:12.93+00:00 In MainWindow.xaml I have one grid that accepts drag and drop of images for which I have written logic in MainWindow.xaml.cpp . After that it stores all the images in a vector named as ImageFiles and it calls a function to counts the number of images. Then there is textbox in the app which takes the input from there and after clicking save image button it supposed to create a folder with the name you give in the textbox in the pictureLibrary
and save all the images in the given folder. It is able to count the images and display it. It is able to create the folder in the pictureLibrary but the images are not getting stored into the folder. It shows exception in the output Exception thrown at 0x00007FF9A0EACF19 in App7.exe: Microsoft C++ exception: winrt::hresult_invalid_argument at memory location 0x0000008DB3D9BF28.
MainWindow.xaml
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="App7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App7" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid x:Name="grid1" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Top"> <Border BorderBrush="Black" BorderThickness="1" > <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <TextBlock Height="100" Width="800" x:Name="fileName1" Margin="15,5,0,0" Foreground="Aqua" Visibility="Collapsed" /> </ScrollViewer> </Border> <TextBox x:Name="folderNameTextBox" PlaceholderText="Enter folder name" /> <Button Click="SaveImagesButton_Click" Content="Save Images" Margin="10" /> <Grid Height="Auto" MinWidth="600" Margin="0, 12, 0,10" Background="Transparent" AllowDrop="True" Drop="FileDrop" DragOver="DragOver" > <Rectangle Margin="0,0,0,25" Width="Auto" Height="100" RadiusX="5" RadiusY="5" Stroke="{ThemeResource SystemColorActiveCaptionColor }" StrokeDashArray="8,2"/> </Grid> </StackPanel> </Grid> </Window>
MainWindow.xaml.cpp
// To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. namespace winrt::App7::implementation { MainWindow::MainWindow() { InitializeComponent(); } int32_t MainWindow::MyProperty() { throw hresult_not_implemented(); } void MainWindow::MyProperty(int32_t /* value */) { throw hresult_not_implemented(); } void MainWindow::SaveImagesButton_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e) { SaveLocalFileAsync(); // Wait for the saving to complete } } void MainWindow::FileDrop(Windows::Foundation::IInspectable const& /*sender*/, Microsoft::UI::Xaml::DragEventArgs const& e) { FileDropAsync(e); } void MainWindow::DragOver(Windows::Foundation::IInspectable const& /*sender*/, Microsoft::UI::Xaml::DragEventArgs const& e) { if (e.DataView().Contains(winrt::Windows::ApplicationModel::DataTransfer::StandardDataFormats::StorageItems())) { auto deferral = e.GetDeferral(); // Get a deferral to handle the drag event asynchronously e.AcceptedOperation(winrt::Windows::ApplicationModel::DataTransfer::DataPackageOperation::Copy); // Allow copying the dropped item deferral.Complete(); // Complete the deferral to finish handling the event } else { e.AcceptedOperation(winrt::Windows::ApplicationModel::DataTransfer::DataPackageOperation::None); // Don't allow dropping other items } } Windows::Foundation::IAsyncAction MainWindow::FileDropAsync(Microsoft::UI::Xaml::DragEventArgs const& e) { if (e.DataView().Contains(Windows::ApplicationModel::DataTransfer::StandardDataFormats::StorageItems())) { auto items = co_await e.DataView().GetStorageItemsAsync(); for (const auto& item : items) { if (item.IsOfType(Windows::Storage::StorageItemTypes::File)) { auto storageFile = item.as<Windows::Storage::StorageFile>(); hstring ext = storageFile.FileType(); if (ext == L".png" || ext == L".PNG" || ext == L".jpg" || ext == L".JPG" || ext == L".jpeg" || ext == L".JPEG") { ImageFiles.push_back(storageFile); } else { // Handle non-image files } } } co_await CountFileAsync(); //then it is calling to it co_return; } } }
MainWindow.xaml.h
// Copyright (c) Microsoft Corporation and Contributors. // Licensed under the MIT License. #pragma once #include "MainWindow.g.h" namespace winrt::App7::implementation { struct MainWindow : MainWindowT<MainWindow> { MainWindow(); int32_t MyProperty(); void MyProperty(int32_t value); Windows::Foundation::IAsyncAction CountFileAsync(); void FileDrop(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::DragEventArgs const& e); void DragOver(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::DragEventArgs const& e); private: std::vector<winrt::Windows::Storage::StorageFile> selectedFiles; private: Windows::Foundation::IAsyncAction SaveLocalFileAsync(); Windows::Foundation::IAsyncAction FileDropAsync(Microsoft::UI::Xaml::DragEventArgs const& e); public: void SaveImagesButton_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); }; } namespace winrt::App7::factory_implementation { struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow> { }; }
-
Jeanine Zhang-MSFT 9,351 Reputation points • Microsoft Vendor
2023-09-04T05:47:33.04+00:00 I tested the code you provided, however there no errors were reproduced.
I put the images into the grid by dragging and dropping, the number of images is displayed correctly.
And I enter
p
in the textbox, then click theSave images
button.A folder named
P
is created in the pictureLibrary. The images are also successfully saved in this folder.If there are the problems with my reproduction steps, please let me know.
-
Vivek Rao 20 Reputation points
2023-09-04T06:21:37.2866667+00:00 yes you are doing it correctly but same is not working for me when I am clicking on the save image button folder s getting created but images are not getting saved. what can be the reason for that?
-
Jeanine Zhang-MSFT 9,351 Reputation points • Microsoft Vendor
2023-09-04T08:17:39.2366667+00:00 Could you please tell us which version of the windows and Windows App SDK you are using?
-
Vivek Rao 20 Reputation points
2023-09-04T09:41:13.09+00:00 Windows version is 10 and WindowsAppSdk version is 1.4.230822000
-
Jeanine Zhang-MSFT 9,351 Reputation points • Microsoft Vendor
2023-09-05T02:06:25.9566667+00:00 I'm using the version 1.3 of the Windows App SDK. I suggest you could try to go back to version 1.3 and check if you still have such the issue.
Sign in to comment