Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. private static Guid NewSequentialGuid()
  2. {
  3. const int S_OK = 0;
  4. const int RPC_S_UUID_LOCAL_ONLY = 1824;
  5.  
  6. Guid oldGuid = Guid.Empty;
  7.  
  8. int result = UuidCreateSequential(ref oldGuid);
  9. if (result != S_OK && result != RPC_S_UUID_LOCAL_ONLY)
  10. {
  11. throw new ExternalException("UuidCreateSequential call failed", result);
  12. }
  13.  
  14. byte[] oldGuidBytes = oldGuid.ToByteArray();
  15. byte[] newGuidBytes = new byte[16];
  16. oldGuidBytes.CopyTo(newGuidBytes, 0);
  17.  
  18. // swap low timestamp bytes (0-3)
  19. newGuidBytes[0] = oldGuidBytes[3];
  20. newGuidBytes[1] = oldGuidBytes[2];
  21. newGuidBytes[2] = oldGuidBytes[1];
  22. newGuidBytes[3] = oldGuidBytes[0];
  23.  
  24. // swap middle timestamp bytes (4-5)
  25. newGuidBytes[4] = oldGuidBytes[5];
  26. newGuidBytes[5] = oldGuidBytes[4];
  27.  
  28. // swap high timestamp bytes (6-7)
  29. newGuidBytes[6] = oldGuidBytes[7];
  30. newGuidBytes[7] = oldGuidBytes[6];
  31.  
  32. //remaining 8 bytes are unchanged (8-15)
  33.  
  34. return new Guid(newGuidBytes);
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement