0

I'm looking for the interfaces to allow me to access the clipboard in native c++ metro app - similar to DataTransfer::Clipboard::SetContent in C#.

can someone please refer me to those interfaces and how can it be done using the WRL library?

Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22
elad-ep
  • 333
  • 1
  • 9

1 Answers1

1

In WRL, you need to get the IClipboardStatics interface which contains the SetContent method.

#include <Windows.Foundation.h>
#include <Windows.ApplicationModel.DataTransfer.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>

using namespace ABI::Windows::Foundation;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::ApplicationModel::DataTransfer;

    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);

    if (FAILED(initialize))
    {
        cout << "Failed to initialize";
    }

    ComPtr<IClipboardStatics> clipboard;

    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_ApplicationModel_DataTransfer_Clipboard).Get(), &clipboard);

    if (FAILED(hr))
    {
        cout << "failed to create a runtime instance";

        return 0;
    }

    ComPtr<IDataPackage> datapackage;

    // create a package and set the data
    // ...

    clipboard->SetContent(datapackage.Get());
Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22
  • thanks! is there any documentation for those interfaces? where did you find? – elad-ep Aug 17 '15 at 11:46
  • No document, I found it in the c++ header file Windows.ApplicationModel.DataTransfer.h – Jeffrey Chen Aug 17 '15 at 23:51
  • The above GetActivationFactory fails with the following error: "Activating a single-threaded class from MTA is not supported". only when I call RoInitialize with RO_INIT_SINGLETHREADED it works. but it doesn't make sense - I attached a debugger to a few Windows store apps to see all their calls to RoInitialize and it's always with RO_INIT_MULTITHREADED. so how they manage to perform copy/paste, why the underlying implementation in the Windows Runtime succeeds where I fail? – elad-ep Aug 24 '15 at 15:10