Text file src/cmd/go/testdata/script/ws2_32.txt

     1  [!GOOS:windows] skip
     2  
     3  go run .
     4  stdout 'ws2_32.dll: not found'
     5  
     6  go run -tags net .
     7  stdout 'ws2_32.dll: found'
     8  
     9  -- go.mod --
    10  module m
    11  
    12  go 1.21
    13  
    14  -- utils.go --
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"syscall"
    20  	"unsafe"
    21  )
    22  
    23  func hasModuleHandle() {
    24  	const ws2_32 = "ws2_32.dll"
    25  	getModuleHandle := syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetModuleHandleW")
    26  	mod, _, _ := getModuleHandle.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(ws2_32))))
    27  	if mod != 0 {
    28  		fmt.Println(ws2_32+":", "found")
    29  	} else {
    30  		fmt.Println(ws2_32+":", "not found")
    31  	}
    32  }
    33  -- net.go --
    34  //go:build net
    35  package main
    36  
    37  import _ "net"
    38  
    39  func main() {
    40      hasModuleHandle()
    41  }
    42  -- nonet.go --
    43  //go:build !net
    44  package main
    45  
    46  func main() {
    47      hasModuleHandle()
    48  }

View as plain text