Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the C++ version of Guid.NewGuid()?

I need to create a GUID in an unmanaged windows C++ project. I'm used to C#, where I'd use Guid.NewGuid(). What's the (unmanaged windows) C++ version?

like image 756
Simon Avatar asked Aug 25 '09 09:08

Simon


People also ask

What is GUID NewGuid () in C#?

Guid. NewGuid() initializes a new instance of the GUID class. using System; namespace GUIDTest { class MainClass { static void Main(string[] args) { System. Guid guid = System. Guid.

Is GUID unique C#?

Guids are statistically unique. The odds of two different clients generating the same Guid are infinitesimally small (assuming no bugs in the Guid generating code).

Why GUID is used in C#?

Use guids when you have multiple independent systems or clients generating ID's that need to be unique. For example, if I have 5 client apps creating and inserting transactional data into a table that has a unique constraint on the ID, then use guids.

Is GUID NewGuid cryptographically secure?

The random GUIDs you create with the Guid. NewGuid method are not known to be cryptographically secure. Thus, it's theoretically possible for a user to predict a GUID value that you generate for another user or task and use this to exploit weaknesses in your system.


2 Answers

I think CoCreateGuid is what you're after. Example:

GUID gidReference; HRESULT hCreateGuid = CoCreateGuid( &gidReference ); 
like image 146
Alan Avatar answered Sep 18 '22 15:09

Alan


UuidCreate() in Win32 API has exactly the same effect. However you need to pass an address of the variable that will receive the generated value:

UUID newId; UuidCreate( &newId ); 

I believe Guid.NewGuid() simply maps onto it inside the .NET runtime.

like image 30
sharptooth Avatar answered Sep 22 '22 15:09

sharptooth