react-native-clipboard / clipboard

React Native Clipboard API for both iOS and Android.
MIT License
690 stars 142 forks source link

Calling Clipboard.setString on Windows crashes with 'CoInitialize has not been called.' #173

Closed chrisglein closed 1 year ago

chrisglein commented 1 year ago

Environment

info Fetching system and libraries information...
System:
    OS: Windows 10 10.0.23405
    CPU: (24) x64 AMD Ryzen Threadripper PRO 3945WX 12-Cores
    Memory: 40.24 GB / 63.86 GB
  Binaries:
    Node: 16.17.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 9.4.1 - C:\Program Files\nodejs\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK: Not Found
    Windows SDK:
      AllowDevelopmentWithoutDevLicense: Enabled
      AllowAllTrustedApps: Enabled
      Versions: 10.0.18362.0, 10.0.19041.0, 10.0.22000.0, 10.0.22621.0
  IDEs:
    Android Studio: Not Found
    Visual Studio: 17.4.33403.182 (Visual Studio Community 2022), 16.11.33214.272 (Visual Studio Community 2019)
  Languages:
    Java: Not Found
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.2.0 => 18.2.0
    react-native: 0.71.0 => 0.71.0
    react-native-windows: 0.71.1 => 0.71.1
  npmGlobalPackages:
    *react-native*: Not Found

Platforms

Windows

Versions

Description

In a React Native for Windows app, use Clipboard.setString, and experience this crash:

'C:\Windows\System32\windows.applicationmodel.datatransfer.dll'.onecore\com\combase\objact\dllcache.cxx(3983)\combase.dll!00007FFA0381D764: (caller: 00007FFA0381C410) ReturnHr(2) tid(7d08) 8000001D Activating a single-threaded class from MTA is not supportedException thrown at 0x00007FFA029B057C (KernelBase.dll) in redacted.exe: WinRT originate error - 0x800401F0 : 'CoInitialize has not been called.'.Exception thrown at 0x00007FFA029B057C in redacted.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x0000002945A9E268.

The cause? In version 0.64 the threading assumptions changed and the way to handle this is described here.

I have a local fix verified and will be creating a PR to address this.

Reproducible Demo

import Clipboard from '@react-native-clipboard/clipboard';

<Button  title="📋 Copy"
  onPress={() => {
    Clipboard.setString(content);
  }}/>