Wrapping a C library | Where to start?

MLackner
Wed, 17 Nov 2021 13:26:12 +0000
General Usage

Hi!

I want to use a few functions of a dll file from Julia and I want to wrap them in a package. The dlls I want to use are for talking to some motion controllers (The dlls in question can be found on thorlabs.com. There are also .h files included.

My first problem is to actually write the wrapping module. From the REPL (and also by includeing a file) I can do

julia> using Libdl
julia> dlopen("Thorlabs.MotionControl.KCube.Solenoid.dll")
Ptr{Nothing} @0x00007ff8be9b0000

with the dll file in the working directory. I am also able to call the functions via ccall.

However, if I try to access this dll file in a module (or its tests) I get an error:

could not load library "Thorlabs.MotionControl.KCube.Solenoid.dll"
  The specified module could not be found.
  Stacktrace:
    [1] open(serial_number::String)
      @ KCubeSolenoid C:\Users\Admin\.julia\dev\KCubeSolenoid\src\KCubeSolenoid.jl:9
    [2] macro expansion
      @ show.jl:955 [inlined]
    [3] macro expansion
      @ C:\Users\Admin\.julia\dev\KCubeSolenoid\test\runtests.jl:9 [inlined]
    [4] macro expansion
      @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Test\src\Test.jl:1151 [inlined]
    [5] top-level scope
      @ C:\Users\Admin\.julia\dev\KCubeSolenoid\test\runtests.jl:7
    [6] include(fname::String)
      @ Base.MainInclude .\client.jl:444
    [7] top-level scope
      @ none:6
    [8] eval
      @ .\boot.jl:360 [inlined]
    [9] exec_options(opts::Base.JLOptions)
      @ Base .\client.jl:261
   [10] _start()
      @ Base .\client.jl:485

So I guess the dll is in the wrong location? How would I actually go about and structure the wrapping package? Where should the dll file go?

I briefly tried to wrap the library with Clang.jl but it failed parsing the header file. Are there any tutorials on how to write a wrapper for a simple dll library and ship that as a Julia package?

7 posts - 4 participants