Reputation: 8843
I'm writing a cross-platform app, so thought I'd do the UI in XAML with C++/CX resp. Cocoa, and the core in standard C++. However, I'm having problems accessing documents.
I present a FolderPicker
and take the path and stick it in a directory_iterator
, but the directory iterator doesn't find any files, and if I call exists()
on the path, it says false
.
I've googled high and low, but everything on the net tells me that I should just have access to the files once I have the StorageFolder, and nothing refers to Standard C++17 APIs.
What do I have to do to give the standard library access to the files?
I bring up the file picker using:
FolderPicker ^picker = ref new FolderPicker;
picker->FileTypeFilter->Append( "*" );
IAsyncOperation<StorageFolder ^> ^storageFolderOp = picker->PickSingleFolderAsync();
auto asyncTask = concurrency::create_task(storageFolderOp);
asyncTask.then([this](StorageFolder ^storageFolder)
{
cout << "Picked directory: " << StdStringFromString(storageFolder->Path) << endl;
commandsPathField->Text = storageFolder->Path;
});
Code that takes this string (as a std::string
) and tries to list files in that directory:
path commandsFolderPath(inFolderPath);
if (exists(commandsFolderPath))
{
directory_iterator directoryIterator(commandsFolderPath);
for ( ; directoryIterator != directory_iterator(); ++directoryIterator )
{
const directory_entry& currFile = *directoryIterator;
if (currFile.path().filename().string().compare("data") == 0 || currFile.path().filename().string().find(".") == 0)
{
continue;
}
load_one_command_folder(currFile.path().string());
}
}
else
{
cout << "No directory " << commandsFolderPath.string() << endl;
}
And my manifest:
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
<Identity
Name="69b58249-31af-4bb3-95f4-fd339268a557"
Publisher="CN=Uli"
Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="69b58249-31af-4bb3-95f4-fd339268a557" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>
<Properties>
<DisplayName>VanguardBotGUI</DisplayName>
<PublisherDisplayName>Uli Kusterer</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="vanguardbot_win.App">
<uap:VisualElements
DisplayName="vanguardbot_win"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png"
Description="vanguardbot_win"
BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="internetClientServer" />
<rescap:Capability Name="appDiagnostics" />
</Capabilities>
</Package>
The full code is at https://github.com/uliwitness/vanguardbot in case you want to run it and step through (just make up username/password for the UI, the failure is before that point). Relevant files are windows/MainPage.xaml.cpp
(vanguardbot_win::MainPage::FolderPicker_Click
), common/vanguardbot.cpp
(vanguardbot::connect
) and vanguardbot/windows/Package.appxmanifest
. The solution is vanguardbot_win.sln
at the top level.
Upvotes: 1
Views: 613
Reputation: 12019
Unfortunately the folder returned from a FolderPicker
is a "brokered" location, meaning that all access to it happens through an out-of-process component that performs the appropriate security checks. The WinRT Storage APIs know how to deal with these brokered locations, but the standard CRT / STL functions do not (at this point in time). The libraries need to be updated to call newer Win32 APIs under-the-covers in order to correctly handle the brokered locations.
For now, you will either have to use the Windows.Storage
APIs or directly use the Win32 APIs such as FindFirstFileExFromApp
that can handle the brokered locations.
Upvotes: 2