Chromium 加载chrome.dll过程分析c++
chrome.exe加载同级版本号目录-》chrome.exe和chrome.dll同级目录。
一、优先从chrome.exe同级版本号目录下加载
例如:chrome.exe [版本号:129.0.6668.101] 从129.0.6668.101\chrome.dll下加载
发布版大多数都是以此种办法,主要是方便升级时候,防止不同版本号chrome.exe加载错误的chrome.dll。
二、 chrome.exe和chrome.dll在同目录直接加载。
三、看下代码逻辑:
F:\code\google\src\chrome\app\main_dll_loader_win.cc
// Returns the full path to |module_name|. Both dev builds (where |module_name|
// is in the current executable's directory) and proper installs (where
// |module_name| is in a versioned sub-directory of the current executable's
// directory) are supported. The identified file is not guaranteed to exist.
base::FilePath GetModulePath(std::wstring_view module_name) {
base::FilePath exe_dir;
const bool has_path = base::PathService::Get(base::DIR_EXE, &exe_dir);
DCHECK(has_path);
// Look for the module in a versioned sub-directory of the current
// executable's directory and return the path if it can be read. This is the
// expected location of modules for proper installs.
const base::FilePath module_path =
exe_dir.AppendASCII(chrome::kChromeVersion).Append(module_name);
if (ModuleCanBeRead(module_path))
return module_path;
// Othwerwise, return the path to the module in the current executable's
// directory. This is the expected location of modules for dev builds.
return exe_dir.Append(module_name);
}
1、 在base::FilePath GetModulePath(std::wstring_view module_name)函数中优先获取chrome.exe所在路径:F:\code\google\src\out\Debug
2、版本号目录F:\code\google\src\out\Debug\122.0.6224.0\chrome.dll
3、如果122.0.6224.0\chrome.dll目录下没有crome.dll直接返回F:\code\google\src\out\Debug
4、最终结果是加载F:\code\google\src\out\Debug\chrome.dll
// Prefetches and loads |module| after setting the CWD to |module|'s
// directory. Returns a handle to the loaded module on success, or nullptr on
// failure.
HMODULE LoadModuleWithDirectory(const base::FilePath& module,
const base::CommandLine& cmd_line) {
::SetCurrentDirectoryW(module.DirName().value().c_str());
if (!cmd_line.HasSwitch(switches::kNoPreReadMainDll)) {
base::PreReadFile(module, /*is_executable=*/true);
}
HMODULE handle = ::LoadLibraryExW(module.value().c_str(), nullptr,
LOAD_WITH_ALTERED_SEARCH_PATH);
return handle;
}
// Prefetches and loads the appropriate DLL for the process type
// |process_type_|. Populates |module| with the path of the loaded DLL.
// Returns a handle to the loaded DLL, or nullptr on failure.
HMODULE Load(base::FilePath* module, const base::CommandLine& cmd_line) {
*module = GetModulePath(installer::kChromeDll);
if (module->empty()) {
PLOG(ERROR) << "Cannot find module " << installer::kChromeDll;
return nullptr;
}
HMODULE dll = LoadModuleWithDirectory(*module, cmd_line);
if (!dll)
PLOG(ERROR) << "Failed to load Chrome DLL from " << module->value();
return dll;
}