Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
skyline75489 committed Jul 26, 2024
1 parent 5e50d84 commit 20743b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class path {
};

#ifdef _WIN32
path(const std::wstring& path) : wpath_(path) {
};
path(const std::wstring& path) : wpath_(path) {};
#endif

static constexpr char separator =
Expand Down
2 changes: 1 addition & 1 deletion src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static SGR LabelToSgr(std::string_view label) {
}

std::ostream& Log(std::string_view label, std::string_view string) {
//assert(g_log.enabled);
if (!g_log.enabled) return *gp_stream;

*gp_stream << SGR::Bold << LabelToSgr(label) << " " << label << " " << SGR::Reset << ' ';
if (!string.empty())
Expand Down
18 changes: 12 additions & 6 deletions src/models/onnxruntime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ inline void InitApi() {
std::string("libonnxruntime.so"),
std::string("libonnxruntime.so.1.18.0"),
std::string("libonnxruntime.so.1.19.0"),
std::string("libonnxruntime.so.1.20.0")
};
std::string("libonnxruntime.so.1.20.0")};

Dl_info dl_info;
dladdr((void*)InitApi, &dl_info);
Expand Down Expand Up @@ -245,13 +244,12 @@ inline void InitApi() {
std::wstring path{};

const std::array<std::wstring, 1> target_libraries = {
std::wstring(L"onnxruntime.dll")
};
std::wstring(L"onnxruntime.dll")};

HMODULE this_dll = NULL;
WCHAR buffer[PATH_MAX];
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)&InitApi, &this_dll);
(LPCWSTR)&InitApi, &this_dll);
GetModuleFileNameW(this_dll, (LPWSTR)(&buffer), PATH_MAX);

std::wstring module_name{buffer};
Expand All @@ -261,7 +259,7 @@ inline void InitApi() {
module_directory = module_name.substr(0, last_slash_idx);
}

for (const std::wstring& lib_path : target_libraries) {
for (const std::wstring& lib_path : target_libraries) {
const fs::path system_path{lib_path};
LOG_INFO_W(L"Attempting to LoadLibrary %s", system_path.c_str());
if (system_path.exists()) {
Expand All @@ -277,6 +275,10 @@ inline void InitApi() {
}
}

if (path.empty()) {
throw std::runtime_error("Failed to find onnxruntime. Please make sure you have onnxruntime installed");
}

AddDllDirectory((module_directory + L"/../onnxruntime/capi/").c_str());

WCHAR cuda_path[PATH_MAX];
Expand All @@ -292,6 +294,10 @@ inline void InitApi() {
}

HMODULE ort_dll = LoadLibraryExW(path.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (ort_dll == nullptr) {
throw std::runtime_error(std::string("Failed to load DLL: " + GetLastError()));
}

OrtApiBaseFn ort_api_base_fn = (OrtApiBaseFn)GetProcAddress(ort_dll, "OrtGetApiBase");
InitApiWithDynamicFn(ort_api_base_fn);
#else // defined(__ANDROID__) || defined(__linux__)
Expand Down
14 changes: 9 additions & 5 deletions src/u8u16convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
#include <intsafe.h>
#include <tchar.h>


inline std::string u16u8(const std::wstring_view& in) {
// If there's nothing to convert, bail early.
if (in.empty()) {
return {};
}
int lengthIn = static_cast<int>(in.size());
const int lengthOut = WideCharToMultiByte(CP_UTF8, 0ul, in.data(), lengthIn, nullptr, 0, nullptr, nullptr);
int iSource;
SizeTToInt(in.size(), &iSource);

const int iTarget = WideCharToMultiByte(CP_UTF8, 0ul, in.data(), iSource, nullptr, 0, nullptr, nullptr);
size_t cchNeeded;
IntToSizeT(iTarget, &cchNeeded);

std::string out{};
out.resize(lengthOut);
WideCharToMultiByte(CP_UTF8, 0ul, in.data(), lengthOut, out.data(), lengthOut, NULL, NULL);
out.resize(cchNeeded);
WideCharToMultiByte(CP_UTF8, 0ul, in.data(), iSource, out.data(), iTarget, nullptr, nullptr);
return out;
}

Expand Down

0 comments on commit 20743b8

Please sign in to comment.