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.

316 lines
8.2 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. ShimLib.h
  5. Abstract:
  6. Routines available in ShimLib.lib
  7. Notes:
  8. None
  9. History:
  10. 08/13/2001 robkenny Created.
  11. 08/14/2001 robkenny Inserted inside the ShimLib namespace.
  12. 08/15/2001 robkenny Merged several include files.
  13. 09/11/2001 mnikkel Modified DPFN and LOGN to retain LastError
  14. --*/
  15. #pragma once
  16. #include <Windows.h>
  17. // ***************************************************************************
  18. // ***************************************************************************
  19. namespace ShimLib
  20. {
  21. // Debug levels
  22. typedef enum
  23. {
  24. eDbgLevelBase = 0,
  25. eDbgLevelError,
  26. eDbgLevelWarning,
  27. eDbgLevelInfo,
  28. eDbgLevelSpew = 9,
  29. } DEBUGLEVEL;
  30. extern BOOL g_bFileLogEnabled; // Is the LOG() routine logging to a file.
  31. // Environment variable with the name of the log file
  32. #define szFileLogEnvironmentVariable "SHIM_FILE_LOG"
  33. #define wszFileLogEnvironmentVariable L"SHIM_FILE_LOG"
  34. // Debug environment variable, values = 0 -> 9
  35. #define szDebugEnvironmentVariable "SHIM_DEBUG_LEVEL"
  36. void APPBreakPoint(void);
  37. VOID ShimLogList(LPCSTR szShimName, DEBUGLEVEL dwDbgLevel, LPCSTR pszFmt, va_list arglist);
  38. VOID ShimLog( LPCSTR szShimName, DEBUGLEVEL dwDbgLevel, LPCSTR pszFmt, ...);
  39. BOOL InitFileLogSupport();
  40. VOID __cdecl FileLog(DWORD dwDetail, LPSTR pszFmt, ...);
  41. VOID DebugPrintfList(LPCSTR szShimName, DEBUGLEVEL dwDetail, LPCSTR szFmt, va_list vaArgList);
  42. VOID DebugPrintf( LPCSTR szShimName, DEBUGLEVEL dwDetail, LPCSTR szFmt, ...);
  43. // ***************************************************************************
  44. // ***************************************************************************
  45. /*++
  46. Shim debug routines.
  47. --*/
  48. // Our own version of ASSERT
  49. #ifdef ASSERT
  50. #undef ASSERT
  51. #endif
  52. #if DBG
  53. VOID DebugAssert(LPCSTR szFile, DWORD dwLine, BOOL bAssert, LPCSTR szHelpString);
  54. #define ASSERT(a, b) DebugAssert(__FILE__, __LINE__, a, b)
  55. #else
  56. #pragma warning(disable : 4002)
  57. #define ASSERT(a, b)
  58. #pragma warning(default : 4002)
  59. #endif
  60. inline void DPF(LPCSTR szShimName, DEBUGLEVEL dwDetail, LPCSTR pszFmt, ...)
  61. {
  62. #if DBG
  63. // This must be the first line of this routine to preserve LastError.
  64. DWORD dwLastError = GetLastError();
  65. va_list vaArgList;
  66. va_start(vaArgList, pszFmt);
  67. DebugPrintfList(szShimName, dwDetail, pszFmt, vaArgList);
  68. va_end(vaArgList);
  69. // This must be the last line of this routine to preserve LastError.
  70. SetLastError(dwLastError);
  71. #else
  72. szShimName;
  73. dwDetail;
  74. pszFmt;
  75. #endif
  76. }
  77. inline void LOG(LPCSTR szShimName, DEBUGLEVEL dwDetail, LPCSTR pszFmt, ...)
  78. {
  79. if (g_bFileLogEnabled)
  80. {
  81. // This must be the first line of this routine to preserve LastError.
  82. DWORD dwLastError = GetLastError();
  83. va_list vaArgList;
  84. va_start(vaArgList, pszFmt);
  85. ShimLogList(szShimName, dwDetail, pszFmt, vaArgList);
  86. va_end(vaArgList);
  87. // This must be the last line of this routine to preserve LastError.
  88. SetLastError(dwLastError);
  89. }
  90. }
  91. }; // end of namespace ShimLib
  92. // ***************************************************************************
  93. // ***************************************************************************
  94. /*++
  95. The shim system uses its own heap.
  96. Malloc, free, new and delete are redirected to these routines:
  97. --*/
  98. namespace ShimLib
  99. {
  100. void * __cdecl ShimMalloc(size_t size);
  101. void __cdecl ShimFree(void * memory);
  102. void * __cdecl ShimCalloc(size_t num, size_t size);
  103. void * __cdecl ShimRealloc(void * memory, size_t size);
  104. }; // end of namespace ShimLib
  105. // We override malloc/free with our own versions using a private heap.
  106. #define malloc(size) ShimLib::ShimMalloc(size)
  107. #define free(memory) ShimLib::ShimFree(memory)
  108. #define calloc(num, size) ShimLib::ShimCalloc(num, size)
  109. #define realloc(memory, size) ShimLib::ShimRealloc(memory, size)
  110. inline void * __cdecl operator new(size_t size)
  111. {
  112. return ShimLib::ShimMalloc(size);
  113. }
  114. inline void * operator new[]( size_t size )
  115. {
  116. return ShimLib::ShimMalloc(size);
  117. }
  118. inline void __cdecl operator delete(void * memory)
  119. {
  120. ShimLib::ShimFree(memory);
  121. }
  122. inline void operator delete[]( void * memory )
  123. {
  124. ShimLib::ShimFree(memory);
  125. }
  126. #include "ShimCString.h"
  127. // ***************************************************************************
  128. // ***************************************************************************
  129. /*++
  130. ShimLib routines
  131. --*/
  132. namespace ShimLib
  133. {
  134. /*++
  135. Prototypes for various helper routines.
  136. --*/
  137. PVOID HookCallback( PVOID pfnOld, PVOID pfnNew );
  138. UINT GetDriveTypeFromHandle(HANDLE hFile);
  139. UINT GetDriveTypeFromFileNameA(LPCSTR lpFileName, char *lpDriveLetter = NULL);
  140. UINT GetDriveTypeFromFileNameW(LPCWSTR lpFileName, WCHAR *lpDriveLetter = NULL);
  141. inline BOOL IsOnCDRom(HANDLE hFile) { return GetDriveTypeFromHandle(hFile) == DRIVE_CDROM; }
  142. inline BOOL IsOnCDRomA(LPCSTR lpFileName) { return GetDriveTypeFromFileNameA(lpFileName) == DRIVE_CDROM; }
  143. inline BOOL IsOnCDRomW(LPCWSTR lpFileName) { return GetDriveTypeFromFileNameW(lpFileName) == DRIVE_CDROM; }
  144. BOOL IsImage16BitA(LPCSTR lpFileName);
  145. BOOL IsImage16BitW(LPCWSTR lpFileName);
  146. WCHAR * ToUnicode(const char * lpszAnsi);
  147. char * ToAnsi(const WCHAR * lpszUnicode);
  148. LPWSTR * _CommandLineToArgvW(LPCWSTR lpCmdLine, int * pNumArgs);
  149. LPSTR * _CommandLineToArgvA(LPCSTR lpCmdLine, int * pNumArgs);
  150. char * StringDuplicateA(const char * strToCopy);
  151. WCHAR * StringDuplicateW(const WCHAR * wstrToCopy);
  152. char * StringNDuplicateA(const char * strToCopy, int stringLength);
  153. WCHAR * StringNDuplicateW(const WCHAR * wstrToCopy, int stringLength);
  154. VOID SkipBlanksW(const WCHAR *& str);
  155. BOOL PatternMatchW(LPCWSTR szPattern, LPCWSTR szTestString);
  156. // stristr is *not* DBCS safe
  157. char * __cdecl stristr(const char* string, const char * strCharSet);
  158. WCHAR * __cdecl wcsistr(const WCHAR* string, const WCHAR * strCharSet);
  159. char * __cdecl _strtok(char *strToken, const char *strDelimit);
  160. BOOL bIsSafeDisc1();
  161. BOOL bIsSafeDisc2();
  162. BOOL IsNTVDM(void);
  163. WCHAR * W9xPathMassageW(const WCHAR * uncorrect);
  164. BOOL MakeShimUnloadLast(HMODULE hMod);
  165. DEBUGLEVEL GetDebugLevel(void);
  166. }; // end of namespace ShimLib
  167. // ***************************************************************************
  168. // ***************************************************************************
  169. /*++
  170. AppAndCommandLine is a class used to parse lpApplicationName and lpCommandline
  171. exactly as it would be by CreateProcess().
  172. --*/
  173. namespace ShimLib
  174. {
  175. class AppAndCommandLine
  176. {
  177. protected:
  178. CString csApplicationName;
  179. CString csCommandLine;
  180. CString csCommandLineNoAppName;
  181. CString csShortCommandLine;
  182. BOOL GetAppnameAndCommandline(const WCHAR * lpcApp, const WCHAR * lpcCl);
  183. public:
  184. AppAndCommandLine(const char * lpcApplicationName, const char * lpcCommandLine);
  185. AppAndCommandLine(const WCHAR * lpcApplicationName, const WCHAR * lpcCommandLine);
  186. inline const CString & GetApplicationName() const;
  187. inline const CString & GetCommandline() const;
  188. inline const CString & GetCommandlineNoAppName() const;
  189. const CString & GetShortCommandLine();
  190. };
  191. inline const CString & AppAndCommandLine::GetApplicationName() const
  192. {
  193. return csApplicationName;
  194. }
  195. inline const CString & AppAndCommandLine::GetCommandline() const
  196. {
  197. return csCommandLine;
  198. }
  199. inline const CString & AppAndCommandLine::GetCommandlineNoAppName() const
  200. {
  201. return csCommandLineNoAppName;
  202. }
  203. }; // end of namespace ShimLib
  204. // ***************************************************************************
  205. // ***************************************************************************