Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Last active April 26, 2018 09:53
Show Gist options
  • Save kennykerr/cb7d7045f3762847eb0af1e433fb9a33 to your computer and use it in GitHub Desktop.
Save kennykerr/cb7d7045f3762847eb0af1e433fb9a33 to your computer and use it in GitHub Desktop.
Generates a GUID and copies it to the clipboard.
// cl /EHsc /std:c++17 /O2 gclip.cpp
// Requires VS 2017 15.6+ and the RS4 Windows SDK.
#pragma comment(lib, "windowsapp")
#pragma comment(lib, "ole32")
#include <winrt/Windows.ApplicationModel.DataTransfer.h>
#include <windows.h>
using namespace winrt;
using namespace Windows::ApplicationModel::DataTransfer;
int main()
{
init_apartment(apartment_type::single_threaded);
GUID guid{};
CoCreateGuid(&guid);
wchar_t* text{};
StringFromCLSID(guid, &text);
text[37] = 0;
DataPackage content;
content.SetText(text + 1);
Clipboard::SetContent(content);
Clipboard::Flush();
}
@kennykerr
Copy link
Author

image

@vbaderks
Copy link

No CoTaskMemFree to free text?

@kennykerr
Copy link
Author

I didn't bother because the process terminates immediately and all is torn down.

@DBJDBJ
Copy link

DBJDBJ commented Apr 26, 2018

auto [guid, guid_str] = make_guid ();

Have no RS4 enabled machine at hand, so just a chat :) If, I implement the above function, I would like to be able to "just" write:

DataPackage content; content.SetText( guid_str);

In other words what would be the preferred guid_str type returning from the make_guid ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment