aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-11-17 10:24:34 -0500
committerGitHub <noreply@github.com>2017-11-17 10:24:34 -0500
commit3e835973db361ebdb35d34c371870acd13d95e17 (patch)
tree9c564d13a9f98e89946ef56dbda41db44acb4cb7 /std
parentb50c676f760272b014e3c6320b13b979bfc864c7 (diff)
parenta7d07d412c4995c7858cd65558edce64ccb91911 (diff)
downloadzig-3e835973db361ebdb35d34c371870acd13d95e17.tar.gz
zig-3e835973db361ebdb35d34c371870acd13d95e17.zip
Merge pull request #617 from dimenus/dll-load
Added DLL loading capability in windows to the std lib.
Diffstat (limited to 'std')
-rw-r--r--std/os/index.zig2
-rw-r--r--std/os/windows/index.zig6
-rw-r--r--std/os/windows/util.zig23
3 files changed, 31 insertions, 0 deletions
diff --git a/std/os/index.zig b/std/os/index.zig
index c3f33c4ccd..34de6f3bbf 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -31,6 +31,8 @@ pub const windowsWaitSingle = windows_util.windowsWaitSingle;
pub const windowsWrite = windows_util.windowsWrite;
pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
pub const windowsOpen = windows_util.windowsOpen;
+pub const windowsLoadDll = windows_util.windowsLoadDll;
+pub const windowsUnloadDll = windows_util.windowsUnloadDll;
pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
pub const FileHandle = if (is_windows) windows.HANDLE else i32;
diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig
index 913cc79801..0ce3794cdc 100644
--- a/std/os/windows/index.zig
+++ b/std/os/windows/index.zig
@@ -84,6 +84,11 @@ pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &con
in_nNumberOfBytesToWrite: DWORD, out_lpNumberOfBytesWritten: ?&DWORD,
in_out_lpOverlapped: ?&OVERLAPPED) -> BOOL;
+//TODO: call unicode versions instead of relying on ANSI code page
+pub extern "kernel32" stdcallcc fn LoadLibraryA(lpLibFileName: LPCSTR) -> ?HMODULE;
+
+pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) -> BOOL;
+
pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;
pub const PROV_RSA_FULL = 1;
@@ -97,6 +102,7 @@ pub const FLOAT = f32;
pub const HANDLE = &c_void;
pub const HCRYPTPROV = ULONG_PTR;
pub const HINSTANCE = &@OpaqueType();
+pub const HMODULE = &@OpaqueType();
pub const INT = c_int;
pub const LPBYTE = &BYTE;
pub const LPCH = &CHAR;
diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig
index de2babe8d7..b3fc095d43 100644
--- a/std/os/windows/util.zig
+++ b/std/os/windows/util.zig
@@ -4,6 +4,7 @@ const windows = std.os.windows;
const assert = std.debug.assert;
const mem = std.mem;
const BufMap = std.BufMap;
+const cstr = std.cstr;
error WaitAbandoned;
error WaitTimeOut;
@@ -149,3 +150,25 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)
result[i] = 0;
return result;
}
+
+error DllNotFound;
+pub fn windowsLoadDll(allocator: &mem.Allocator, dll_path: []const u8) -> %windows.HMODULE {
+ const padded_buff = %return cstr.addNullByte(allocator, dll_path);
+ defer allocator.free(padded_buff);
+ return windows.LoadLibraryA(padded_buff.ptr) ?? error.DllNotFound;
+}
+
+pub fn windowsUnloadDll(hModule: windows.HMODULE) {
+ assert(windows.FreeLibrary(hModule)!= 0);
+}
+
+
+test "InvalidDll" {
+ const DllName = "asdf.dll";
+ const allocator = std.debug.global_allocator;
+ const handle = os.windowsLoadDll(allocator, DllName) %% |err| {
+ assert(err == error.DllNotFound);
+ return;
+ };
+}
+