Skip to content

Commit 11eadc0

Browse files
rolandshoemakergopherbot
authored andcommitted
windows: add AddDllDirectory and RemoveDllDirectory
Per https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-adddlldirectory and https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-removedlldirectory. Change-Id: If44a3758720345d1bbd9af96ec2481fbe9398a08 Reviewed-on: https://go-review.googlesource.com/c/sys/+/537755 Reviewed-by: Tatiana Bradley <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]> Reviewed-by: Alex Brainman <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent e4099bf commit 11eadc0

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

windows/syscall_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ func NewCallbackCDecl(fn interface{}) uintptr {
155155
//sys GetModuleFileName(module Handle, filename *uint16, size uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
156156
//sys GetModuleHandleEx(flags uint32, moduleName *uint16, module *Handle) (err error) = kernel32.GetModuleHandleExW
157157
//sys SetDefaultDllDirectories(directoryFlags uint32) (err error)
158+
//sys AddDllDirectory(path *uint16) (cookie uintptr, err error) = kernel32.AddDllDirectory
159+
//sys RemoveDllDirectory(cookie uintptr) (err error) = kernel32.RemoveDllDirectory
158160
//sys SetDllDirectory(path string) (err error) = kernel32.SetDllDirectoryW
159161
//sys GetVersion() (ver uint32, err error)
160162
//sys FormatMessage(flags uint32, msgsrc uintptr, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) = FormatMessageW

windows/syscall_windows_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"os"
14+
"os/exec"
1415
"path/filepath"
1516
"runtime"
1617
"strconv"
@@ -1222,3 +1223,55 @@ func TestGetStartupInfo(t *testing.T) {
12221223
t.Fatalf("GetStartupInfo: got error %v, want nil", err)
12231224
}
12241225
}
1226+
1227+
func TestAddRemoveDllDirectory(t *testing.T) {
1228+
if _, err := exec.LookPath("gcc"); err != nil {
1229+
t.Skip("skipping test: gcc is missing")
1230+
}
1231+
dllSrc := `#include <stdint.h>
1232+
#include <windows.h>
1233+
1234+
uintptr_t beep(void) {
1235+
return 5;
1236+
}`
1237+
tmpdir := t.TempDir()
1238+
srcname := "beep.c"
1239+
err := os.WriteFile(filepath.Join(tmpdir, srcname), []byte(dllSrc), 0)
1240+
if err != nil {
1241+
t.Fatal(err)
1242+
}
1243+
name := "beep.dll"
1244+
cmd := exec.Command("gcc", "-shared", "-s", "-Werror", "-o", name, srcname)
1245+
cmd.Dir = tmpdir
1246+
out, err := cmd.CombinedOutput()
1247+
if err != nil {
1248+
t.Fatalf("failed to build dll: %v - %v", err, string(out))
1249+
}
1250+
1251+
if _, err := windows.LoadLibraryEx("beep.dll", 0, windows.LOAD_LIBRARY_SEARCH_USER_DIRS); err == nil {
1252+
t.Fatal("LoadLibraryEx unexpectedly found beep.dll")
1253+
}
1254+
1255+
dllCookie, err := windows.AddDllDirectory(windows.StringToUTF16Ptr(tmpdir))
1256+
if err != nil {
1257+
t.Fatalf("AddDllDirectory failed: %s", err)
1258+
}
1259+
1260+
handle, err := windows.LoadLibraryEx("beep.dll", 0, windows.LOAD_LIBRARY_SEARCH_USER_DIRS)
1261+
if err != nil {
1262+
t.Fatalf("LoadLibraryEx failed: %s", err)
1263+
}
1264+
1265+
if err := windows.FreeLibrary(handle); err != nil {
1266+
t.Fatalf("FreeLibrary failed: %s", err)
1267+
}
1268+
1269+
if err := windows.RemoveDllDirectory(dllCookie); err != nil {
1270+
t.Fatalf("RemoveDllDirectory failed: %s", err)
1271+
}
1272+
1273+
_, err = windows.LoadLibraryEx("beep.dll", 0, windows.LOAD_LIBRARY_SEARCH_USER_DIRS)
1274+
if err == nil {
1275+
t.Fatal("LoadLibraryEx unexpectedly found beep.dll")
1276+
}
1277+
}

windows/zsyscall_windows.go

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)