mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-04 08:07:19 -04:00
windows: add support for NTSTATUS values
The native NT API returns error values from a different namespace as the usual Win32 one. This means it needs to be typed differently. This commit adds broad support for using NTSTATUS values in a new type called NTStatus. First we add the type as a basic uint32. Then we add all of the predefined constants from ntstatus.h, by augmenting mkerrors.bash to do automatic extraction. There's a convenece way to convert an NT error to a Win32 error, so we add the NTStatus.Errno() function. Since NTStatus is an error type, we define an Error() function that returns a string by asking ntdll.dll for its contents, in the exact same way that syscall.Errno.Error() does, by calling FormatMessage. Since functions need to actually use this, we add the rule that if a `//sys` declaration returns an error value called "ntstatus", then the type underlying the error interface is an NTStatus instead of an Errno. Finally we fix one function that was returning an error interface of an Errno rather than an NTStatus. Change-Id: I06296b9563bbec526759d12a19f13ac6ad46dcc3 Reviewed-on: https://go-review.googlesource.com/c/sys/+/297330 Trust: Jason A. Donenfeld <Jason@zx2c4.com> Trust: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
@@ -8,6 +8,7 @@ package windows
|
||||
|
||||
import (
|
||||
errorspkg "errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -65,9 +66,8 @@ const (
|
||||
LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
|
||||
LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
|
||||
|
||||
// Return values of SleepEx and other APC functions
|
||||
STATUS_USER_APC = 0x000000C0
|
||||
WAIT_IO_COMPLETION = STATUS_USER_APC
|
||||
// Return value of SleepEx and other APC functions
|
||||
WAIT_IO_COMPLETION = 0x000000C0
|
||||
)
|
||||
|
||||
// StringToUTF16 is deprecated. Use UTF16FromString instead.
|
||||
@@ -375,7 +375,8 @@ func NewCallbackCDecl(fn interface{}) uintptr {
|
||||
//sys stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int32) (chars int32) = ole32.StringFromGUID2
|
||||
//sys coCreateGuid(pguid *GUID) (ret error) = ole32.CoCreateGuid
|
||||
//sys CoTaskMemFree(address unsafe.Pointer) = ole32.CoTaskMemFree
|
||||
//sys rtlGetVersion(info *OsVersionInfoEx) (ret error) = ntdll.RtlGetVersion
|
||||
//sys rtlNtStatusToDosError(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosError
|
||||
//sys rtlGetVersion(info *OsVersionInfoEx) (ntstatus error) = ntdll.RtlGetVersion
|
||||
//sys rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers
|
||||
//sys getProcessPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetProcessPreferredUILanguages
|
||||
//sys getThreadPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetThreadPreferredUILanguages
|
||||
@@ -1514,3 +1515,21 @@ func getUILanguages(flags uint32, f func(flags uint32, numLanguages *uint32, buf
|
||||
func SetConsoleCursorPosition(console Handle, position Coord) error {
|
||||
return setConsoleCursorPosition(console, *((*uint32)(unsafe.Pointer(&position))))
|
||||
}
|
||||
|
||||
func (s NTStatus) Errno() syscall.Errno {
|
||||
return rtlNtStatusToDosError(s)
|
||||
}
|
||||
|
||||
func langID(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) }
|
||||
|
||||
func (s NTStatus) Error() string {
|
||||
b := make([]uint16, 300)
|
||||
n, err := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_ARGUMENT_ARRAY, modntdll.Handle(), uint32(s), langID(LANG_ENGLISH, SUBLANG_ENGLISH_US), b, nil)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("NTSTATUS 0x%08x", uint32(s))
|
||||
}
|
||||
// trim terminating \r and \n
|
||||
for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
|
||||
}
|
||||
return string(utf16.Decode(b[:n]))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user