0

I made a C++/WinRT module which implements Windows.Storage.Pickers.FileOpenPicker and FileSavePicker to React Native for Windows. This is my FilePicker.h:

#pragma once

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Pickers.h>

#include "pch.h"
#include "JSValue.h"
#include "NativeModules.h"

using namespace std;
using namespace winrt;
using namespace winrt::Microsoft::ReactNative;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;
using namespace Windows::UI::Xaml;

namespace FilePicker
{
    REACT_MODULE(Panel);
    struct Panel
    {
        REACT_METHOD(Open, L"open");
        fire_and_forget Open(wchar_t ext[], React::ReactPromise<string> promise) noexcept
        {
            wchar_t str[32] = L".";
            wcscat_s(str, 32, ext);
            
            FileOpenPicker openPicker;
            openPicker.ViewMode(PickerViewMode::List);
            openPicker.SuggestedStartLocation(PickerLocationId::DocumentsLibrary);
            openPicker.FileTypeFilter().ReplaceAll({ str });
            
            StorageFile file = co_await openPicker.PickSingleFileAsync();
            if (file == nullptr) {
                promise.Reject("No file selected.");
            } else {
                promise.Resolve(to_string(file.Path()));
            }
        }

        REACT_METHOD(Save, L"save");
        fire_and_forget Save(wchar_t ext[], wchar_t content[]) noexcept
        {
            wchar_t str[32] = L".";
            wcscat_s(str, 32, ext);
            
            FileSavePicker savePicker;
            savePicker.SuggestedStartLocation(PickerLocationId::DocumentsLibrary);
            savePicker.FileTypeChoices().Insert(L"", single_threaded_vector<hstring>({ str }));

            StorageFile file = co_await savePicker.PickSaveFileAsync();
            await FileIO::WriteTextAsync(file, content);
        }
    };
}

and I linked it to testing RNW project, which App.tsx is this:

import React from 'react';
import { Button, Text, View } from 'react-native';
import { open, save } from 'react-native-file-panel';

export default class App extends React.Component<undefined, { text: string }> {
  constructor(props: undefined) {
    super(props);

    this.state = { text: '' };
  }
  render() {
    return (
      <View>
        <Button title='SAVE' onPress={ () => save('txt', 'The quick fox jumps over the lazy dog.') } />
        <Button title='OPEN' onPress={ () => open('txt').then((content: string) => this.setState({ text: content })) } />
        <Text>{ this.state.text }</Text>
      </View>
    );
  }
}

But when I ran it, this error occurs:

error C2338: Coroutine parameter must be passed by value for safe access: void __cdecl winrt::Microsoft::ReactNative::ValidateCoroutineArg<struct winrt::fire_and_forget,wchar_t*>(void) noexcept

I tried to modify, and googled, nothing was helped. Anyone knows the solution?

Jihoo Byeon
  • 69
  • 1
  • 8
  • 1
    Is this a runtime error or a compile time error? I'm guessing this is a compiler error, raised for the `Open` method. It's first argument is a `wchar_t*`. You will want to pass an `hstring` by value instead. (Same for the `Save` method.) – IInspectable Oct 09 '22 at 06:50
  • @IInspectable yes, it is a compile time error. I tried modifying `wchar_t []` to `hstring const&`, but same error has thrown. – Jihoo Byeon Oct 09 '22 at 10:50
  • 1
    *"Pass an `hstring` **by value**."* – IInspectable Oct 09 '22 at 10:58
  • @IInspectable yes, I am compling with `hstring` now. `hstring const&` was what I tried before. – Jihoo Byeon Oct 09 '22 at 11:05
  • Does that fix the error? (Note that you will have to apply the same changes to the `Save` method.) – IInspectable Oct 09 '22 at 11:09
  • @IInspectable yes, but now I have new error: `C:\Users\username\Desktop\foo\windows\foo\pch.h(25,10): fatal error C1083: Cannot open include file. 'winrt/FilePicker.h': No such file or directory` this more seems like problem of React Native, so the original problem seems to be fixed. Thanks! – Jihoo Byeon Oct 09 '22 at 11:17

0 Answers0