diff options
| author | emekoi <emekankurumeh@outlook.com> | 2018-09-27 16:51:22 -0500 |
|---|---|---|
| committer | emekoi <emekankurumeh@outlook.com> | 2018-09-30 01:05:13 -0500 |
| commit | 623f5085f152b3a2fedf3b4e4451910f1edb6739 (patch) | |
| tree | fedd67d54f83bc81690c9c9cf26e98240fabe418 /std/dynamic_library.zig | |
| parent | 42ba206c5d0701989f15d554d89c7d84667a7086 (diff) | |
| download | zig-623f5085f152b3a2fedf3b4e4451910f1edb6739.tar.gz zig-623f5085f152b3a2fedf3b4e4451910f1edb6739.zip | |
merged windows dll apis
Diffstat (limited to 'std/dynamic_library.zig')
| -rw-r--r-- | std/dynamic_library.zig | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/std/dynamic_library.zig b/std/dynamic_library.zig index 10068e83a8..c0922003ce 100644 --- a/std/dynamic_library.zig +++ b/std/dynamic_library.zig @@ -1,11 +1,15 @@ const builtin = @import("builtin"); const Os = builtin.Os; -const std = @import("std"); + +const std = @import("index.zig"); const mem = std.mem; -const elf = std.elf; const cstr = std.cstr; -const linux = std.os.linux; -const windows = std.os.windows; +const os = std.os; +const assert = std.debug.assert; +const elf = std.elf; +const linux = os.linux; +const windows = os.windows; +const win_util = @import("os/windows/util.zig"); pub const DynLib = switch (builtin.os) { Os.linux => LinuxDynLib, @@ -169,17 +173,24 @@ pub const WindowsDynLib = struct { dll: windows.HMODULE, pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib { - const wpath = try std.unicode.utf8ToUtf16LeWithNull(allocator, path); - defer allocator.free(wpath); + const wpath = try win_util.sliceToPrefixedFileW(path); return WindowsDynLib { .allocator = allocator, - .dll = windows.LoadLibraryW(wpath[0..].ptr) orelse return error.FileNotFound + .dll = windows.LoadLibraryW(&wpath) orelse { + const err = windows.GetLastError(); + switch (err) { + windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, + windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, + windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound, + else => return os.unexpectedErrorWindows(err), + } + }, }; } pub fn close(self: *WindowsDynLib) void { - _ = windows.FreeLibrary(self.dll); + assert(windows.FreeLibrary(self.dll) != 0); self.* = undefined; } @@ -187,3 +198,17 @@ pub const WindowsDynLib = struct { return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr)); } }; + +test "dynamic_library" { + const libname = switch (builtin.os) { + Os.linux => "invalid_so.so", + Os.windows => "invalid_dll.dll", + else => return;, + }; + + const dynlib = DynLib.open(std.debug.global_allocator, libname) catch |err| { + assert(err == error.FileNotFound); + return; + }; + @panic("Expected error from function"); +} |
