Skip to content

Commit 3e83597

Browse files
authored
Merge pull request #617 from dimenus/dll-load
Added DLL loading capability in windows to the std lib.
2 parents b50c676 + a7d07d4 commit 3e83597

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

std/os/index.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub const windowsWaitSingle = windows_util.windowsWaitSingle;
3131
pub const windowsWrite = windows_util.windowsWrite;
3232
pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
3333
pub const windowsOpen = windows_util.windowsOpen;
34+
pub const windowsLoadDll = windows_util.windowsLoadDll;
35+
pub const windowsUnloadDll = windows_util.windowsUnloadDll;
3436
pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
3537

3638
pub const FileHandle = if (is_windows) windows.HANDLE else i32;

std/os/windows/index.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &con
8484
in_nNumberOfBytesToWrite: DWORD, out_lpNumberOfBytesWritten: ?&DWORD,
8585
in_out_lpOverlapped: ?&OVERLAPPED) -> BOOL;
8686

87+
//TODO: call unicode versions instead of relying on ANSI code page
88+
pub extern "kernel32" stdcallcc fn LoadLibraryA(lpLibFileName: LPCSTR) -> ?HMODULE;
89+
90+
pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) -> BOOL;
91+
8792
pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;
8893

8994
pub const PROV_RSA_FULL = 1;
@@ -97,6 +102,7 @@ pub const FLOAT = f32;
97102
pub const HANDLE = &c_void;
98103
pub const HCRYPTPROV = ULONG_PTR;
99104
pub const HINSTANCE = &@OpaqueType();
105+
pub const HMODULE = &@OpaqueType();
100106
pub const INT = c_int;
101107
pub const LPBYTE = &BYTE;
102108
pub const LPCH = &CHAR;

std/os/windows/util.zig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const windows = std.os.windows;
44
const assert = std.debug.assert;
55
const mem = std.mem;
66
const BufMap = std.BufMap;
7+
const cstr = std.cstr;
78

89
error WaitAbandoned;
910
error WaitTimeOut;
@@ -149,3 +150,25 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)
149150
result[i] = 0;
150151
return result;
151152
}
153+
154+
error DllNotFound;
155+
pub fn windowsLoadDll(allocator: &mem.Allocator, dll_path: []const u8) -> %windows.HMODULE {
156+
const padded_buff = %return cstr.addNullByte(allocator, dll_path);
157+
defer allocator.free(padded_buff);
158+
return windows.LoadLibraryA(padded_buff.ptr) ?? error.DllNotFound;
159+
}
160+
161+
pub fn windowsUnloadDll(hModule: windows.HMODULE) {
162+
assert(windows.FreeLibrary(hModule)!= 0);
163+
}
164+
165+
166+
test "InvalidDll" {
167+
const DllName = "asdf.dll";
168+
const allocator = std.debug.global_allocator;
169+
const handle = os.windowsLoadDll(allocator, DllName) %% |err| {
170+
assert(err == error.DllNotFound);
171+
return;
172+
};
173+
}
174+

0 commit comments

Comments
 (0)