authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-17 10:24:34-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2017-11-17 10:24:34-05:00
log3e835973db361ebdb35d34c371870acd13d95e17
tree9c564d13a9f98e89946ef56dbda41db44acb4cb7
parentb50c676f760272b014e3c6320b13b979bfc864c7
parenta7d07d412c4995c7858cd65558edce64ccb91911
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #617 from dimenus/dll-load

Added DLL loading capability in windows to the std lib.

3 files changed, 31 insertions(+), 0 deletions(-)

std/os/index.zig+2
......@@ -31,6 +31,8 @@ pub const windowsWaitSingle = windows_util.windowsWaitSingle;
3131pub const windowsWrite = windows_util.windowsWrite;
3232pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
3333pub const windowsOpen = windows_util.windowsOpen;
34pub const windowsLoadDll = windows_util.windowsLoadDll;
35pub const windowsUnloadDll = windows_util.windowsUnloadDll;
3436pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
3537
3638pub const FileHandle = if (is_windows) windows.HANDLE else i32;
std/os/windows/index.zig+6
......@@ -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
88pub extern "kernel32" stdcallcc fn LoadLibraryA(lpLibFileName: LPCSTR) -> ?HMODULE;
89
90pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) -> BOOL;
91
8792pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;
8893
8994pub const PROV_RSA_FULL = 1;
......@@ -97,6 +102,7 @@ pub const FLOAT = f32;
97102pub const HANDLE = &c_void;
98103pub const HCRYPTPROV = ULONG_PTR;
99104pub const HINSTANCE = &@OpaqueType();
105pub const HMODULE = &@OpaqueType();
100106pub const INT = c_int;
101107pub const LPBYTE = &BYTE;
102108pub const LPCH = &CHAR;
std/os/windows/util.zig+23
......@@ -4,6 +4,7 @@ const windows = std.os.windows;
44const assert = std.debug.assert;
55const mem = std.mem;
66const BufMap = std.BufMap;
7const cstr = std.cstr;
78
89error WaitAbandoned;
910error WaitTimeOut;
......@@ -149,3 +150,25 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)
149150 result[i] = 0;
150151 return result;
151152}
153
154error DllNotFound;
155pub 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
161pub fn windowsUnloadDll(hModule: windows.HMODULE) {
162 assert(windows.FreeLibrary(hModule)!= 0);
163}
164
165
166test "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