DirectML.dll load fails when executable path contains Non-English characters (#9229)

* enable unicode dml

* add wide string L prefix

* Add Fail Fast back

Co-authored-by: Sheil Kumar <sheilk@microsoft.com>
This commit is contained in:
Sheil Kumar 2021-09-30 15:16:57 -07:00 committed by GitHub
parent 634bb5ede0
commit c6cb49c5a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,30 +22,25 @@ namespace winmla = Windows::AI::MachineLearning::Adapter;
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
static std::string CurrentModulePath() {
char path[MAX_PATH];
FAIL_FAST_IF(0 == GetModuleFileNameA((HINSTANCE)&__ImageBase, path, _countof(path)));
static std::wstring CurrentModulePath() {
WCHAR path[MAX_PATH];
FAIL_FAST_IF(0 == GetModuleFileNameW((HINSTANCE)&__ImageBase, path, _countof(path)));
char absolute_path[MAX_PATH];
char* name;
FAIL_FAST_IF(0 == GetFullPathNameA(path, _countof(path), absolute_path, &name));
WCHAR absolute_path[MAX_PATH];
WCHAR* name;
FAIL_FAST_IF(0 == GetFullPathNameW(path, _countof(path), absolute_path, &name));
auto idx = std::distance(absolute_path, name);
auto out_path = std::string(absolute_path);
out_path.resize(idx);
auto idx = std::distance(absolute_path, name);
auto out_path = std::wstring(absolute_path);
out_path.resize(idx);
return out_path;
return out_path;
}
Microsoft::WRL::ComPtr<IDMLDevice> CreateDmlDevice(ID3D12Device* d3d12Device) {
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
// Dynamically load DML to avoid WinML taking a static dependency on DirectML.dll
auto directml_dll = CurrentModulePath() + "\\DirectML.dll";
wil::unique_hmodule dmlDll(LoadLibraryExA(directml_dll.c_str(), nullptr, 0));
#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PC_APP)
wil::unique_hmodule dmlDll(LoadPackagedLibrary(L"DirectML.dll", 0));
#endif
auto directml_dll = CurrentModulePath() + L"DirectML.dll";
wil::unique_hmodule dmlDll(LoadLibraryExW(directml_dll.c_str(), nullptr, 0));
THROW_LAST_ERROR_IF(!dmlDll);
auto dmlCreateDevice1Fn = reinterpret_cast<decltype(&DMLCreateDevice1)>(