1 // Copyright 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/browser/install_verification/win/loaded_modules_snapshot.h"
9 #include "base/logging.h"
11 bool GetLoadedModulesSnapshot(std::vector
<HMODULE
>* snapshot
) {
13 DCHECK_EQ(0u, snapshot
->size());
16 HANDLE process
= ::GetCurrentProcess();
18 // We will retry at least once after first determining |bytes_required|. If
19 // the list of modules changes after we receive |bytes_required| we may retry
21 int retries_remaining
= 5;
23 DWORD bytes_required
= 0;
24 // EnumProcessModules returns 'success' even if the buffer size is too
26 if (!::EnumProcessModules(process
,
28 snapshot
->size() * sizeof(HMODULE
),
30 DPLOG(ERROR
) << "::EnumProcessModules failed.";
33 DCHECK_EQ(0u, bytes_required
% sizeof(HMODULE
));
34 size_t num_modules
= bytes_required
/ sizeof(HMODULE
);
35 if (num_modules
<= snapshot
->size()) {
36 // Buffer size was too big, presumably because a module was unloaded.
37 snapshot
->erase(snapshot
->begin() + num_modules
, snapshot
->end());
39 } else if (num_modules
== 0) {
40 DLOG(ERROR
) << "Can't determine the module list size.";
43 // Buffer size was too small. Try again with a larger buffer.
44 snapshot
->resize(num_modules
, NULL
);
46 } while (--retries_remaining
);
48 DLOG(ERROR
) << "Failed to enumerate modules.";