修复Windows编译问题

This commit is contained in:
2025-03-06 16:07:47 +08:00
parent c1e666ce5b
commit dc94573862
5 changed files with 98 additions and 98 deletions

View File

@ -3,6 +3,12 @@
#include <onnxruntime_cxx_api.h>
#include "node.h"
#ifdef WIN32
#include <locale>
#include <codecvt>
#include <Windows.h>
#endif
using namespace Napi;
#define SESSION_INSTANCE_METHOD(method) InstanceMethod<&OrtSession::method>(#method, static_cast<napi_property_attributes>(napi_writable | napi_configurable))
@ -168,7 +174,23 @@ class OrtSession : public ObjectWrap<OrtSession> {
: ObjectWrap(info)
{
try {
if (info[0].IsString()) session_ = std::make_shared<Ort::Session>(env, info[0].As<String>().Utf8Value().c_str(), sessionOptions);
if (info[0].IsString()) {
#ifdef WIN32
std::string str = info[0].As<String>().Utf8Value();
auto len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
wchar_t *buffer = new wchar_t[len + 1];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0';
std::wstring filename(buffer);
delete[] buffer;
// std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
// std::wstring filename = converter.from_bytes(info[0].As<String>().Utf8Value());
session_ = std::make_shared<Ort::Session>(env, filename.c_str(), sessionOptions);
#else
session_ = std::make_shared<Ort::Session>(env, info[0].As<String>().Utf8Value().c_str(), sessionOptions);
#endif
}
else if (info[0].IsTypedArray()) {
size_t bufferBytes;
auto buffer = dataFromTypedArray(info[0], bufferBytes);