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 @@...@@ -1,11 +1,15 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const Os = builtin.Os;2const Os = builtin.Os;
3const std = @import("std");3
4const std = @import("index.zig");
4const mem = std.mem;5const mem = std.mem;
5const elf = std.elf;
6const cstr = std.cstr;6const cstr = std.cstr;
7const linux = std.os.linux;7const os = std.os;
8const windows = std.os.windows;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
10pub const DynLib = switch (builtin.os) {14pub const DynLib = switch (builtin.os) {
11 Os.linux => LinuxDynLib,15 Os.linux => LinuxDynLib,
...@@ -169,17 +173,24 @@ pub const WindowsDynLib = struct {...@@ -169,17 +173,24 @@ pub const WindowsDynLib = struct {
169 dll: windows.HMODULE,173 dll: windows.HMODULE,
170174
171 pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib {175 pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib {
172 const wpath = try std.unicode.utf8ToUtf16LeWithNull(allocator, path);176 const wpath = try win_util.sliceToPrefixedFileW(path);
173 defer allocator.free(wpath);
174 177
175 return WindowsDynLib {178 return WindowsDynLib {
176 .allocator = allocator,179 .allocator = allocator,
177 .dll = windows.LoadLibraryW(wpath[0..].ptr) orelse return error.FileNotFound180 .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 },
178 };189 };
179 }190 }
180191
181 pub fn close(self: *WindowsDynLib) void {192 pub fn close(self: *WindowsDynLib) void {
182 _ = windows.FreeLibrary(self.dll);193 assert(windows.FreeLibrary(self.dll) != 0);
183 self.* = undefined;194 self.* = undefined;
184 }195 }
185196
...@@ -187,3 +198,17 @@ pub const WindowsDynLib = struct {...@@ -187,3 +198,17 @@ pub const WindowsDynLib = struct {
187 return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr));198 return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr));
188 }199 }
189};200};
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;...@@ -58,8 +58,6 @@ pub const windowsWrite = windows_util.windowsWrite;
58pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;58pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
59pub const windowsOpen = windows_util.windowsOpen;59pub const windowsOpen = windows_util.windowsOpen;
60pub const windowsOpenW = windows_util.windowsOpenW;60pub const windowsOpenW = windows_util.windowsOpenW;
61pub const windowsLoadDll = windows_util.windowsLoadDll;
62pub const windowsUnloadDll = windows_util.windowsUnloadDll;
63pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;61pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
6462
65pub const WindowsCreateIoCompletionPortError = windows_util.WindowsCreateIoCompletionPortError;63pub 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...@@ -4,10 +4,10 @@ pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVE
44
5pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL;5pub 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
9pub extern "kernel32" stdcallcc fn CreateFileW(9pub extern "kernel32" stdcallcc fn CreateFileW(
10 lpFileName: LPCWSTR, // TODO null terminated pointer type10 lpFileName: [*]const u16, // TODO null terminated pointer type
11 dwDesiredAccess: DWORD,11 dwDesiredAccess: DWORD,
12 dwShareMode: DWORD,12 dwShareMode: DWORD,
13 lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES,13 lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES,
...@@ -36,21 +36,21 @@ pub extern "kernel32" stdcallcc fn CreateProcessW(...@@ -36,21 +36,21 @@ pub extern "kernel32" stdcallcc fn CreateProcessW(
36 lpProcessInformation: *PROCESS_INFORMATION,36 lpProcessInformation: *PROCESS_INFORMATION,
37) BOOL;37) 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
41pub extern "kernel32" stdcallcc fn CreateIoCompletionPort(FileHandle: HANDLE, ExistingCompletionPort: ?HANDLE, CompletionKey: ULONG_PTR, NumberOfConcurrentThreads: DWORD) ?HANDLE;41pub extern "kernel32" stdcallcc fn CreateIoCompletionPort(FileHandle: HANDLE, ExistingCompletionPort: ?HANDLE, CompletionKey: ULONG_PTR, NumberOfConcurrentThreads: DWORD) ?HANDLE;
4242
43pub extern "kernel32" stdcallcc fn CreateThread(lpThreadAttributes: ?LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T, lpStartAddress: LPTHREAD_START_ROUTINE, lpParameter: ?LPVOID, dwCreationFlags: DWORD, lpThreadId: ?LPDWORD) ?HANDLE;43pub 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
47pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) noreturn;47pub 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;
50pub extern "kernel32" stdcallcc fn FindClose(hFindFile: HANDLE) BOOL;50pub extern "kernel32" stdcallcc fn FindClose(hFindFile: HANDLE) BOOL;
51pub extern "kernel32" stdcallcc fn FindNextFileW(hFindFile: HANDLE, lpFindFileData: *WIN32_FIND_DATAW) BOOL;51pub 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
55pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR;55pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR;
5656
...@@ -63,7 +63,7 @@ pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lp...@@ -63,7 +63,7 @@ pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lp
63pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;63pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
64pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;64pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
6565
66pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?LPSTR;66pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
6767
68pub extern "kernel32" stdcallcc fn GetEnvironmentVariableA(lpName: LPCSTR, lpBuffer: LPSTR, nSize: DWORD) DWORD;68pub 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...@@ -73,7 +73,7 @@ pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LAR
7373
74pub extern "kernel32" stdcallcc fn GetFileAttributesW(lpFileName: [*]const WCHAR) DWORD;74pub 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
78pub extern "kernel32" stdcallcc fn GetModuleHandleW(lpModuleName: ?[*]const WCHAR) HMODULE;78pub extern "kernel32" stdcallcc fn GetModuleHandleW(lpModuleName: ?[*]const WCHAR) HMODULE;
7979
...@@ -88,7 +88,7 @@ pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(...@@ -88,7 +88,7 @@ pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(
8888
89pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleW(89pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleW(
90 hFile: HANDLE,90 hFile: HANDLE,
91 lpszFilePath: LPWSTR,91 lpszFilePath: [*]u16,
92 cchFilePath: DWORD,92 cchFilePath: DWORD,
93 dwFlags: DWORD,93 dwFlags: DWORD,
94) DWORD;94) DWORD;
...@@ -117,8 +117,8 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem...@@ -117,8 +117,8 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem
117pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*const c_void) BOOL;117pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*const c_void) BOOL;
118118
119pub extern "kernel32" stdcallcc fn MoveFileExW(119pub extern "kernel32" stdcallcc fn MoveFileExW(
120 lpExistingFileName: LPCWSTR,120 lpExistingFileName: [*]const u16,
121 lpNewFileName: LPCWSTR,121 lpNewFileName: [*]const u16,
122 dwFlags: DWORD,122 dwFlags: DWORD,
123) BOOL;123) BOOL;
124124
...@@ -141,13 +141,13 @@ pub extern "kernel32" stdcallcc fn ReadDirectoryChangesW(...@@ -141,13 +141,13 @@ pub extern "kernel32" stdcallcc fn ReadDirectoryChangesW(
141141
142pub extern "kernel32" stdcallcc fn ReadFile(142pub extern "kernel32" stdcallcc fn ReadFile(
143 in_hFile: HANDLE,143 in_hFile: HANDLE,
144 out_lpBuffer: LPSTR,144 out_lpBuffer: [*]u8,
145 in_nNumberOfBytesToRead: DWORD,145 in_nNumberOfBytesToRead: DWORD,
146 out_lpNumberOfBytesRead: ?*DWORD,146 out_lpNumberOfBytesRead: ?*DWORD,
147 in_out_lpOverlapped: ?*OVERLAPPED,147 in_out_lpOverlapped: ?*OVERLAPPED,
148) BOOL;148) BOOL;
149149
150pub extern "kernel32" stdcallcc fn RemoveDirectoryW(lpPathName: LPCWSTR) BOOL;150pub extern "kernel32" stdcallcc fn RemoveDirectoryW(lpPathName: [*]const u16) BOOL;
151151
152pub extern "kernel32" stdcallcc fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) BOOL;152pub extern "kernel32" stdcallcc fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) BOOL;
153153
...@@ -168,17 +168,17 @@ pub extern "kernel32" stdcallcc fn WaitForSingleObject(hHandle: HANDLE, dwMillis...@@ -168,17 +168,17 @@ pub extern "kernel32" stdcallcc fn WaitForSingleObject(hHandle: HANDLE, dwMillis
168168
169pub extern "kernel32" stdcallcc fn WriteFile(169pub extern "kernel32" stdcallcc fn WriteFile(
170 in_hFile: HANDLE,170 in_hFile: HANDLE,
171 in_lpBuffer: LPCSTR,171 in_lpBuffer: [*]const u8,
172 in_nNumberOfBytesToWrite: DWORD,172 in_nNumberOfBytesToWrite: DWORD,
173 out_lpNumberOfBytesWritten: ?*DWORD,173 out_lpNumberOfBytesWritten: ?*DWORD,
174 in_out_lpOverlapped: ?*OVERLAPPED,174 in_out_lpOverlapped: ?*OVERLAPPED,
175) BOOL;175) 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
183pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) BOOL;183pub 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)...@@ -188,37 +188,6 @@ pub fn createWindowsEnvBlock(allocator: *mem.Allocator, env_map: *const BufMap)
188 return allocator.shrink(u16, result, i);188 return allocator.shrink(u16, result, i);
189}189}
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
222pub fn windowsFindFirstFile(191pub fn windowsFindFirstFile(
223 dir_path: []const u8,192 dir_path: []const u8,
224 find_file_data: *windows.WIN32_FIND_DATAW,193 find_file_data: *windows.WIN32_FIND_DATAW,