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.

80 lines
2.0 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. DuplicateHandleFix.cpp
  5. Abstract:
  6. DuplicateHandle was changed to always NULL the destination handle, even if
  7. errors were generated. THis shim ensures that the DestinationHandle is
  8. not modified if the duplication was not successful.
  9. History:
  10. 10/11/2001 robkenny Created.
  11. --*/
  12. #include "precomp.h"
  13. IMPLEMENT_SHIM_BEGIN(DuplicateHandleFix)
  14. #include "ShimHookMacro.h"
  15. APIHOOK_ENUM_BEGIN
  16. APIHOOK_ENUM_ENTRY(DuplicateHandle)
  17. APIHOOK_ENUM_END
  18. typedef BOOL (WINAPI *_pfn_DuplicateHandle)(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions );
  19. /*++
  20. Don't allow DestinationHandle to change of DuplicateHandle generates an error.
  21. --*/
  22. BOOL
  23. APIHOOK(DuplicateHandle)(
  24. HANDLE hSourceProcessHandle, // handle to source process
  25. HANDLE hSourceHandle, // handle to duplicate
  26. HANDLE hTargetProcessHandle, // handle to target process
  27. LPHANDLE lpTargetHandle, // duplicate handle
  28. DWORD dwDesiredAccess, // requested access
  29. BOOL bInheritHandle, // handle inheritance option
  30. DWORD dwOptions // optional actions
  31. )
  32. {
  33. // Save the original value
  34. HANDLE origHandle = *lpTargetHandle;
  35. BOOL bSuccess = ORIGINAL_API(DuplicateHandle)(hSourceProcessHandle,
  36. hSourceHandle, hTargetProcessHandle, lpTargetHandle, dwDesiredAccess,
  37. bInheritHandle, dwOptions);
  38. if (!bSuccess) {
  39. DWORD dwLastError = GetLastError();
  40. //
  41. // DuplicateHandle has set *lpTargetHandle to NULL, revert to it's previous value.
  42. //
  43. LOGN(eDbgLevelError, "DuplicateHandle failed, reverting *lpTargetHandle to previous value");
  44. *lpTargetHandle = origHandle;
  45. }
  46. return bSuccess;
  47. }
  48. /*++
  49. Register hooked functions
  50. --*/
  51. HOOK_BEGIN
  52. APIHOOK_ENTRY(KERNEL32.DLL, DuplicateHandle)
  53. HOOK_END
  54. IMPLEMENT_SHIM_END