Developers.Hash.Guid.CopyIn

From Shareaza Wiki
Jump to navigation Jump to search

How to copy a GUID value into camper's GUID type

Classes camper has written wrap a GUID and provide features, but there isn't any code to actually make a GUID. Shareaza makes GUIDs itself, and then stores them in camper's GUID type.

<source lang="c"> // Make a camper guid tye, and name it g Hashes::Guid g;

// Make an interator named i that starts at the start of the GUID Hashes::Guid::iterator i = g.begin();

// Set each of the 4 DWORDs of the GUID

  • i++ += GetTickCount();
  • i++ += sequence++;
  • i++ += rand() * (RAND_MAX + 1) * (RAND_MAX + 1) + rand() * (RAND_MAX + 1) + rand();
  • i += rand() * (RAND_MAX + 1) * (RAND_MAX + 1) + rand() * (RAND_MAX + 1) + rand();

</source>

The iterator, named i, is really just a pointer to the start of the memory of the GUID. The word type for GUIDs is DWORD, so i++ moves it forward 4 bytes.

There are a lot of ways to set the 16 bytes of the GUID directly. Here are 3 more.

<source lang="c"> // Have Windwos make a GUID in its own GUID type GUID guid; CoCreateGuid(&guid);

// Make a new camper GUID from the Windows one Hashes::Guid g( reinterpret_cast< Hashes::Guid::RawStorage* >( guid ) );

// Make a new camper GUID and copy the memory of the Windows one into it Hashes::Guid g2; memcpy( &g2[0], &guid, sizeof guid ); g2.validate();

// Make a new camper GUID and copy the 4 DWORDs of the GUID into it Hashes::Guid g3; std::copy( &guid.Data1, &guid.Data1 + 4, g3.begin() ); g3.validate(); </source>