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.

321 lines
8.0 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(char* pszShim);
  40. VOID __cdecl FileLog(DWORD dwDetail, LPSTR pszFmt, ...);
  41. #if DBG
  42. VOID DebugPrintfList(LPCSTR szShimName, DEBUGLEVEL dwDetail, LPCSTR szFmt, va_list vaArgList);
  43. VOID DebugPrintf( LPCSTR szShimName, DEBUGLEVEL dwDetail, LPCSTR szFmt, ...);
  44. #endif
  45. // ***************************************************************************
  46. // ***************************************************************************
  47. /*++
  48. Shim debug routines.
  49. --*/
  50. // Our own version of ASSERT
  51. #ifdef ASSERT
  52. #undef ASSERT
  53. #endif
  54. #if DBG
  55. VOID DebugAssert(LPCSTR szFile, DWORD dwLine, BOOL bAssert, LPCSTR szHelpString);
  56. #define ASSERT(a, b) DebugAssert(__FILE__, __LINE__, a, b)
  57. #else
  58. #pragma warning(disable : 4002)
  59. #define ASSERT(a, b)
  60. #pragma warning(default : 4002)
  61. #endif
  62. inline void DPF(LPCSTR szShimName, DEBUGLEVEL dwDetail, LPSTR pszFmt, ...)
  63. {
  64. #if DBG
  65. // This must be the first line of this routine to preserve LastError.
  66. DWORD dwLastError = GetLastError();
  67. va_list vaArgList;
  68. va_start(vaArgList, pszFmt);
  69. DebugPrintfList(szShimName, dwDetail, pszFmt, vaArgList);
  70. va_end(vaArgList);
  71. // This must be the last line of this routine to preserve LastError.
  72. SetLastError(dwLastError);
  73. #else
  74. szShimName;
  75. dwDetail;
  76. pszFmt;
  77. #endif
  78. }
  79. inline void LOG(LPCSTR szShimName, DEBUGLEVEL dwDetail, LPSTR pszFmt, ...)
  80. {
  81. if (g_bFileLogEnabled)
  82. {
  83. // This must be the first line of this routine to preserve LastError.
  84. DWORD dwLastError = GetLastError();
  85. va_list vaArgList;
  86. va_start(vaArgList, pszFmt);
  87. ShimLogList(szShimName, dwDetail, pszFmt, vaArgList);
  88. va_end(vaArgList);
  89. // This must be the last line of this routine to preserve LastError.
  90. SetLastError(dwLastError);
  91. }
  92. }
  93. }; // end of namespace ShimLib
  94. // ***************************************************************************
  95. // ***************************************************************************
  96. /*++
  97. The shim system uses its own heap.
  98. Malloc, free, new and delete are redirected to these routines:
  99. --*/
  100. namespace ShimLib
  101. {
  102. void * __cdecl ShimMalloc(size_t size);
  103. void __cdecl ShimFree(void * memory);
  104. void * __cdecl ShimCalloc(size_t num, size_t size);
  105. void * __cdecl ShimRealloc(void * memory, size_t size);
  106. }; // end of namespace ShimLib
  107. // We override malloc/free with our own versions using a private heap.
  108. #define malloc(size) ShimLib::ShimMalloc(size)
  109. #define free(memory) ShimLib::ShimFree(memory)
  110. #define calloc(num, size) ShimLib::ShimCalloc(num, size)
  111. #define realloc(memory, size) ShimLib::ShimRealloc(memory, size)
  112. inline void * __cdecl operator new(size_t size)
  113. {
  114. return ShimLib::ShimMalloc(size);
  115. }
  116. inline void * operator new[]( size_t size )
  117. {
  118. return ShimLib::ShimMalloc(size);
  119. }
  120. inline void __cdecl operator delete(void * memory)
  121. {
  122. ShimLib::ShimFree(memory);
  123. }
  124. inline void operator delete[]( void * memory )
  125. {
  126. ShimLib::ShimFree(memory);
  127. }
  128. #include "ShimCString.h"
  129. // ***************************************************************************
  130. // ***************************************************************************
  131. /*++
  132. ShimLib routines
  133. --*/
  134. namespace ShimLib
  135. {
  136. /*++
  137. Prototypes for various helper routines.
  138. --*/
  139. PVOID HookCallback( PVOID pfnOld, PVOID pfnNew );
  140. UINT GetDriveTypeFromHandle(HANDLE hFile);
  141. UINT GetDriveTypeFromFileNameA(LPCSTR lpFileName, char *lpDriveLetter = NULL);
  142. UINT GetDriveTypeFromFileNameW(LPCWSTR lpFileName, WCHAR *lpDriveLetter = NULL);
  143. inline BOOL IsOnCDRom(HANDLE hFile) { return GetDriveTypeFromHandle(hFile) == DRIVE_CDROM; }
  144. inline BOOL IsOnCDRomA(LPCSTR lpFileName) { return GetDriveTypeFromFileNameA(lpFileName) == DRIVE_CDROM; }
  145. inline BOOL IsOnCDRomW(LPCWSTR lpFileName) { return GetDriveTypeFromFileNameW(lpFileName) == DRIVE_CDROM; }
  146. BOOL IsImage16BitA(LPCSTR lpFileName);
  147. BOOL IsImage16BitW(LPCWSTR lpFileName);
  148. WCHAR * ToUnicode(const char * lpszAnsi);
  149. char * ToAnsi(const WCHAR * lpszUnicode);
  150. LPWSTR * _CommandLineToArgvW(LPCWSTR lpCmdLine, int * pNumArgs);
  151. LPSTR * _CommandLineToArgvA(LPCSTR lpCmdLine, int * pNumArgs);
  152. char * StringDuplicateA(const char * strToCopy);
  153. WCHAR * StringDuplicateW(const WCHAR * wstrToCopy);
  154. char * StringNDuplicateA(const char * strToCopy, int stringLength);
  155. WCHAR * StringNDuplicateW(const WCHAR * wstrToCopy, int stringLength);
  156. int SafeStringCopyW(WCHAR *lpDest, DWORD nDestChars, const WCHAR *lpSrc, DWORD nSrcChars);
  157. VOID SkipBlanksW(const WCHAR *& str);
  158. BOOL PatternMatchW(LPCWSTR szPattern, LPCWSTR szTestString);
  159. // stristr is *not* DBCS safe
  160. char * __cdecl stristr(const char* string, const char * strCharSet);
  161. WCHAR * __cdecl wcsistr(const WCHAR* string, const WCHAR * strCharSet);
  162. char * __cdecl _strtok(char *strToken, const char *strDelimit);
  163. BOOL bIsSafeDisc1();
  164. BOOL bIsSafeDisc2();
  165. BOOL IsNTVDM(void);
  166. WCHAR * W9xPathMassageW(const WCHAR * uncorrect);
  167. BOOL MakeShimUnloadLast(HMODULE hMod);
  168. DEBUGLEVEL GetDebugLevel(void);
  169. }; // end of namespace ShimLib
  170. // ***************************************************************************
  171. // ***************************************************************************
  172. /*++
  173. AppAndCommandLine is a class used to parse lpApplicationName and lpCommandline
  174. exactly as it would be by CreateProcess().
  175. --*/
  176. namespace ShimLib
  177. {
  178. class AppAndCommandLine
  179. {
  180. protected:
  181. CString csApplicationName;
  182. CString csCommandLine;
  183. CString csCommandLineNoAppName;
  184. CString csShortCommandLine;
  185. BOOL GetAppnameAndCommandline(const WCHAR * lpcApp, const WCHAR * lpcCl);
  186. public:
  187. AppAndCommandLine(const char * lpcApplicationName, const char * lpcCommandLine);
  188. AppAndCommandLine(const WCHAR * lpcApplicationName, const WCHAR * lpcCommandLine);
  189. inline const CString & GetApplicationName() const;
  190. inline const CString & GetCommandline() const;
  191. inline const CString & GetCommandlineNoAppName() const;
  192. const CString & GetShortCommandLine();
  193. };
  194. inline const CString & AppAndCommandLine::GetApplicationName() const
  195. {
  196. return csApplicationName;
  197. }
  198. inline const CString & AppAndCommandLine::GetCommandline() const
  199. {
  200. return csCommandLine;
  201. }
  202. inline const CString & AppAndCommandLine::GetCommandlineNoAppName() const
  203. {
  204. return csCommandLineNoAppName;
  205. }
  206. }; // end of namespace ShimLib
  207. // ***************************************************************************
  208. // ***************************************************************************