...

Source file src/internal/syscall/windows/string_windows.go

Documentation: internal/syscall/windows

     1  // Copyright 2024 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  package windows
     6  
     7  import "syscall"
     8  
     9  // NTUnicodeString is a UTF-16 string for NT native APIs, corresponding to UNICODE_STRING.
    10  type NTUnicodeString struct {
    11  	Length        uint16
    12  	MaximumLength uint16
    13  	Buffer        *uint16
    14  }
    15  
    16  // NewNTUnicodeString returns a new NTUnicodeString structure for use with native
    17  // NT APIs that work over the NTUnicodeString type. Note that most Windows APIs
    18  // do not use NTUnicodeString, and instead UTF16PtrFromString should be used for
    19  // the more common *uint16 string type.
    20  func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
    21  	s16, err := syscall.UTF16FromString(s)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	n := len(s16) * 2
    26  	if n > (1<<16)-1 {
    27  		return nil, syscall.EINVAL
    28  	}
    29  	// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdmsec/nf-wdmsec-wdmlibrtlinitunicodestringex
    30  	return &NTUnicodeString{
    31  		Length:        uint16(n) - 2, // subtract 2 bytes for the NUL terminator
    32  		MaximumLength: uint16(n),
    33  		Buffer:        &s16[0],
    34  	}, nil
    35  }
    36  

View as plain text