david50407
2/12/2016 - 2:42 PM

dl.cr

ifdef windows
  # already required "./winapi/kernel32.cr" in prelude
else
  require "./dl/lib_dl.cr"
end

# This is a frontend wrapper,
# using LibDL or WinApi as backend.
module DL
  ifdef windows
    @[AlwaysInline]
    def self.open(path)
      WinApi.load_library(path)
    end
    # If specifics flags, calling LoadLibraryEx instead.
    @[AlwaysInline]
    def self.open(path, flags = 0)
      WinApi.load_library_ex(path, nil, flags)
    end

    @[AlwaysInline]
    def self.sym(handle, name)
      WinApi.get_proc_address(handle, name)
    end
  else
    @[AlwaysInline]
    def self.open(path, mode = LibDL::LAZY | LibDL::GLOBAL)
      LibDL.dlopen(path, mode)
    end

    @[AlwaysInline]
    def self.sym(handle, symbol)
      LibDL.dlsym(handle, symbol)
    end

    @[AlwaysInline]
    def self.addr(addr, info)
      LibDL.dladdr(addr, info)
    end
  end
end