-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Windows: Show more useful DLL load errors to say exactly what DLL is missing #24053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include <windows.h> | ||
| #include <dbghelp.h> | ||
| #include <iostream> | ||
| #include <string> | ||
| #pragma comment(lib, "dbghelp.lib") | ||
|
|
||
| struct HMODULE_Deleter { | ||
| typedef HMODULE pointer; | ||
| void operator()(HMODULE h) { FreeLibrary(h); } | ||
| }; | ||
|
|
||
| using ModulePtr = std::unique_ptr<HMODULE, HMODULE_Deleter>; | ||
|
|
||
| // If a DLL fails to load, this will try loading the DLL and then its dependencies recursively | ||
| // until it finds a missing file, then will report which file is missing and what the dependency | ||
| // chain is. | ||
| std::wstring DetermineLoadLibraryError(const wchar_t* filename_in) { | ||
| std::wstring error(L"Error loading"); | ||
|
|
||
| std::wstring filename{filename_in}; | ||
| while (filename.size()) { | ||
| error += std::wstring(L" \"") + filename + L"\""; | ||
|
|
||
| ModulePtr hModule = ModulePtr{LoadLibraryExW(filename.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES)}; | ||
|
tianleiwu marked this conversation as resolved.
Outdated
|
||
| if (!hModule) { | ||
| error += L" which is missing."; | ||
| return error; | ||
| } | ||
|
|
||
| // Get the address of the Import Directory | ||
| ULONG size; | ||
| PIMAGE_IMPORT_DESCRIPTOR importDesc = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData(hModule.get(), TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size); | ||
| if (!importDesc) { | ||
| error += L" No import directory found."; // This is unexpected, and I'm not sure how it could happen but we handle it just in case. | ||
| return error; | ||
| } | ||
|
|
||
| // Iterate through the import descriptors to see which dependent DLL can't load | ||
| filename.clear(); | ||
| for (; importDesc->Characteristics; importDesc++) { | ||
|
tianleiwu marked this conversation as resolved.
Outdated
|
||
| char* dllName = (char*)((BYTE*)(hModule.get()) + importDesc->Name); | ||
| ModulePtr hDepModule{LoadLibrary(dllName)}; | ||
| if (!hDepModule) { | ||
| filename = std::wstring(dllName, dllName + strlen(dllName)); | ||
|
tianleiwu marked this conversation as resolved.
Outdated
|
||
| error += L" which depends on"; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| error += L" But no dependency issue could be determined."; | ||
| return error; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| std::wstring DetermineLoadLibraryError(const wchar_t* filename); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.