Leaked source code of windows server 2003
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.

178 lines
3.6 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. util.c
  5. Abstract:
  6. This module contains the following private utility routines for the Plug and
  7. Play registry merge-restore routines:
  8. FileExists
  9. Author:
  10. Jim Cavalaris (jamesca) 2-10-2000
  11. Environment:
  12. User-mode only.
  13. Revision History:
  14. 10-February-2000 jamesca
  15. Creation and initial implementation.
  16. --*/
  17. //
  18. // includes
  19. //
  20. #include "precomp.h"
  21. //
  22. // definitions
  23. //
  24. #define MAX_GUID_STRING_LEN 39 // 38 chars + terminating NULL
  25. //
  26. // Declare data used in GUID->string conversion (from ole32\common\ccompapi.cxx).
  27. //
  28. static const BYTE GuidMap[] = { 3, 2, 1, 0, '-', 5, 4, '-', 7, 6, '-',
  29. 8, 9, '-', 10, 11, 12, 13, 14, 15 };
  30. static const TCHAR szDigits[] = TEXT("0123456789ABCDEF");
  31. //
  32. // routines
  33. //
  34. BOOL
  35. pSifUtilFileExists(
  36. IN PCTSTR FileName,
  37. OUT PWIN32_FIND_DATA FindData OPTIONAL
  38. )
  39. /*++
  40. Routine Description:
  41. Determine if a file exists and is accessible.
  42. Errormode is set (and then restored) so the user will not see
  43. any pop-ups.
  44. Arguments:
  45. FileName - supplies full path of file to check for existance.
  46. FindData - if specified, receives find data for the file.
  47. Return Value:
  48. TRUE if the file exists and is accessible.
  49. FALSE if not. GetLastError() returns extended error info.
  50. --*/
  51. {
  52. WIN32_FIND_DATA findData;
  53. HANDLE FindHandle;
  54. UINT OldMode;
  55. DWORD Error;
  56. OldMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  57. FindHandle = FindFirstFile(FileName,&findData);
  58. if(FindHandle == INVALID_HANDLE_VALUE) {
  59. Error = GetLastError();
  60. } else {
  61. FindClose(FindHandle);
  62. if(FindData) {
  63. *FindData = findData;
  64. }
  65. Error = NO_ERROR;
  66. }
  67. SetErrorMode(OldMode);
  68. SetLastError(Error);
  69. return (Error == NO_ERROR);
  70. }
  71. BOOL
  72. pSifUtilStringFromGuid(
  73. IN CONST GUID *Guid,
  74. OUT PTSTR GuidString,
  75. IN DWORD GuidStringSize
  76. )
  77. /*++
  78. Routine Description:
  79. This routine converts a GUID into a null-terminated string which represents
  80. it. This string is of the form:
  81. {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
  82. where x represents a hexadecimal digit.
  83. This routine comes from ole32\common\ccompapi.cxx. It is included here to avoid linking
  84. to ole32.dll. (The RPC version allocates memory, so it was avoided as well.)
  85. Arguments:
  86. Guid - Supplies a pointer to the GUID whose string representation is
  87. to be retrieved.
  88. GuidString - Supplies a pointer to character buffer that receives the
  89. string. This buffer must be _at least_ 39 (MAX_GUID_STRING_LEN) characters
  90. long.
  91. Return Value:
  92. Returns TRUE if successful, FALSE if not.
  93. GetLastError() returns extended error info.
  94. --*/
  95. {
  96. CONST BYTE *GuidBytes;
  97. INT i;
  98. if(GuidStringSize < MAX_GUID_STRING_LEN) {
  99. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  100. return FALSE;
  101. }
  102. GuidBytes = (CONST BYTE *)Guid;
  103. *GuidString++ = TEXT('{');
  104. for(i = 0; i < sizeof(GuidMap); i++) {
  105. if(GuidMap[i] == '-') {
  106. *GuidString++ = TEXT('-');
  107. } else {
  108. *GuidString++ = szDigits[ (GuidBytes[GuidMap[i]] & 0xF0) >> 4 ];
  109. *GuidString++ = szDigits[ (GuidBytes[GuidMap[i]] & 0x0F) ];
  110. }
  111. }
  112. *GuidString++ = TEXT('}');
  113. *GuidString = TEXT('\0');
  114. return TRUE;
  115. }
  116.