WinRT component: How to generate a GUID? - Eternal Coding - HTML5 / Windows / Kinect / 3D development - Site Home - MSDN Blogs

WinRT component: How to generate a GUID?


 

WinRT component: How to generate a GUID?

  • Comments 2

For your Windows 8 applications, you may need to generate a unique identifier (for instance, if you want to uniquely identify your users).

To do so, here is a small C++ WinRT component (I decided not to use C# to make the component light-weighted because it is pure native code and so you do not need to load the CLR to use it).

#pragma once
#include <Objbase.h>
using namespace Platform;

namespace UrzaBox
{
    public ref class Tools sealed
    {
    public:
        Tools();

        static Platform::String^ GetNewID()
        {
            GUID result;
            HRESULT hr = CoCreateGuid(&result);

            if (SUCCEEDED(hr))
            {
                Guid gd(result);
                return gd.ToString();
            }

            throw Exception::CreateException(hr);
        }
    };
}
Leave a Comment
  • Please add 4 and 2 and type the answer here:
  • Post
  • As long as you use a "ref class", isn't the CLR loaded already ???!!

    IMHO, this is not pure native code, but C++/CLI  instead

  • This is a common mistake with the new WinRT model. C++/CX (used here) has nothing to do with CLR because it is PURE native code (ref keyword just indicate it is a WinRT commom, ie. a kind of COM component).

Page 1 of 1 (2 items)