| 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 := uint16(len(s16) * 2) |
| 26 |
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdmsec/nf-wdmsec-wdmlibrtlinitunicodestringex |
| 27 |
return &NTUnicodeString{ |
| 28 |
Length: n - 2, // subtract 2 bytes for the NUL terminator |
| 29 |
MaximumLength: n, |
| 30 |
Buffer: &s16[0], |
| 31 |
}, nil |
| 32 |
} |