Skip to content

Instantly share code, notes, and snippets.

@manassra
Created May 27, 2015 19:09
Show Gist options
  • Save manassra/28c3d9270b18dbfcf4ad to your computer and use it in GitHub Desktop.
Save manassra/28c3d9270b18dbfcf4ad to your computer and use it in GitHub Desktop.

Revisions

  1. manassra created this gist May 27, 2015.
    28 changes: 28 additions & 0 deletions guid.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /////////////////////////////////////////////
    /// using CoCreateGuid ///
    ///////////////////////////////////////////
    GUID guid;
    std::string guidString;
    HRESULT hr = CoCreateGuid(&guid);
    if (SUCCEEDED(hr))
    {
    stringstream stream;
    stream << std::hex << std::uppercase
    << std::setw(8) << std::setfill('0') << guid.Data1
    << "-" << std::setw(4) << std::setfill('0') << guid.Data2
    << "-" << std::setw(4) << std::setfill('0') << guid.Data3
    << "-";
    for (int i = 0; i < sizeof(guid.Data4); ++i)
    {
    if (i == 2)
    stream << "-";
    stream << std::hex << std::setw(2) << std::setfill('0') << (int)guid.Data4[i];
    }
    guidString = stream.str();
    }

    /////////////////////////////////////////////
    /// using boost ///
    ///////////////////////////////////////////
    boost::uuids::uuid guid = boost::uuids::random_generator()();
    std::string guidString = boost::lexical_cast<string>(guid);