modelon-community/fmi-library

UTF8 file path support on Windows

joerg-schlager opened this issue · 2 comments

Hi,

in the default Windows configuration for 3.02a, jm_portability_load_dll_handle_with_flag(const char* dll_file_path, jm_portability_loadlibrary_flag_t flag) uses LoadLibraryEx(dll_file_path, NULL, flag); - there is thus no support for UTF8 characters in the dll_file_path, and loading DLLs from any path containing UTF8 characters subsequently fails. Is there any solution for support unicode file paths within fmilib?

I just drafted a simple fix for this issue (see below), and it appears to be working that way, However, I'm afraid that similar code would need to be injected all over jm_portability.c to get full UTF8 support on Windows.

DLL_HANDLE jm_portability_load_dll_handle_with_flag(const char* dll_file_path, jm_portability_loadlibrary_flag_t flag)
{
#ifdef WIN32
// first, try to load using UTF16

HMODULE h_module = NULL;
int utf8_len = (int)strlen(dll_file_path);
int utf16_buffer_len =  MultiByteToWideChar(CP_UTF8, 0, dll_file_path, utf8_len, NULL, 0);
if (utf16_buffer_len > 0)
{
    wchar_t* dll_file_path_utf16 = (wchar_t*)malloc(sizeof(wchar_t) * (utf16_buffer_len+1));
    if (dll_file_path_utf16 != NULL)
    {            
        int utf16_len = MultiByteToWideChar(CP_UTF8, 0, dll_file_path, utf8_len, dll_file_path_utf16, utf16_buffer_len);
        if (utf16_len == utf16_buffer_len)
        {
            dll_file_path_utf16[utf16_buffer_len] = L'\0';
            h_module = LoadLibraryExW(dll_file_path_utf16, NULL, flag);
        }
        free(dll_file_path_utf16);            
    }
}                
return (h_module !=NULL) ? h_module : LoadLibraryExA(dll_file_path, NULL, flag);

#else
return dlopen(dll_file_path, flag);
#endif
}

Hi @joerg-schlager, thank you for highlighting this. Unfortunately we're not able to look into this at the moment but I will keep this open for future enhancements. We are currently prioritizing remaining work for support of FMI 3.0.