Skip to content

Commit

Permalink
Merge pull request #42 from ziglibs/auguste/shell32-ole32-removed
Browse files Browse the repository at this point in the history
shell32 and ole32 removed from std
  • Loading branch information
SuperAuguste authored Nov 26, 2023
2 parents 8554730 + fa6271b commit 8bb4bf7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions known-folders.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,29 @@ pub fn getPath(allocator: std.mem.Allocator, folder: KnownFolder) Error!?[]const

switch (builtin.os.tag) {
.windows => {
const funcs = struct {
extern "shell32" fn SHGetKnownFolderPath(
rfid: *const std.os.windows.GUID,
dwFlags: std.os.windows.DWORD,
hToken: ?std.os.windows.HANDLE,
ppszPathL: *std.os.windows.PWSTR,
) callconv(std.os.windows.WINAPI) std.os.windows.HRESULT;
extern "ole32" fn CoTaskMemFree(pv: std.os.windows.LPVOID) callconv(std.os.windows.WINAPI) void;
};

const folder_spec = windows_folder_spec.get(folder);

switch (folder_spec) {
.by_guid => |guid| {
var dir_path_ptr: [*:0]u16 = undefined;
switch (std.os.windows.shell32.SHGetKnownFolderPath(
switch (funcs.SHGetKnownFolderPath(
&guid,
std.os.windows.KF_FLAG_CREATE, // TODO: Chose sane option here?
null,
&dir_path_ptr,
)) {
std.os.windows.S_OK => {
defer std.os.windows.ole32.CoTaskMemFree(@ptrCast(dir_path_ptr));
defer funcs.CoTaskMemFree(@ptrCast(dir_path_ptr));
const global_dir = std.unicode.utf16leToUtf8Alloc(allocator, std.mem.span(dir_path_ptr)) catch |err| switch (err) {
error.UnexpectedSecondSurrogateHalf => return null,
error.ExpectedSecondSurrogateHalf => return null,
Expand Down

0 comments on commit 8bb4bf7

Please sign in to comment.