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.

128 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 2000-2002 Microsoft Corporation
  3. Module Name:
  4. PropagateProcessHistory.cpp
  5. Abstract:
  6. This DLL adds the current process to the __PROCESS_HISTORY environment
  7. variable. This is needed for 32-bit applications that launch other
  8. 32-bit executables that have been put in a temporary directory and have
  9. no appropriate side-step files. It allows the matching mechanism to
  10. locate files in the parent's directory, which are unique to the application.
  11. History:
  12. 03/21/2000 markder Created
  13. 03/13/2002 mnikkel Modified to use strsafe and correctly handle error returns from
  14. GetEnvironmentVariableW and GetModuleFileNameW. Removed
  15. HEAP_GENERATE_EXCEPTIONS from HeapAllocs.
  16. 03/26/2002 mnikkel Removed incorrect checks for error on GetEnvironmentVariableW.
  17. --*/
  18. #include "precomp.h"
  19. IMPLEMENT_SHIM_BEGIN(PropagateProcessHistory)
  20. #include "ShimHookMacro.h"
  21. APIHOOK_ENUM_BEGIN
  22. APIHOOK_ENUM_END
  23. /*++
  24. Register hooked functions
  25. --*/
  26. BOOL
  27. NOTIFY_FUNCTION(
  28. DWORD fdwReason)
  29. {
  30. BOOL bRet = TRUE;
  31. if (fdwReason == DLL_PROCESS_ATTACH)
  32. {
  33. DWORD dwProcessHistoryBufSize, dwExeFileNameBufSize, dwFullProcessSize;
  34. LPWSTR wszExeFileName = NULL, wszProcessHistory = NULL;
  35. // Get size of buffers, note that if PROCESS_HISTORY is not defined that
  36. // dwProcessHistoryBufSize can be zero. This is expected.
  37. dwProcessHistoryBufSize = GetEnvironmentVariableW( L"__PROCESS_HISTORY", NULL, 0 );
  38. dwExeFileNameBufSize = MAX_PATH*2; // GetModuleFileNameW doesn't return buffer size needed??;
  39. dwFullProcessSize = dwProcessHistoryBufSize + dwExeFileNameBufSize + 2;
  40. wszProcessHistory = (LPWSTR) HeapAlloc(
  41. GetProcessHeap(),
  42. 0,
  43. dwFullProcessSize * sizeof(WCHAR) );
  44. wszExeFileName = (LPWSTR) HeapAlloc(
  45. GetProcessHeap(),
  46. 0,
  47. (dwExeFileNameBufSize + 1) * sizeof(WCHAR) );
  48. if( wszExeFileName && wszProcessHistory )
  49. {
  50. wszProcessHistory[0] = L'\0';
  51. if (dwProcessHistoryBufSize > 0)
  52. {
  53. dwProcessHistoryBufSize = GetEnvironmentVariableW(
  54. L"__PROCESS_HISTORY",
  55. wszProcessHistory,
  56. dwProcessHistoryBufSize );
  57. }
  58. dwExeFileNameBufSize = GetModuleFileNameW( NULL, wszExeFileName, dwExeFileNameBufSize );
  59. if (dwExeFileNameBufSize <= 0)
  60. {
  61. DPFN( eDbgLevelError, "GetModuleFileNameW failed.");
  62. bRet = FALSE;
  63. goto exitnotify;
  64. }
  65. if( *wszProcessHistory && wszProcessHistory[wcslen(wszProcessHistory) - 1] != L';' )
  66. StringCchCatW(wszProcessHistory, dwFullProcessSize, L";");
  67. StringCchCatW(wszProcessHistory, dwFullProcessSize, wszExeFileName);
  68. if( ! SetEnvironmentVariableW( L"__PROCESS_HISTORY", wszProcessHistory ) )
  69. {
  70. DPFN( eDbgLevelError, "SetEnvironmentVariable failed!");
  71. }
  72. else
  73. {
  74. DPFN( eDbgLevelInfo, "Current EXE added to process history");
  75. DPFN( eDbgLevelInfo, "__PROCESS_HISTORY=%S", wszProcessHistory);
  76. }
  77. }
  78. else
  79. {
  80. DPFN( eDbgLevelError, "Could not allocate memory for strings");
  81. bRet = FALSE;
  82. }
  83. exitnotify:
  84. if( wszProcessHistory )
  85. HeapFree( GetProcessHeap(), 0, wszProcessHistory );
  86. if( wszExeFileName )
  87. HeapFree( GetProcessHeap(), 0, wszExeFileName );
  88. }
  89. return bRet;
  90. }
  91. HOOK_BEGIN
  92. CALL_NOTIFY_FUNCTION
  93. HOOK_END
  94. IMPLEMENT_SHIM_END