| author | |
| committer | |
| log | 3e835973db361ebdb35d34c371870acd13d95e17 |
| tree | 9c564d13a9f98e89946ef56dbda41db44acb4cb7 |
| parent | b50c676f760272b014e3c6320b13b979bfc864c7 |
| parent | a7d07d412c4995c7858cd65558edce64ccb91911 |
| signature | Signed by PGP key 4AEE18F83AFDEB23 |
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; |
| 31 | pub const windowsWrite = windows_util.windowsWrite; | 31 | pub const windowsWrite = windows_util.windowsWrite; |
| 32 | pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty; | 32 | pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty; |
| 33 | pub const windowsOpen = windows_util.windowsOpen; | 33 | pub const windowsOpen = windows_util.windowsOpen; |
| 34 | pub const windowsLoadDll = windows_util.windowsLoadDll; | ||
| 35 | pub const windowsUnloadDll = windows_util.windowsUnloadDll; | ||
| 34 | pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock; | 36 | pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock; |
| 35 | 37 | ||
| 36 | pub const FileHandle = if (is_windows) windows.HANDLE else i32; | 38 | pub 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; |
| 86 | 86 | ||
| 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 | |||
| 87 | pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int; | 92 | pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int; |
| 88 | 93 | ||
| 89 | pub const PROV_RSA_FULL = 1; | 94 | pub const PROV_RSA_FULL = 1; |
| ... | @@ -97,6 +102,7 @@ pub const FLOAT = f32; | ... | @@ -97,6 +102,7 @@ pub const FLOAT = f32; |
| 97 | pub const HANDLE = &c_void; | 102 | pub const HANDLE = &c_void; |
| 98 | pub const HCRYPTPROV = ULONG_PTR; | 103 | pub const HCRYPTPROV = ULONG_PTR; |
| 99 | pub const HINSTANCE = &@OpaqueType(); | 104 | pub const HINSTANCE = &@OpaqueType(); |
| 105 | pub const HMODULE = &@OpaqueType(); | ||
| 100 | pub const INT = c_int; | 106 | pub const INT = c_int; |
| 101 | pub const LPBYTE = &BYTE; | 107 | pub const LPBYTE = &BYTE; |
| 102 | pub const LPCH = &CHAR; | 108 | pub 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; |
| 4 | const assert = std.debug.assert; | 4 | const assert = std.debug.assert; |
| 5 | const mem = std.mem; | 5 | const mem = std.mem; |
| 6 | const BufMap = std.BufMap; | 6 | const BufMap = std.BufMap; |
| 7 | const cstr = std.cstr; | ||
| 7 | 8 | ||
| 8 | error WaitAbandoned; | 9 | error WaitAbandoned; |
| 9 | error WaitTimeOut; | 10 | error 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 | |||
| 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 |