Jihoo Byeon
Jihoo Byeon

Reputation: 89

"Coroutine parameter must be passed by value for safe access" how can I do it?

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?

Upvotes: 0

Views: 58

Answers (0)

Related Questions