Source file src/runtime/cgo/library_windows_c.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build goexperiment.cgobindings && windows && (msan || tsan)
     6  
     7  package cgo
     8  
     9  /*
    10  #define WIN32_LEAN_AND_MEAN
    11  #include <stdint.h>
    12  #include <windows.h>
    13  
    14  static uintptr_t
    15  cgo_load_library(const uint16_t *path, uint32_t *errp) {
    16  	HMODULE h;
    17  
    18  	*errp = 0;
    19  	h = LoadLibraryW((LPCWSTR)path);
    20  	if (h == NULL) {
    21  		*errp = (uint32_t)GetLastError();
    22  	}
    23  	return (uintptr_t)h;
    24  }
    25  
    26  static uintptr_t
    27  cgo_get_proc_address(uintptr_t h, const char *name, uint32_t *errp) {
    28  	FARPROC p;
    29  
    30  	*errp = 0;
    31  	p = GetProcAddress((HMODULE)h, name);
    32  	if (p == NULL) {
    33  		*errp = (uint32_t)GetLastError();
    34  	}
    35  	return (uintptr_t)p;
    36  }
    37  */
    38  import "C"
    39  
    40  import (
    41  	"errors"
    42  	"internal/strconv"
    43  	"internal/stringslite"
    44  	"unsafe"
    45  )
    46  
    47  func dlopen(path string) (uintptr, error) {
    48  	if stringslite.IndexByte(path, 0) >= 0 {
    49  		return 0, errors.New("cgo: dlopen: path contains NUL byte")
    50  	}
    51  	wpath := utf16FromString(path)
    52  
    53  	var errno C.uint32_t
    54  	ret := uintptr(C.cgo_load_library((*C.uint16_t)(unsafe.Pointer(&wpath[0])), &errno))
    55  	if ret == 0 {
    56  		return 0, errors.New("cgo: dlopen: " + path + ": error " + strconv.FormatUint(uint64(errno), 10))
    57  	}
    58  	return ret, nil
    59  }
    60  
    61  func dlsym(h uintptr, name string) (unsafe.Pointer, string) {
    62  	if stringslite.IndexByte(name, 0) >= 0 {
    63  		return nil, "symbol name contains NUL byte"
    64  	}
    65  	cname := append([]byte(name), 0)
    66  
    67  	var errno C.uint32_t
    68  	ret := uintptr(C.cgo_get_proc_address(C.uintptr_t(h), (*C.char)(unsafe.Pointer(&cname[0])), &errno))
    69  	if ret == 0 {
    70  		return nil, "error " + strconv.FormatUint(uint64(errno), 10)
    71  	}
    72  	return *(*unsafe.Pointer)(unsafe.Pointer(&ret)), ""
    73  }
    74  
    75  func utf16FromString(s string) []uint16 {
    76  	const (
    77  		replacementChar = '\uFFFD'
    78  		maxRune         = '\U0010FFFF'
    79  		surr1           = 0xd800
    80  		surr2           = 0xdc00
    81  		surr3           = 0xe000
    82  		surrSelf        = 0x10000
    83  	)
    84  	buf := make([]uint16, 0, len(s)+1)
    85  	for _, r := range s {
    86  		switch {
    87  		case 0 <= r && r < surr1, surr3 <= r && r < surrSelf:
    88  			buf = append(buf, uint16(r))
    89  		case surrSelf <= r && r <= maxRune:
    90  			r -= surrSelf
    91  			buf = append(buf, uint16(surr1+(r>>10)&0x3ff), uint16(surr2+r&0x3ff))
    92  		default:
    93  			buf = append(buf, replacementChar)
    94  		}
    95  	}
    96  	return append(buf, 0)
    97  }
    98  

View as plain text