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.

78 lines
2.6 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. // External function prototypes
  4. FARPROC
  5. WINAPI
  6. DelayLoadFailureHook (
  7. LPCSTR pszDllName,
  8. LPCSTR pszProcName
  9. );
  10. //
  11. // This function is for people who statically link to dload.lib so that
  12. // the can get all of kernel32's dload error stubs on any os <= Whistler.
  13. //
  14. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  15. //
  16. // NOTE: You should ONLY use this if you have a binary that must run on NT4, win2k, win9x, etc.
  17. // If your binary is whistler or greater, use DLOAD_ERROR_HANDLER=kernel32 in your
  18. // sources file instead.
  19. //
  20. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  21. FARPROC
  22. WINAPI
  23. Downlevel_DelayLoadFailureHook(
  24. UINT unReason,
  25. PDelayLoadInfo pDelayInfo
  26. )
  27. {
  28. FARPROC ReturnValue = NULL;
  29. // For a failed LoadLibrary, we will return the HINSTANCE of this DLL.
  30. // This will cause the loader to try a GetProcAddress on our DLL for the
  31. // function. This will subsequently fail and then we will be called
  32. // for dliFailGetProc below.
  33. if (dliFailLoadLib == unReason)
  34. {
  35. // HACKHACK (reinerf)
  36. //
  37. // For ORDINAL delayload failures we cannot just return our base addr and be done with everything. The problem
  38. // is that the linker stub code will turn around and call GetProcAddress() some random ordinal which probably
  39. // exists and is definately NOT the correct function.
  40. //
  41. // So to get around this problem we return -1 for a the hModule, which should cause GetProcAddress(-1, ...) to
  42. // always fail. This is good, because the linker code will call us back for the GetProcAddress failure and we
  43. // can then return the stub error handler proc.
  44. ReturnValue = (FARPROC)-1;
  45. }
  46. else if (dliFailGetProc == unReason)
  47. {
  48. // The loader is asking us to return a pointer to a procedure.
  49. // Lookup the handler for this DLL/procedure and, if found, return it.
  50. ReturnValue = DelayLoadFailureHook(pDelayInfo->szDll, pDelayInfo->dlp.szProcName);
  51. if (ReturnValue)
  52. {
  53. // Do this on behalf of the handler now that it is about to
  54. // be called.
  55. SetLastError(ERROR_MOD_NOT_FOUND);
  56. }
  57. }
  58. return ReturnValue;
  59. }
  60. extern const ULONG g_ulDelayLoad_Win32Error = ERROR_PROC_NOT_FOUND;
  61. extern const LONG g_lDelayLoad_NtStatus = STATUS_ENTRYPOINT_NOT_FOUND;
  62. VOID
  63. WINAPI
  64. DelayLoad_SetLastNtStatusAndWin32Error(
  65. )
  66. {
  67. SetLastError(g_ulDelayLoad_Win32Error);
  68. }