--- /dev/null
+[!GOOS:windows] skip
+
+go run .
+stdout 'ws2_32.dll: not found'
+
+go run -tags net .
+stdout 'ws2_32.dll: found'
+
+-- go.mod --
+module m
+
+go 1.21
+
+-- utils.go --
+package main
+
+import (
+ "fmt"
+ "syscall"
+ "unsafe"
+)
+
+func hasModuleHandle() {
+ const ws2_32 = "ws2_32.dll"
+ getModuleHandle := syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetModuleHandleW")
+ mod, _, _ := getModuleHandle.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(ws2_32))))
+ if mod != 0 {
+ fmt.Println(ws2_32+":", "found")
+ } else {
+ fmt.Println(ws2_32+":", "not found")
+ }
+}
+-- net.go --
+//go:build net
+package main
+
+import _ "net"
+
+func main() {
+ hasModuleHandle()
+}
+-- nonet.go --
+//go:build !net
+package main
+
+func main() {
+ hasModuleHandle()
+}
\ No newline at end of file
useSetFileCompletionNotificationModes = true
}
-func init() {
+// InitWSA initiates the use of the Winsock DLL by the current process.
+// It is called from the net package at init time to avoid
+// loading ws2_32.dll when net is not used.
+var InitWSA = sync.OnceFunc(func() {
var d syscall.WSAData
e := syscall.WSAStartup(uint32(0x202), &d)
if e != nil {
initErr = e
}
checkSetFileCompletionNotificationModes()
-}
+})
// operation contains superset of data necessary to perform all async IO.
type operation struct {
func init() {
loggedFDs = make(map[syscall.Handle]*loggedFD)
*poll.LogInitFD = logFD
+
+ poll.InitWSA()
}
func findLoggedFD(h syscall.Handle) (lfd *loggedFD, found bool) {
writeMsgSyscallName = "wsasendmsg"
)
+func init() {
+ poll.InitWSA()
+}
+
// canUseConnectEx reports whether we can use the ConnectEx Windows API call
// for the given network type.
func canUseConnectEx(net string) bool {