authorgravatar for ryan.saunderson88@gmail.comdimenus <ryan.saunderson88@gmail.com> 2017-11-15 14:46:49-06:00
committergravatar for ryan.saunderson88@gmail.comRyan Saunderson <ryan.saunderson88@gmail.com> 2017-11-16 21:49:05-06:00
loga7d07d412c4995c7858cd65558edce64ccb91911
treec8847953bf4f8bb40e181e9ac315032784091e56
parent7a74dbadd79d2b26a449027dd83753ae4d2a8032

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;...@@ -31,6 +31,8 @@ pub const windowsWaitSingle = windows_util.windowsWaitSingle;
31pub const windowsWrite = windows_util.windowsWrite;31pub const windowsWrite = windows_util.windowsWrite;
32pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;32pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
33pub const windowsOpen = windows_util.windowsOpen;33pub const windowsOpen = windows_util.windowsOpen;
34pub const windowsLoadDll = windows_util.windowsLoadDll;
35pub const windowsUnloadDll = windows_util.windowsUnloadDll;
34pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;36pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
3537
36pub const FileHandle = if (is_windows) windows.HANDLE else i32;38pub 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...@@ -84,6 +84,11 @@ pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &con
84 in_nNumberOfBytesToWrite: DWORD, out_lpNumberOfBytesWritten: ?&DWORD,84 in_nNumberOfBytesToWrite: DWORD, out_lpNumberOfBytesWritten: ?&DWORD,
85 in_out_lpOverlapped: ?&OVERLAPPED) -> BOOL;85 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
87pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;92pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;
8893
89pub const PROV_RSA_FULL = 1;94pub const PROV_RSA_FULL = 1;
...@@ -97,6 +102,7 @@ pub const FLOAT = f32;...@@ -97,6 +102,7 @@ pub const FLOAT = f32;
97pub const HANDLE = &c_void;102pub const HANDLE = &c_void;
98pub const HCRYPTPROV = ULONG_PTR;103pub const HCRYPTPROV = ULONG_PTR;
99pub const HINSTANCE = &@OpaqueType();104pub const HINSTANCE = &@OpaqueType();
105pub const HMODULE = &@OpaqueType();
100pub const INT = c_int;106pub const INT = c_int;
101pub const LPBYTE = &BYTE;107pub const LPBYTE = &BYTE;
102pub const LPCH = &CHAR;108pub const LPCH = &CHAR;
std/os/windows/util.zig+23
...@@ -4,6 +4,7 @@ const windows = std.os.windows;...@@ -4,6 +4,7 @@ const windows = std.os.windows;
4const assert = std.debug.assert;4const assert = std.debug.assert;
5const mem = std.mem;5const mem = std.mem;
6const BufMap = std.BufMap;6const BufMap = std.BufMap;
7const cstr = std.cstr;
78
8error WaitAbandoned;9error WaitAbandoned;
9error WaitTimeOut;10error WaitTimeOut;
...@@ -149,3 +150,25 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)...@@ -149,3 +150,25 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)
149 result[i] = 0;150 result[i] = 0;
150 return result;151 return result;
151}152}
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