authorgravatar for emekankurumeh@outlook.comemekoi <emekankurumeh@outlook.com> 2018-09-27 16:51:22-05:00
committergravatar for emekankurumeh@outlook.comemekoi <emekankurumeh@outlook.com> 2018-09-30 01:05:13-05:00
log623f5085f152b3a2fedf3b4e4451910f1edb6739
treefedd67d54f83bc81690c9c9cf26e98240fabe418
parent42ba206c5d0701989f15d554d89c7d84667a7086

merged windows dll apis


4 files changed, 50 insertions(+), 58 deletions(-)

std/dynamic_library.zig+33-8
......@@ -1,11 +1,15 @@
11const builtin = @import("builtin");
22const Os = builtin.Os;
3const std = @import("std");
3
4const std = @import("index.zig");
45const mem = std.mem;
5const elf = std.elf;
66const cstr = std.cstr;
7const linux = std.os.linux;
8const windows = std.os.windows;
7const os = std.os;
8const assert = std.debug.assert;
9const elf = std.elf;
10const linux = os.linux;
11const windows = os.windows;
12const win_util = @import("os/windows/util.zig");
913
1014pub const DynLib = switch (builtin.os) {
1115 Os.linux => LinuxDynLib,
......@@ -169,17 +173,24 @@ pub const WindowsDynLib = struct {
169173 dll: windows.HMODULE,
170174
171175 pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib {
172 const wpath = try std.unicode.utf8ToUtf16LeWithNull(allocator, path);
173 defer allocator.free(wpath);
176 const wpath = try win_util.sliceToPrefixedFileW(path);
174177
175178 return WindowsDynLib {
176179 .allocator = allocator,
177 .dll = windows.LoadLibraryW(wpath[0..].ptr) orelse return error.FileNotFound
180 .dll = windows.LoadLibraryW(&wpath) orelse {
181 const err = windows.GetLastError();
182 switch (err) {
183 windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
184 windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
185 windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound,
186 else => return os.unexpectedErrorWindows(err),
187 }
188 },
178189 };
179190 }
180191
181192 pub fn close(self: *WindowsDynLib) void {
182 _ = windows.FreeLibrary(self.dll);
193 assert(windows.FreeLibrary(self.dll) != 0);
183194 self.* = undefined;
184195 }
185196
......@@ -187,3 +198,17 @@ pub const WindowsDynLib = struct {
187198 return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr));
188199 }
189200};
201
202test "dynamic_library" {
203 const libname = switch (builtin.os) {
204 Os.linux => "invalid_so.so",
205 Os.windows => "invalid_dll.dll",
206 else => return;,
207 };
208
209 const dynlib = DynLib.open(std.debug.global_allocator, libname) catch |err| {
210 assert(err == error.FileNotFound);
211 return;
212 };
213 @panic("Expected error from function");
214}
std/os/index.zig-2
......@@ -58,8 +58,6 @@ pub const windowsWrite = windows_util.windowsWrite;
5858pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
5959pub const windowsOpen = windows_util.windowsOpen;
6060pub const windowsOpenW = windows_util.windowsOpenW;
61pub const windowsLoadDll = windows_util.windowsLoadDll;
62pub const windowsUnloadDll = windows_util.windowsUnloadDll;
6361pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
6462
6563pub const WindowsCreateIoCompletionPortError = windows_util.WindowsCreateIoCompletionPortError;
std/os/windows/kernel32.zig+17-17
......@@ -4,10 +4,10 @@ pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVE
44
55pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL;
66
7pub extern "kernel32" stdcallcc fn CreateDirectoryW(lpPathName: LPCWSTR, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL;
7pub extern "kernel32" stdcallcc fn CreateDirectoryW(lpPathName: [*]const u16, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL;
88
99pub extern "kernel32" stdcallcc fn CreateFileW(
10 lpFileName: LPCWSTR, // TODO null terminated pointer type
10 lpFileName: [*]const u16, // TODO null terminated pointer type
1111 dwDesiredAccess: DWORD,
1212 dwShareMode: DWORD,
1313 lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES,
......@@ -36,21 +36,21 @@ pub extern "kernel32" stdcallcc fn CreateProcessW(
3636 lpProcessInformation: *PROCESS_INFORMATION,
3737) BOOL;
3838
39pub extern "kernel32" stdcallcc fn CreateSymbolicLinkW(lpSymlinkFileName: LPCWSTR, lpTargetFileName: LPCWSTR, dwFlags: DWORD) BOOLEAN;
39pub extern "kernel32" stdcallcc fn CreateSymbolicLinkW(lpSymlinkFileName: [*]const u16, lpTargetFileName: [*]const u16, dwFlags: DWORD) BOOLEAN;
4040
4141pub extern "kernel32" stdcallcc fn CreateIoCompletionPort(FileHandle: HANDLE, ExistingCompletionPort: ?HANDLE, CompletionKey: ULONG_PTR, NumberOfConcurrentThreads: DWORD) ?HANDLE;
4242
4343pub extern "kernel32" stdcallcc fn CreateThread(lpThreadAttributes: ?LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T, lpStartAddress: LPTHREAD_START_ROUTINE, lpParameter: ?LPVOID, dwCreationFlags: DWORD, lpThreadId: ?LPDWORD) ?HANDLE;
4444
45pub extern "kernel32" stdcallcc fn DeleteFileW(lpFileName: LPCWSTR) BOOL;
45pub extern "kernel32" stdcallcc fn DeleteFileW(lpFileName: [*]const u16) BOOL;
4646
4747pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) noreturn;
4848
49pub extern "kernel32" stdcallcc fn FindFirstFileW(lpFileName: LPCWSTR, lpFindFileData: *WIN32_FIND_DATAW) HANDLE;
49pub extern "kernel32" stdcallcc fn FindFirstFileW(lpFileName: [*]const u16, lpFindFileData: *WIN32_FIND_DATAW) HANDLE;
5050pub extern "kernel32" stdcallcc fn FindClose(hFindFile: HANDLE) BOOL;
5151pub extern "kernel32" stdcallcc fn FindNextFileW(hFindFile: HANDLE, lpFindFileData: *WIN32_FIND_DATAW) BOOL;
5252
53pub extern "kernel32" stdcallcc fn FreeEnvironmentStringsA(penv: LPSTR) BOOL;
53pub extern "kernel32" stdcallcc fn FreeEnvironmentStringsA(penv: [*]u8) BOOL;
5454
5555pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR;
5656
......@@ -63,7 +63,7 @@ pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lp
6363pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
6464pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
6565
66pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?LPSTR;
66pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
6767
6868pub extern "kernel32" stdcallcc fn GetEnvironmentVariableA(lpName: LPCSTR, lpBuffer: LPSTR, nSize: DWORD) DWORD;
6969
......@@ -73,7 +73,7 @@ pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LAR
7373
7474pub extern "kernel32" stdcallcc fn GetFileAttributesW(lpFileName: [*]const WCHAR) DWORD;
7575
76pub extern "kernel32" stdcallcc fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: LPWSTR, nSize: DWORD) DWORD;
76pub extern "kernel32" stdcallcc fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u16, nSize: DWORD) DWORD;
7777
7878pub extern "kernel32" stdcallcc fn GetModuleHandleW(lpModuleName: ?[*]const WCHAR) HMODULE;
7979
......@@ -88,7 +88,7 @@ pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(
8888
8989pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleW(
9090 hFile: HANDLE,
91 lpszFilePath: LPWSTR,
91 lpszFilePath: [*]u16,
9292 cchFilePath: DWORD,
9393 dwFlags: DWORD,
9494) DWORD;
......@@ -117,8 +117,8 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem
117117pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*const c_void) BOOL;
118118
119119pub extern "kernel32" stdcallcc fn MoveFileExW(
120 lpExistingFileName: LPCWSTR,
121 lpNewFileName: LPCWSTR,
120 lpExistingFileName: [*]const u16,
121 lpNewFileName: [*]const u16,
122122 dwFlags: DWORD,
123123) BOOL;
124124
......@@ -141,13 +141,13 @@ pub extern "kernel32" stdcallcc fn ReadDirectoryChangesW(
141141
142142pub extern "kernel32" stdcallcc fn ReadFile(
143143 in_hFile: HANDLE,
144 out_lpBuffer: LPSTR,
144 out_lpBuffer: [*]u8,
145145 in_nNumberOfBytesToRead: DWORD,
146146 out_lpNumberOfBytesRead: ?*DWORD,
147147 in_out_lpOverlapped: ?*OVERLAPPED,
148148) BOOL;
149149
150pub extern "kernel32" stdcallcc fn RemoveDirectoryW(lpPathName: LPCWSTR) BOOL;
150pub extern "kernel32" stdcallcc fn RemoveDirectoryW(lpPathName: [*]const u16) BOOL;
151151
152152pub extern "kernel32" stdcallcc fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) BOOL;
153153
......@@ -168,17 +168,17 @@ pub extern "kernel32" stdcallcc fn WaitForSingleObject(hHandle: HANDLE, dwMillis
168168
169169pub extern "kernel32" stdcallcc fn WriteFile(
170170 in_hFile: HANDLE,
171 in_lpBuffer: LPCSTR,
171 in_lpBuffer: [*]const u8,
172172 in_nNumberOfBytesToWrite: DWORD,
173173 out_lpNumberOfBytesWritten: ?*DWORD,
174174 in_out_lpOverlapped: ?*OVERLAPPED,
175175) BOOL;
176176
177pub extern "kernel32" stdcallcc fn WriteFileEx(hFile: HANDLE, lpBuffer: LPCSTR, nNumberOfBytesToWrite: DWORD, lpOverlapped: LPOVERLAPPED, lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE) BOOL;
177pub extern "kernel32" stdcallcc fn WriteFileEx(hFile: HANDLE, lpBuffer: [*]const u8, nNumberOfBytesToWrite: DWORD, lpOverlapped: LPOVERLAPPED, lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE) BOOL;
178178
179pub extern "kernel32" stdcallcc fn LoadLibraryW(lpLibFileName: LPCWSTR) ?HMODULE;
179pub extern "kernel32" stdcallcc fn LoadLibraryW(lpLibFileName: [*]const u16) ?HMODULE;
180180
181pub extern "kernel32" stdcallcc fn GetProcAddress(hModule: HMODULE, lpProcName: LPCSTR) ?FARPROC;
181pub extern "kernel32" stdcallcc fn GetProcAddress(hModule: HMODULE, lpProcName: [*]const u8) ?FARPROC;
182182
183183pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) BOOL;
184184
std/os/windows/util.zig-31
......@@ -188,37 +188,6 @@ pub fn createWindowsEnvBlock(allocator: *mem.Allocator, env_map: *const BufMap)
188188 return allocator.shrink(u16, result, i);
189189}
190190
191pub fn windowsLoadDllW(dll_path_w: [*]const u16) !windows.HMODULE {
192 return windows.LoadLibraryW(dll_path_w) orelse {
193 const err = windows.GetLastError();
194 switch (err) {
195 windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
196 windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
197 windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound,
198 else => return os.unexpectedErrorWindows(err),
199 }
200 };
201}
202
203pub fn windowsLoadDll(dll_path: []const u8) !windows.HMODULE {
204 const dll_path_w = try sliceToPrefixedFileW(dll_path);
205 return windowsLoadDllW(&dll_path_w);
206}
207
208pub fn windowsUnloadDll(hModule: windows.HMODULE) void {
209 assert(windows.FreeLibrary(hModule) != 0);
210}
211
212test "InvalidDll" {
213 if (builtin.os != builtin.Os.windows) return error.SkipZigTest;
214
215 const handle = os.windowsLoadDll("asdf.dll") catch |err| {
216 assert(err == error.FileNotFound);
217 return;
218 };
219 @panic("Expected error from function");
220}
221
222191pub fn windowsFindFirstFile(
223192 dir_path: []const u8,
224193 find_file_data: *windows.WIN32_FIND_DATAW,