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.

171 lines
5.0 KiB

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