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.

154 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. SampleShim.cpp
  5. Abstract:
  6. This DLL serves as a template for the creation of shim DLLs. Follow
  7. the commenting/coding style of this source file wherever possible.
  8. Never use tabs, configure your editor to insert spaces instead of
  9. tab characters.
  10. Notes:
  11. This is a sample DLL.
  12. History:
  13. 02/02/2000 markder Created
  14. --*/
  15. #include "ShimHook.h"
  16. // Add APIs that you wish to hook to this enumeration. The first one
  17. // must have "= USERAPIHOOKSTART", and the last one must be
  18. // APIHOOK_Count.
  19. enum
  20. {
  21. APIHOOK_OutputDebugStringA = USERAPIHOOKSTART,
  22. APIHOOK_OutputDebugStringW,
  23. APIHOOK_Count
  24. };
  25. /*++
  26. This stub function intercepts all calls to OutputDebugStringA
  27. and prefixes the output string with "SampleShim says:".
  28. Note that all Win32 APIs use __stdcall calling conventions, so
  29. you must be sure to have this set in MSVC++. Go to Projects|Settings,
  30. C/C++ tab, select Category: "Code Generation" from dropdown, make
  31. sure "Calling convention" is set to __stdcall.
  32. --*/
  33. VOID
  34. APIHook_OutputDebugStringA(
  35. LPCSTR szOutputString
  36. )
  37. {
  38. // Declare all local variables at top of function. Always use
  39. // Hungarian notation as follows:
  40. //
  41. // Type Scope
  42. // ----------------------- ------------------
  43. // Pointers p Global g_
  44. // DWORD dw Class member m_
  45. // LONG l Static s_
  46. // ANSI strings sz
  47. // Wide-char strings wsz
  48. // Arrays rg
  49. //
  50. LPSTR szNewOutputString;
  51. CHAR szPrefix[] = "SampleShim says: ";
  52. // All string alterations must be done in new memory. Never
  53. // alter a passed-in string in-place.
  54. szNewOutputString = (LPSTR) malloc( strlen( szOutputString ) +
  55. strlen( szPrefix ) + 1 );
  56. // Use the DPF macro to print debug strings. See Hooks\inc\ShimDebug.h
  57. // for debug level values. Use eDbgLevelError if an unexpected error occurs
  58. // in your shim code. For informational output, use eDbgLevelUser.
  59. DPF(eDbgLevelUser, "APIHook_OutputDebugStringA called.\n");
  60. strcpy( szNewOutputString, szPrefix );
  61. strcat( szNewOutputString, szOutputString );
  62. // Use the LOOKUP_APIHOOK macro to call the original API. You must use
  63. // this so that API chaining and inclusion/exclusion information is
  64. // preserved.
  65. LOOKUP_APIHOOK(OutputDebugStringA)( szNewOutputString );
  66. free( szNewOutputString );
  67. return;
  68. }
  69. /*++
  70. This stub function intercepts all calls to OutputDebugStringW
  71. and prefixes the output string with "SampleShim says:".
  72. Note that to make your shim generally applicable, you should include
  73. both ANSI and wide-character versions of your stub function.
  74. --*/
  75. VOID
  76. APIHook_OutputDebugStringW(
  77. LPCWSTR wszOutputString
  78. )
  79. {
  80. // NEVER use TCHAR variables or tcs-prefixed string manipulation routines.
  81. // Prefix all wide-character string constants with L. Never use _T() or
  82. // TEXT() macros.
  83. LPWSTR wszNewOutputString;
  84. WCHAR wszPrefix[] = L"SampleShim says: ";
  85. // A single line of code should never be more than 80 characters long.
  86. wszNewOutputString = (LPWSTR) malloc( sizeof(WCHAR) *
  87. ( wcslen( wszOutputString ) +
  88. wcslen( wszPrefix ) + 1 ) );
  89. DPF(eDbgLevelUser, "APIHook_OutputDebugStringW called.\n");
  90. // Make sure to use wide-character versions of all string manipulation
  91. // routines where appropriate.
  92. wcscpy( wszNewOutputString, wszPrefix );
  93. wcscat( wszNewOutputString, wszOutputString );
  94. LOOKUP_APIHOOK(OutputDebugStringW)( wszNewOutputString );
  95. free( wszNewOutputString );
  96. return;
  97. }
  98. /*++
  99. Register hooked functions
  100. --*/
  101. VOID
  102. InitializeHooks(DWORD fdwReason)
  103. {
  104. if (fdwReason != DLL_PROCESS_ATTACH) return;
  105. // Don't touch this line.
  106. INIT_HOOKS(APIHOOK_Count);
  107. // Add APIs that you wish to hook here. All API prototypes
  108. // must be declared in Hooks\inc\ShimProto.h. Compiler errors
  109. // will result if you forget to add them.
  110. DECLARE_APIHOOK(KERNEL32.DLL, OutputDebugStringA);
  111. DECLARE_APIHOOK(KERNEL32.DLL, OutputDebugStringW);
  112. // If you have any more initialization to do, do it here.
  113. }