pub const DlDynLib = struct handle: *anyopaqueSeparated to avoid referencing DlDynLib, because its field types may not be valid on other targets.
pub fn open(path: []const u8) Error!DlDynLibpub fn openZ(path_c: [*: 0]const u8) Error!DlDynLibpub fn close(self: *DlDynLib) voidpub fn lookup(self: *DlDynLib, comptime T: type, name: [: 0]const u8) ?Tpub fn getError() ?[: 0]const u8DlDynLib specific Returns human readable string describing most recent error than occurred from lookup or null if no error has occurred since initialization or when getError was last called.
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593pub const DlDynLib = struct {
pub const Error = DlDynLibError;
handle: *anyopaque,
pub fn open(path: []const u8) Error!DlDynLib {
const path_c = try posix.toPosixPath(path);
return openZ(&path_c);
}
pub fn openZ(path_c: [*:0]const u8) Error!DlDynLib {
return .{
.handle = std.c.dlopen(path_c, .{ .LAZY = true }) orelse {
return error.FileNotFound;
},
};
}
pub fn close(self: *DlDynLib) void {
switch (posix.errno(std.c.dlclose(self.handle))) {
.SUCCESS => return,
else => unreachable,
}
self.* = undefined;
}
pub fn lookup(self: *DlDynLib, comptime T: type, name: [:0]const u8) ?T {
// dlsym (and other dl-functions) secretly take shadow parameter - return address on stack
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826
if (@call(.never_tail, std.c.dlsym, .{ self.handle, name.ptr })) |symbol| {
return @as(T, @ptrCast(@alignCast(symbol)));
} else {
return null;
}
}
/// DlDynLib specific
/// Returns human readable string describing most recent error than occurred from `lookup`
/// or `null` if no error has occurred since initialization or when `getError` was last called.
pub fn getError() ?[:0]const u8 {
return mem.span(std.c.dlerror());
}
}