Skip to main content
Log in

Mojo struct

DLHandle

@register_passable(trivial) struct DLHandle

Represents a dynamically linked library that can be loaded and unloaded.

The library is loaded on initialization and unloaded by close.

Fields

  • handle (UnsafePointer[NoneType]): The handle to the dynamic library.

Implemented traits

AnyType, Boolable, CollectionElement, CollectionElementNew, Copyable, ExplicitlyCopyable, Movable, UnknownDestructibility

Methods

__init__

__init__(out self, path: String, flags: Int = Int(2.__or__(256 if eq(:string target_get_field(current_target(), "os"), "linux") else 8)))

Initialize a DLHandle object by loading the dynamic library at the given path.

Args:

  • path (String): The path to the dynamic library file.
  • flags (Int): The flags to load the dynamic library.

__init__(out self, *, other: Self)

Copy the object.

Args:

  • other (Self): The value to copy.

__bool__

__bool__(self) -> Bool

Checks if the handle is valid.

Returns:

True if the DLHandle is not null and False otherwise.

check_symbol

check_symbol(self, name: String) -> Bool

Check that the symbol exists in the dynamic library.

Args:

  • name (String): The symbol to check.

Returns:

True if the symbol exists.

close

close(mut self)

Delete the DLHandle object unloading the associated dynamic library.

get_function

get_function[result_type: AnyTrivialRegType](self, name: String) -> result_type

Returns a handle to the function with the given name in the dynamic library.

Parameters:

  • result_type (AnyTrivialRegType): The type of the function pointer to return.

Args:

  • name (String): The name of the function to get the handle for.

Returns:

A handle to the function.

get_symbol

get_symbol[result_type: AnyType](self, name: StringLiteral) -> UnsafePointer[result_type]

Returns a pointer to the symbol with the given name in the dynamic library.

Parameters:

  • result_type (AnyType): The type of the symbol to return.

Args:

  • name (StringLiteral): The name of the symbol to get the handle for.

Returns:

A pointer to the symbol.

get_symbol[result_type: AnyType](self, name: UnsafePointer[SIMD[int8, 1]]) -> UnsafePointer[result_type]

Returns a pointer to the symbol with the given name in the dynamic library.

Parameters:

  • result_type (AnyType): The type of the symbol to return.

Args:

  • name (UnsafePointer[SIMD[int8, 1]]): The name of the symbol to get the handle for.

Returns:

A pointer to the symbol.

call

call[name: StringLiteral, return_type: AnyTrivialRegType = NoneType, *T: AnyType = *?](self, *args: *T) -> return_type

Call a function with any amount of arguments.

Parameters:

  • name (StringLiteral): The name of the function.
  • return_type (AnyTrivialRegType): The return type of the function.
  • *T (AnyType): The types of args.

Args:

  • *args (*T): The arguments.

Returns:

The result.

call[name: StringLiteral, return_type: AnyTrivialRegType = NoneType](self, args: VariadicPack[origin, AnyType, element_types]) -> return_type

Call a function with any amount of arguments.

Parameters:

  • name (StringLiteral): The name of the function.
  • return_type (AnyTrivialRegType): The return type of the function.

Args:

  • args (VariadicPack[origin, AnyType, element_types]): The arguments.

Returns:

The result.