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.

105 lines
3.1 KiB

  1. /*++
  2. Copyright (c) 2000 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. --*/
  14. #include "precomp.h"
  15. IMPLEMENT_SHIM_BEGIN(PropagateProcessHistory)
  16. #include "ShimHookMacro.h"
  17. APIHOOK_ENUM_BEGIN
  18. APIHOOK_ENUM_END
  19. /*++
  20. Register hooked functions
  21. --*/
  22. BOOL
  23. NOTIFY_FUNCTION(
  24. DWORD fdwReason)
  25. {
  26. if (fdwReason == DLL_PROCESS_ATTACH)
  27. {
  28. DWORD dwProcessHistoryBufSize, dwExeFileNameBufSize;
  29. LPWSTR wszExeFileName = NULL, wszProcessHistory = NULL;
  30. dwProcessHistoryBufSize = GetEnvironmentVariableW( L"__PROCESS_HISTORY", NULL, 0 );
  31. dwExeFileNameBufSize = MAX_PATH; // GetModuleFileNameW doesn't return buffer size needed??;
  32. wszProcessHistory = (LPWSTR) HeapAlloc(
  33. GetProcessHeap(),
  34. HEAP_GENERATE_EXCEPTIONS,
  35. (dwProcessHistoryBufSize + dwExeFileNameBufSize + 2) * sizeof(WCHAR) );
  36. wszExeFileName = (LPWSTR) HeapAlloc(
  37. GetProcessHeap(),
  38. HEAP_GENERATE_EXCEPTIONS,
  39. (dwExeFileNameBufSize + 1) * sizeof(WCHAR) );
  40. if( wszExeFileName && wszProcessHistory )
  41. {
  42. wszProcessHistory[0] = L'\0';
  43. dwProcessHistoryBufSize = GetEnvironmentVariableW(
  44. L"__PROCESS_HISTORY",
  45. wszProcessHistory,
  46. dwProcessHistoryBufSize );
  47. dwExeFileNameBufSize = GetModuleFileNameW( NULL, wszExeFileName, dwExeFileNameBufSize );
  48. if( *wszProcessHistory && wszProcessHistory[wcslen(wszProcessHistory) - 1] != L';' )
  49. wcscat( wszProcessHistory, L";" );
  50. wcscat( wszProcessHistory, wszExeFileName );
  51. if( ! SetEnvironmentVariableW( L"__PROCESS_HISTORY", wszProcessHistory ) )
  52. {
  53. DPFN( eDbgLevelError, "SetEnvironmentVariable failed!");
  54. }
  55. else
  56. {
  57. DPFN( eDbgLevelInfo, "Current EXE added to process history");
  58. DPFN( eDbgLevelInfo, "__PROCESS_HISTORY=%S", wszProcessHistory);
  59. }
  60. }
  61. else
  62. DPFN( eDbgLevelError, "Could not allocate memory for strings");
  63. if( wszProcessHistory )
  64. HeapFree( GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, wszProcessHistory );
  65. if( wszExeFileName )
  66. HeapFree( GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, wszExeFileName );
  67. }
  68. return TRUE;
  69. }
  70. HOOK_BEGIN
  71. CALL_NOTIFY_FUNCTION
  72. HOOK_END
  73. IMPLEMENT_SHIM_END