C-bindings on windows

I have a DLL which provides several functions which I want to call inside my crystal app.

Does the C-binding work on windows ?

Can anybody recommend a shards as an example ?

It works on Windows, but there are many pitfall.
raylib is well known.

This might not be the best example, but here’s what I’ve been working on. On Windows, Link annotations often require some ingenuity.

2 Likes

If you’re still interested in examples, I also created bindings for SDL, which work flawlessly on Windows:

These are (mostly) automatically generated from header files and can then also be used with the respective DLLs.

1 Like

What kind of pitfalls? I’m not aware of C bindings on Windows being different from any other platform.

Yes, C bindings work fine on Windows. I have not seen any specific issues with Crystal itself.
I am just not very familiar with Windows, so I sometimes miss small steps. For example, I forget to open the developer console with mt.exe in the PATH. Then I get an error saying “mt.exe not found” and realize the mistake.

MSVC and MinGW handle manifest files differently, which can be confusing. DLL paths and dependency resolution can also be complicated. Without MinGW’s ldd and gdb, it would have been harder to figure out. Be careful about conflicts between msvcrt and libucrt.

Also, mixing MT and DM flags can cause issues. The Crystal official guide covers that. PDB files for debugging must also be in the correct location.

I remember that @[Link("foo", dll: "libfoo.dll")] can be used to copy DLLs.

MinGW has several variants: ucrt, clang, mingw32, and mingw64. Using mingw64 was the most stable. There may be others, but I can’t recall them now.