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.

170 lines
5.0 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // General utility routines.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000-2001.
  6. //
  7. //----------------------------------------------------------------------------
  8. #ifndef __CMNUTIL_HPP__
  9. #define __CMNUTIL_HPP__
  10. #define DIMAT(Array, EltType) (sizeof(Array) / sizeof(EltType))
  11. #define DIMA(Array) DIMAT(Array, (Array)[0])
  12. #define CSRSS_PROCESS_ID ((ULONG)-1)
  13. // Converts an NTSTATUS into an HRESULT with success check.
  14. #define CONV_NT_STATUS(Status) \
  15. (NT_SUCCESS(Status) ? S_OK : HRESULT_FROM_NT(Status))
  16. // Converts a Win32 status into an HRESULT with a guarantee it's
  17. // an error code. This avoids problems with routines which
  18. // don't set a last error.
  19. #define WIN32_STATUS(Err) ((Err) == 0 ? E_FAIL : HRESULT_FROM_WIN32(Err))
  20. #define WIN32_LAST_STATUS() WIN32_STATUS(GetLastError())
  21. // Converts a BOOL into an HRESULT with success check.
  22. #define CONV_W32_STATUS(Status) \
  23. ((Status) ? S_OK : WIN32_LAST_STATUS())
  24. //----------------------------------------------------------------------------
  25. //
  26. // Assertions.
  27. //
  28. //----------------------------------------------------------------------------
  29. #if DBG
  30. void DbgAssertionFailed(PCSTR File, int Line, PCSTR Str);
  31. #define DBG_ASSERT(Expr) \
  32. if (!(Expr)) \
  33. { \
  34. DbgAssertionFailed(__FILE__, __LINE__, #Expr); \
  35. } \
  36. else 0
  37. #else
  38. #define DBG_ASSERT(Expr)
  39. #endif
  40. //----------------------------------------------------------------------------
  41. //
  42. // COM help.
  43. //
  44. //----------------------------------------------------------------------------
  45. // Wrapper that can be locally implemented if necessary to
  46. // remove usage of ole32.dll on platforms where IsEqualIID
  47. // is not inlined.
  48. #define DbgIsEqualIID(Id1, Id2) \
  49. IsEqualIID(Id1, Id2)
  50. // Safely releases and NULLs an interface pointer.
  51. #define RELEASE(pUnk) \
  52. ((pUnk) != NULL ? ((pUnk)->Release(), (pUnk) = NULL) : NULL)
  53. // Transfers an interface pointer into a holder that may or
  54. // may not already hold and interface.
  55. #define TRANSFER(pOld, pNew) \
  56. (((pNew) != NULL ? (pNew)->AddRef() : 0), \
  57. ((pOld) != NULL ? (pOld)->Release() : 0), \
  58. (pOld) = (pNew))
  59. //----------------------------------------------------------------------------
  60. //
  61. // Utility functions.
  62. //
  63. //----------------------------------------------------------------------------
  64. extern PSECURITY_DESCRIPTOR g_AllAccessSecDesc;
  65. extern SECURITY_ATTRIBUTES g_AllAccessSecAttr;
  66. PSTR FormatStatusCode(HRESULT Status);
  67. #define FormatStatus(Status) FormatStatusArgs(Status, NULL)
  68. PSTR FormatStatusArgs(HRESULT Status, PVOID Arguments);
  69. BOOL InstallAsAeDebug(PCSTR Append);
  70. HANDLE CreatePidEvent(ULONG Pid, ULONG CreateOrOpen);
  71. BOOL SetPidEvent(ULONG Pid, ULONG CreateOrOpen);
  72. HRESULT EnableDebugPrivilege(void);
  73. HRESULT FillDataBuffer(PVOID Data, ULONG DataLen,
  74. PVOID Buffer, ULONG BufferLen, PULONG BufferUsed);
  75. HRESULT FillStringBuffer(PCSTR String, ULONG StringLenIn,
  76. PSTR Buffer, ULONG BufferLen, PULONG StringLenOut);
  77. HRESULT AppendToStringBuffer(HRESULT Status, PCSTR String, BOOL First,
  78. PSTR* Buffer, ULONG* BufferLen, PULONG LenOut);
  79. void Win32ToNtTimeout(ULONG Win32Timeout, PLARGE_INTEGER NtTimeout);
  80. HRESULT InitializeAllAccessSecObj(void);
  81. void DeleteAllAccessSecObj(void);
  82. #define BASE_YEAR_ADJUSTMENT 11644473600
  83. // Convert to seconds and then from base year 1601 to base year 1970.
  84. #define FileTimeToTimeDateStamp(FileTime) \
  85. (ULONG)(((FileTime) / 10000000) - BASE_YEAR_ADJUSTMENT)
  86. // Adjust date back to 1601 from 1970 and convert to 100 nanoseconds
  87. #define TimeDateStampToFileTime(TimeDate) \
  88. (((ULONG64)(TimeDate) + BASE_YEAR_ADJUSTMENT) * 10000000)
  89. // Convert to seconds
  90. #define FileTimeToTime(FileTime) \
  91. (ULONG)((FileTime) / 10000000)
  92. // Convert to seconds .
  93. #define TimeToFileTime(TimeDate) \
  94. ((ULONG64)(TimeDate) * 10000000)
  95. HRESULT QueryVersionDataBuffer(PVOID VerData, PCSTR Item,
  96. PVOID Buffer, ULONG BufferSize,
  97. PULONG DataSize);
  98. PVOID GetAllFileVersionInfo(PSTR VerFile);
  99. BOOL GetFileStringFileInfo(PSTR VerFile, PCSTR SubItem,
  100. PSTR Buffer, ULONG BufferSize);
  101. HRESULT ExpandDumpCab(PCSTR CabFile, ULONG FileFlags,
  102. PSTR DmpFile, INT_PTR* DmpFh);
  103. enum FILE_IO_TYPE
  104. {
  105. FIO_WIN32,
  106. FIO_WININET,
  107. };
  108. class PathFile
  109. {
  110. public:
  111. PathFile(FILE_IO_TYPE IoType)
  112. {
  113. m_IoType = IoType;
  114. }
  115. virtual ~PathFile(void);
  116. virtual HRESULT Open(PCSTR Path, ULONG SymOpt) = 0;
  117. virtual HRESULT QueryDataAvailable(PULONG Avail) = 0;
  118. virtual HRESULT GetLastWriteTime(PFILETIME Time) = 0;
  119. virtual HRESULT Read(PVOID Buffer, ULONG BufferLen, PULONG Done) = 0;
  120. FILE_IO_TYPE m_IoType;
  121. };
  122. BOOL IsUrlPathComponent(PCSTR Path);
  123. #ifndef NT_NATIVE
  124. BOOL PathFileExists(PCSTR Path, ULONG SymOpt, FILE_IO_TYPE* IoType);
  125. HRESULT OpenPathFile(PCSTR Path, ULONG SymOpt, PathFile** File);
  126. #endif
  127. #endif // #ifndef __CMNUTIL_HPP__