merged windows dll apis

This commit is contained in:
emekoi 2018-09-27 16:51:22 -05:00
parent 42ba206c5d
commit 623f5085f1
4 changed files with 50 additions and 58 deletions

View File

@ -1,11 +1,15 @@
const builtin = @import("builtin");
const Os = builtin.Os;
const std = @import("std");
const std = @import("index.zig");
const mem = std.mem;
const elf = std.elf;
const cstr = std.cstr;
const linux = std.os.linux;
const windows = std.os.windows;
const os = std.os;
const assert = std.debug.assert;
const elf = std.elf;
const linux = os.linux;
const windows = os.windows;
const win_util = @import("os/windows/util.zig");
pub const DynLib = switch (builtin.os) {
Os.linux => LinuxDynLib,
@ -169,17 +173,24 @@ pub const WindowsDynLib = struct {
dll: windows.HMODULE,
pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib {
const wpath = try std.unicode.utf8ToUtf16LeWithNull(allocator, path);
defer allocator.free(wpath);
const wpath = try win_util.sliceToPrefixedFileW(path);
return WindowsDynLib {
.allocator = allocator,
.dll = windows.LoadLibraryW(wpath[0..].ptr) orelse return error.FileNotFound
.dll = windows.LoadLibraryW(&wpath) orelse {
const err = windows.GetLastError();
switch (err) {
windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound,
else => return os.unexpectedErrorWindows(err),
}
},
};
}
pub fn close(self: *WindowsDynLib) void {
_ = windows.FreeLibrary(self.dll);
assert(windows.FreeLibrary(self.dll) != 0);
self.* = undefined;
}
@ -187,3 +198,17 @@ pub const WindowsDynLib = struct {
return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr));
}
};
test "dynamic_library" {
const libname = switch (builtin.os) {
Os.linux => "invalid_so.so",
Os.windows => "invalid_dll.dll",
else => return;,
};
const dynlib = DynLib.open(std.debug.global_allocator, libname) catch |err| {
assert(err == error.FileNotFound);
return;
};
@panic("Expected error from function");
}

View File

@ -58,8 +58,6 @@ pub const windowsWrite = windows_util.windowsWrite;
pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
pub const windowsOpen = windows_util.windowsOpen;
pub const windowsOpenW = windows_util.windowsOpenW;
pub const windowsLoadDll = windows_util.windowsLoadDll;
pub const windowsUnloadDll = windows_util.windowsUnloadDll;
pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
pub const WindowsCreateIoCompletionPortError = windows_util.WindowsCreateIoCompletionPortError;

View File

@ -4,10 +4,10 @@ pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVE
pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL;
pub extern "kernel32" stdcallcc fn CreateDirectoryW(lpPathName: LPCWSTR, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL;
pub extern "kernel32" stdcallcc fn CreateDirectoryW(lpPathName: [*]const u16, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL;
pub extern "kernel32" stdcallcc fn CreateFileW(
lpFileName: LPCWSTR, // TODO null terminated pointer type
lpFileName: [*]const u16, // TODO null terminated pointer type
dwDesiredAccess: DWORD,
dwShareMode: DWORD,
lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES,
@ -36,21 +36,21 @@ pub extern "kernel32" stdcallcc fn CreateProcessW(
lpProcessInformation: *PROCESS_INFORMATION,
) BOOL;
pub extern "kernel32" stdcallcc fn CreateSymbolicLinkW(lpSymlinkFileName: LPCWSTR, lpTargetFileName: LPCWSTR, dwFlags: DWORD) BOOLEAN;
pub extern "kernel32" stdcallcc fn CreateSymbolicLinkW(lpSymlinkFileName: [*]const u16, lpTargetFileName: [*]const u16, dwFlags: DWORD) BOOLEAN;
pub extern "kernel32" stdcallcc fn CreateIoCompletionPort(FileHandle: HANDLE, ExistingCompletionPort: ?HANDLE, CompletionKey: ULONG_PTR, NumberOfConcurrentThreads: DWORD) ?HANDLE;
pub extern "kernel32" stdcallcc fn CreateThread(lpThreadAttributes: ?LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T, lpStartAddress: LPTHREAD_START_ROUTINE, lpParameter: ?LPVOID, dwCreationFlags: DWORD, lpThreadId: ?LPDWORD) ?HANDLE;
pub extern "kernel32" stdcallcc fn DeleteFileW(lpFileName: LPCWSTR) BOOL;
pub extern "kernel32" stdcallcc fn DeleteFileW(lpFileName: [*]const u16) BOOL;
pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) noreturn;
pub extern "kernel32" stdcallcc fn FindFirstFileW(lpFileName: LPCWSTR, lpFindFileData: *WIN32_FIND_DATAW) HANDLE;
pub extern "kernel32" stdcallcc fn FindFirstFileW(lpFileName: [*]const u16, lpFindFileData: *WIN32_FIND_DATAW) HANDLE;
pub extern "kernel32" stdcallcc fn FindClose(hFindFile: HANDLE) BOOL;
pub extern "kernel32" stdcallcc fn FindNextFileW(hFindFile: HANDLE, lpFindFileData: *WIN32_FIND_DATAW) BOOL;
pub extern "kernel32" stdcallcc fn FreeEnvironmentStringsA(penv: LPSTR) BOOL;
pub extern "kernel32" stdcallcc fn FreeEnvironmentStringsA(penv: [*]u8) BOOL;
pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR;
@ -63,7 +63,7 @@ pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lp
pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?LPSTR;
pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
pub extern "kernel32" stdcallcc fn GetEnvironmentVariableA(lpName: LPCSTR, lpBuffer: LPSTR, nSize: DWORD) DWORD;
@ -73,7 +73,7 @@ pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LAR
pub extern "kernel32" stdcallcc fn GetFileAttributesW(lpFileName: [*]const WCHAR) DWORD;
pub extern "kernel32" stdcallcc fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: LPWSTR, nSize: DWORD) DWORD;
pub extern "kernel32" stdcallcc fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u16, nSize: DWORD) DWORD;
pub extern "kernel32" stdcallcc fn GetModuleHandleW(lpModuleName: ?[*]const WCHAR) HMODULE;
@ -88,7 +88,7 @@ pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(
pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleW(
hFile: HANDLE,
lpszFilePath: LPWSTR,
lpszFilePath: [*]u16,
cchFilePath: DWORD,
dwFlags: DWORD,
) DWORD;
@ -117,8 +117,8 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem
pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*const c_void) BOOL;
pub extern "kernel32" stdcallcc fn MoveFileExW(
lpExistingFileName: LPCWSTR,
lpNewFileName: LPCWSTR,
lpExistingFileName: [*]const u16,
lpNewFileName: [*]const u16,
dwFlags: DWORD,
) BOOL;
@ -141,13 +141,13 @@ pub extern "kernel32" stdcallcc fn ReadDirectoryChangesW(
pub extern "kernel32" stdcallcc fn ReadFile(
in_hFile: HANDLE,
out_lpBuffer: LPSTR,
out_lpBuffer: [*]u8,
in_nNumberOfBytesToRead: DWORD,
out_lpNumberOfBytesRead: ?*DWORD,
in_out_lpOverlapped: ?*OVERLAPPED,
) BOOL;
pub extern "kernel32" stdcallcc fn RemoveDirectoryW(lpPathName: LPCWSTR) BOOL;
pub extern "kernel32" stdcallcc fn RemoveDirectoryW(lpPathName: [*]const u16) BOOL;
pub extern "kernel32" stdcallcc fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) BOOL;
@ -168,17 +168,17 @@ pub extern "kernel32" stdcallcc fn WaitForSingleObject(hHandle: HANDLE, dwMillis
pub extern "kernel32" stdcallcc fn WriteFile(
in_hFile: HANDLE,
in_lpBuffer: LPCSTR,
in_lpBuffer: [*]const u8,
in_nNumberOfBytesToWrite: DWORD,
out_lpNumberOfBytesWritten: ?*DWORD,
in_out_lpOverlapped: ?*OVERLAPPED,
) BOOL;
pub extern "kernel32" stdcallcc fn WriteFileEx(hFile: HANDLE, lpBuffer: LPCSTR, nNumberOfBytesToWrite: DWORD, lpOverlapped: LPOVERLAPPED, lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE) BOOL;
pub extern "kernel32" stdcallcc fn WriteFileEx(hFile: HANDLE, lpBuffer: [*]const u8, nNumberOfBytesToWrite: DWORD, lpOverlapped: LPOVERLAPPED, lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE) BOOL;
pub extern "kernel32" stdcallcc fn LoadLibraryW(lpLibFileName: LPCWSTR) ?HMODULE;
pub extern "kernel32" stdcallcc fn LoadLibraryW(lpLibFileName: [*]const u16) ?HMODULE;
pub extern "kernel32" stdcallcc fn GetProcAddress(hModule: HMODULE, lpProcName: LPCSTR) ?FARPROC;
pub extern "kernel32" stdcallcc fn GetProcAddress(hModule: HMODULE, lpProcName: [*]const u8) ?FARPROC;
pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) BOOL;

View File

@ -188,37 +188,6 @@ pub fn createWindowsEnvBlock(allocator: *mem.Allocator, env_map: *const BufMap)
return allocator.shrink(u16, result, i);
}
pub fn windowsLoadDllW(dll_path_w: [*]const u16) !windows.HMODULE {
return windows.LoadLibraryW(dll_path_w) orelse {
const err = windows.GetLastError();
switch (err) {
windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound,
else => return os.unexpectedErrorWindows(err),
}
};
}
pub fn windowsLoadDll(dll_path: []const u8) !windows.HMODULE {
const dll_path_w = try sliceToPrefixedFileW(dll_path);
return windowsLoadDllW(&dll_path_w);
}
pub fn windowsUnloadDll(hModule: windows.HMODULE) void {
assert(windows.FreeLibrary(hModule) != 0);
}
test "InvalidDll" {
if (builtin.os != builtin.Os.windows) return error.SkipZigTest;
const handle = os.windowsLoadDll("asdf.dll") catch |err| {
assert(err == error.FileNotFound);
return;
};
@panic("Expected error from function");
}
pub fn windowsFindFirstFile(
dir_path: []const u8,
find_file_data: *windows.WIN32_FIND_DATAW,