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 (EndsWith(dll_name
, kDelaySuffix
, false)) {
32 // Trim the "-delay.dll" suffix from the string.
33 dll_name
.resize(dll_name
.length() - (sizeof(kDelaySuffix
) - 1));
34 dll_name
.append(".dll");
36 return reinterpret_cast<FARPROC
>(::LoadLibraryA(dll_name
.c_str()));
44 // This function is a delay load notification hook. It is invoked by the
45 // delay load support in the visual studio runtime.
46 // See http://msdn.microsoft.com/en-us/library/z9h1h6ty(v=vs.100).aspx for
48 extern "C" FARPROC WINAPI
ChromeDelayLoadHook(unsigned reason
,
49 DelayLoadInfo
* info
) {
51 case dliNoteStartProcessing
:
52 case dliNoteEndProcessing
:
53 // Nothing to do here.
56 case dliNotePreLoadLibrary
:
57 return OnPreLoadLibrary(info
);
60 case dliNotePreGetProcAddress
:
61 // Nothing to do here.
66 // Returning NULL from error notifications will cause the delay load
67 // runtime to raise a VcppException structured exception, that some code
68 // might want to handle.
73 NOTREACHED() << "Impossible delay load notification.";
77 // Returning NULL causes the delay load machinery to perform default
78 // processing for this notification.