Chromium Code Reviews| Index: chrome/app/delay_load_hook.cc |
| diff --git a/chrome/app/delay_load_hook.cc b/chrome/app/delay_load_hook.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c661faa21a0e0f9d257ed02819b47f2682228c19 |
| --- /dev/null |
| +++ b/chrome/app/delay_load_hook.cc |
| @@ -0,0 +1,94 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/app/delay_load_hook.h" |
| + |
| +#if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700 |
| +// The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h, and in |
| +// delayimp.h previous to VS2012. |
| +#undef FACILITY_VISUALCPP |
| +#endif |
| +#include <DelayIMP.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/string_util.h" |
| + |
| +// So long as these symbols are supplied to the final binary through an |
| +// object file (as opposed to indirectly thruogh a library), these pointers |
|
M-A Ruel
2013/02/07 21:14:30
through
|
| +// will override the CRT's symbols and direct the notifications to our hook. |
| +PfnDliHook __pfnDliNotifyHook2 = ChromeDelayLoadHook; |
| +PfnDliHook __pfnDliFailureHook2 = ChromeDelayLoadHook; |
| + |
| + |
| +namespace { |
| + |
| +FARPROC OnPreLoadLibrary(DelayLoadInfo* info) { |
| + // If the DLL name ends with "-delay.dll", this call is about one of our |
| + // custom import libraries. In this case we need to snip the suffix off, |
| + // and bind to the real DLL. |
| + std::string dll_name(info->szDll); |
| + const char kDelaySuffix[] = "-delay.dll"; |
| + if (EndsWith(dll_name, kDelaySuffix, false)) { |
| + // Trim the "-delay.dll" suffix from the string. |
| + dll_name.resize(dll_name.length() - (sizeof(kDelaySuffix) - 1)); |
| + dll_name.append(".dll"); |
| + |
| + HMODULE dll = ::LoadLibraryA(dll_name.c_str()); |
|
M-A Ruel
2013/02/07 21:14:30
return reinterpret_cast<FARPROC>(::LoadLibraryA(dl
|
| + |
| + return reinterpret_cast<FARPROC>(dll); |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| +FARPROC OnFailLoadLib(DelayLoadInfo* info) { |
| + LOG(FATAL) << "Failed to delay load DLL: " << info->szDll; |
| + return NULL; |
| +} |
| + |
| +FARPROC OnFailGetProc(DelayLoadInfo* info) { |
| + if (info->dlp.fImportByName) { |
| + LOG(FATAL) << "Failed to delay load function: '" << info->dlp.szProcName |
| + << "' from DLL: " << info->szDll; |
| + } else { |
| + LOG(FATAL) << "Failed to delay load ordinal: '" << info->dlp.dwOrdinal |
| + << "' from DLL: " << info->szDll; |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| +} // namespace |
| + |
| +FARPROC WINAPI ChromeDelayLoadHook(unsigned reason, DelayLoadInfo* info) { |
| + switch (reason) { |
| + case dliNoteStartProcessing: |
| + LOG(INFO) << "Delay load dliNoteStartProcessing notification."; |
| + break; |
| + case dliNotePreLoadLibrary: |
| + return OnPreLoadLibrary(info); |
| + |
| + break; |
| + case dliNotePreGetProcAddress: |
| + LOG(INFO) << "Delay load dliNotePreGetProcAddress notification."; |
| + break; |
| + |
| + case dliFailLoadLib: |
| + return OnFailLoadLib(info); |
| + break; |
| + |
| + case dliFailGetProc: |
| + return OnFailGetProc(info); |
| + break; |
| + |
| + case dliNoteEndProcessing: |
| + LOG(INFO) << "Delay load dliNoteEndProcessing notification."; |
| + break; |
| + default: |
| + NOTREACHED() << "Impossible delay load notification."; |
| + break; |
| + } |
| + |
| + return NULL; |
| +} |