Skip to content

Add dynamic path resolution for Windows - #36

Merged
thep merged 12 commits into
tlwg:masterfrom
bact:win32-dynamic-path
Jul 30, 2026
Merged

Add dynamic path resolution for Windows#36
thep merged 12 commits into
tlwg:masterfrom
bact:win32-dynamic-path

Conversation

@bact

@bact bact commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Find .dll for Windows to fix #7 and fix #17

Add win_inst_dir to handle path resolution for Windows.

Uses standard Windows APIs (partly based on #12 by @fanc999 and from @thep 's suggestion in #36 (comment)):

  • GetModuleHandleExW: To get the DLL module handle.
  • GetModuleFileNameW: To retrieve the DLL's installation path (handle Unicode characters)

Tested with mingw-w64 (cross-compiled), macOS, Ubuntu.

--

Path resolution flow:

# Step Function Output
1 Get DLL module handle GetModuleHandleExW (in win_inst_dir) DLL handle, or NULL on failure
2 Get DLL full path GetModuleFileNameW (in win_inst_dir) DLL path = C:\App\bin\libthai.dll or C:\App2\libthai.dll, or NULL on failure
3 Get install path win_find_inst_dir Install dir = C:\App (remove "bin") or C:\App2 (no "bin" found, just use the full dir), or NULL on malloc failure
4 Guess dictionary path wfull_path (in brk_load_default_dict) Dict path = C:\App\share\libthai\thbrk.tri or C:\App2\share\libthai\thbrk.tri, or NULL on malloc failure
5 Open guessed path _wfopen (in brk_load_default_dict) File handle, or NULL if no file exists there
6 Parse dictionary trie_fread (in brk_load_default_dict) Trie *dict_trie, or NULL if the file is bad
7 Fall back on any failure in 1-6 trie_new_from_file(DICT_DIR "/" DICT_NAME ".tri") (in brk_load_default_dict) Likely fails on Windows, but will bring us to regular fallback path before this patch

bact and others added 4 commits July 13, 2026 11:48
Find .dll for Windows

To fix tlwg#7

Partly based on tlwg#12

Co-Authored-By: fanc999 <9366079+fanc999@users.noreply.github.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@bact

bact commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Btw, not sure if it is ok to add th_get_win32_installdir to priv-utils.h.

I can create a separate file (dir-utils.h?), if priv-utils.h is not a good location to host path utils.

@bact bact mentioned this pull request Jul 13, 2026
@bact

bact commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Note that #12 and #30 also solve the same issue that this PR is trying to solve.

But this PR only focuses on path resolution, not an entirely new build process. Comparisons:

Comment thread src/thbrk/brk-common.c
Comment thread src/libthai.c Outdated
Comment thread src/libthai.c Outdated
Comment thread src/libthai.c Outdated
Comment thread src/libthai.c Outdated
Co-Authored-By: Theppitak Karoonboonyanan <631551+thep@users.noreply.github.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@bact
bact marked this pull request as draft July 14, 2026 15:55
bact and others added 4 commits July 15, 2026 15:32
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
From reviewer's code suggestion

Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Co-Authored-By: Theppitak Karoonboonyanan <631551+thep@users.noreply.github.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Co-Authored-By: Theppitak Karoonboonyanan <631551+thep@users.noreply.github.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@bact
bact marked this pull request as ready for review July 15, 2026 16:28
@bact
bact marked this pull request as draft July 15, 2026 16:41
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@bact
bact marked this pull request as ready for review July 15, 2026 22:34
@bact

bact commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Updated the install dir finding algorithm per reviewer suggestion, with some adaptation for wide-char path in Windows. (see the flow table in the updated PR description for general idea; Step 3-4 are heuristic)

@thep the libthai.c is now crowded with functions for Windows, do you have concerns on that? Or should we move them to a location specifically for utility functions?

Comment thread src/thbrk/brk-common.c Outdated
@thep

thep commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

@thep the libthai.c is now crowded with functions for Windows, do you have concerns on that? Or should we move them to a location specifically for utility functions?

Yes, it should be good to have the Windows-specific stuffs in a separate file.

Meanwhile, the static pointer cached_dir in win_inst_dir() is not freed on unload. It should be cleaner to free it somehow.

@thep

thep commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

It seems one comment I made before has been lost. So, let me repeat it here:

In brk_load_default_dict() [brk-common.c], this part:

   /* Then, fall back to default DICT_DIR macro */
    if (!dict_trie) {
#if defined (_WIN32) && !defined (__CYGWIN__)
        {
            /* ... */
        }
        if (!dict_trie)
            dict_trie = trie_new_from_file (DICT_DIR "/" DICT_NAME ".tri");
#else
        dict_trie = trie_new_from_file (DICT_DIR "/" DICT_NAME ".tri");
#endif
    }

can be rearranged like this:

#if defined (_WIN32) && !defined (__CYGWIN__)
    /* Try to find dict under the base dir used to install the DLL */
    if (!dict_trie) {
        /* ... */
    }
#endif

   /* Then, fall back to default DICT_DIR macro */
    if (!dict_trie) {
        dict_trie = trie_new_from_file (DICT_DIR "/" DICT_NAME ".tri");
    }

That is, the Windows-specific stuff becomes just another step in between.

Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Comment thread src/Makefile.am Outdated
endif

libthai_la_SOURCES = libthai.c
libthai_la_SOURCES = libthai.c utils/win-utils.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not quite good to refer to a file from different directory in Makefile.am. For example, this makes it more complicated to check whether a file is included in the release tarball prepared with make dist.

Instead, we may need to create another internal library libutils.la under the utils/ subdir. That is:

  • In src/Makefile.am, add $(top_builddir)/src/utils/libutils.la to libthai_sublibs. Previously, the utils/ subdir just provided a plain header. Now it does provide some binary code.
  • In src/utils/Makefile.am, replace the noinst_HEADERS target with this (with win-utils.h split out of priv-utils.h):
noinst_LTLIBRARIES = libutils.la

libutils_la_SOURCES = \
        priv-utils.h \
        win-utils.h \
        win-utils.c \
        $(NULL)

Some source files that previously included priv-utils.h for the Windows stuff may need to include win-utils.h instead, of course.

Per suggestions in review

Co-Authored-By: Theppitak Karoonboonyanan <631551+thep@users.noreply.github.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@thep
thep merged commit 934fd50 into tlwg:master Jul 30, 2026
@thep

thep commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Looks good. Merged. Thank you very much for your work!

thep added a commit that referenced this pull request Jul 30, 2026
@bact
bact deleted the win32-dynamic-path branch July 30, 2026 19:41
@bact

bact commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

portable win32 installation issues Crashing on Windows

2 participants