Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
1.9 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: utils.c
  4. //
  5. // History: May-00 vadimb Created.
  6. //
  7. // Desc: Utilties for creating a 64-bit key for sorting elements.
  8. //
  9. ////////////////////////////////////////////////////////////////////////////////////
  10. #define WIN
  11. #define FLAT_32
  12. #define TRUE_IF_WIN32 1
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #define _WINDOWS
  17. #include <windows.h>
  18. #include "shimdb.h"
  19. // we are in the world of nt now
  20. BOOL GUIDFromString(LPCTSTR lpszGuid, GUID* pGuid)
  21. {
  22. UNICODE_STRING ustrGuid;
  23. NTSTATUS status;
  24. // convert from ansi to unicode
  25. #ifdef _UNICODE
  26. RtlInitUnicodeString(&ustrGuid, lpszGuid);
  27. #else
  28. ANSI_STRING astrGuid;
  29. RtlInitAnsiString(&astrGuid, lpszGuid);
  30. RtlAnsiStringToUnicodeString(&ustrGuid, &astrGuid, TRUE);
  31. #endif
  32. // now convert
  33. status = RtlGUIDFromString(&ustrGuid, pGuid);
  34. #ifndef _UNICODE
  35. RtlFreeUnicodeString(&ustrGuid);
  36. #endif
  37. return NT_SUCCESS(status);
  38. }
  39. ULONGLONG ullMakeKey(LPCTSTR lpszStr)
  40. {
  41. #ifdef _UNICODE
  42. return SdbMakeIndexKeyFromString(lpszStr);
  43. #else
  44. // we are ANSI
  45. ULONGLONG ullKey;
  46. char szAnsiKey[8]; // need 8 + 1 for the zero byte
  47. char szFlippedKey[8]; // flipped to deal with little-endian issues
  48. NTSTATUS status;
  49. int i;
  50. ZeroMemory(szAnsiKey, 8);
  51. strncpy(szAnsiKey, lpszStr, 8);
  52. // flip the key
  53. for (i = 0; i < 8; ++i) {
  54. szFlippedKey[i] = szAnsiKey[7-i];
  55. }
  56. return *((ULONGLONG*)szFlippedKey);
  57. #endif
  58. }
  59. BOOL
  60. StringFromGUID(
  61. LPTSTR lpszGuid,
  62. GUID* pGuid
  63. )
  64. {
  65. UNICODE_STRING ustrGuid;
  66. NTSTATUS Status;
  67. Status = RtlStringFromGUID(pGuid, &ustrGuid);
  68. if (NT_SUCCESS(Status)) {
  69. #ifdef _UNICODE
  70. wcscpy(lpszGuid, ustrGuid.Buffer);
  71. #else
  72. sprintf(lpszGuid, "%ls", ustrGuid.Buffer);
  73. #endif
  74. RtlFreeUnicodeString(&ustrGuid);
  75. return TRUE;
  76. }
  77. return FALSE;
  78. }