1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/app/delay_load_hook_win.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
13 // So long as these symbols are supplied to the final binary through an
14 // object file (as opposed to indirectly through a library), these pointers
15 // will override the CRT's symbols and direct the notifications to our hook.
16 // Alternatively referencing the ChromeDelayLoadHook function somehow will
17 // cause this declaration of these variables to take preference to the delay
18 // load runtime's defaults (in delayimp.lib).
19 PfnDliHook __pfnDliNotifyHook2
= ChromeDelayLoadHook
;
20 PfnDliHook __pfnDliFailureHook2
= ChromeDelayLoadHook
;
25 FARPROC
OnPreLoadLibrary(DelayLoadInfo
* info
) {
26 // If the DLL name ends with "-delay.dll", this call is about one of our
27 // custom import libraries. In this case we need to snip the suffix off,
28 // and bind to the real DLL.
29 std::string
dll_name(info
->szDll
);
30 const char kDelaySuffix
[] = "-delay.dll";
31 if (base::EndsWith(dll_name
, kDelaySuffix
,
32 base::CompareCase::INSENSITIVE_ASCII
)) {
33 // Trim the "-delay.dll" suffix from the string.
34 dll_name
.resize(dll_name
.length() - (sizeof(kDelaySuffix
) - 1));
35 dll_name
.append(".dll");
37 return reinterpret_cast<FARPROC
>(::LoadLibraryA(dll_name
.c_str()));
45 // This function is a delay load notification hook. It is invoked by the
46 // delay load support in the visual studio runtime.
47 // See http://msdn.microsoft.com/en-us/library/z9h1h6ty(v=vs.100).aspx for
49 extern "C" FARPROC WINAPI
ChromeDelayLoadHook(unsigned reason
,
50 DelayLoadInfo
* info
) {
52 case dliNoteStartProcessing
:
53 case dliNoteEndProcessing
:
54 // Nothing to do here.
57 case dliNotePreLoadLibrary
:
58 return OnPreLoadLibrary(info
);
61 case dliNotePreGetProcAddress
:
62 // Nothing to do here.
67 // Returning NULL from error notifications will cause the delay load
68 // runtime to raise a VcppException structured exception, that some code
69 // might want to handle.
74 NOTREACHED() << "Impossible delay load notification.";
78 // Returning NULL causes the delay load machinery to perform default
79 // processing for this notification.