Skip to content

Commit d1fd9fc

Browse files
committed
refactor: deprecate old methods and point to upstream lib
deprecate methods implemented in the upstream x/sys/windows library
1 parent 655280b commit d1fd9fc

File tree

8 files changed

+49
-163
lines changed

8 files changed

+49
-163
lines changed

constants.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@
2121
package windows
2222

2323
const (
24-
// This process access rights are missing from Go's syscall package as of 1.10.3
25-
26-
// PROCESS_VM_READ right allows to read memory from the target process.
24+
// Deprecated: use x/sys/windows
2725
PROCESS_VM_READ = 0x10
2826

29-
// PROCESS_QUERY_LIMITED_INFORMATION right allows to access a subset of the
30-
// information granted by PROCESS_QUERY_INFORMATION. Not available in XP
31-
// and Server 2003.
27+
// Deprecated: use x/sys/windows
3228
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
3329
)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/stretchr/testify v1.9.0
7-
golang.org/x/sys v0.18.0
7+
golang.org/x/sys v0.24.0
88
)
99

1010
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
66
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7-
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
8-
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7+
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
8+
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
99
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1010
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1111
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

kernel32.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ import (
2626
"syscall"
2727
"time"
2828
"unsafe"
29+
30+
"golang.org/x/sys/windows"
2931
)
3032

3133
// Syscalls
3234
//sys _GetNativeSystemInfo(systemInfo *SystemInfo) = kernel32.GetNativeSystemInfo
3335
//sys _GetTickCount64() (millis uint64, err error) = kernel32.GetTickCount64
3436
//sys _GetSystemTimes(idleTime *syscall.Filetime, kernelTime *syscall.Filetime, userTime *syscall.Filetime) (err error) = kernel32.GetSystemTimes
3537
//sys _GlobalMemoryStatusEx(buffer *MemoryStatusEx) (err error) = kernel32.GlobalMemoryStatusEx
36-
//sys _ReadProcessMemory(handle syscall.Handle, baseAddress uintptr, buffer uintptr, size uintptr, numRead *uintptr) (err error) = kernel32.ReadProcessMemory
3738
//sys _GetProcessHandleCount(handle syscall.Handle, pdwHandleCount *uint32) (err error) = kernel32.GetProcessHandleCount
3839

3940
var (
@@ -224,18 +225,16 @@ func GlobalMemoryStatusEx() (MemoryStatusEx, error) {
224225
return memoryStatusEx, nil
225226
}
226227

227-
// ReadProcessMemory reads from another process memory. The Handle needs to have
228-
// the PROCESS_VM_READ right.
229-
// A zero-byte read is a no-op, no error is returned.
230-
func ReadProcessMemory(handle syscall.Handle, baseAddress uintptr, dest []byte) (numRead uintptr, err error) {
228+
// Deprecated: use x/sys/windows
229+
func ReadProcessMemory(handle syscall.Handle, baseAddress uintptr, dest []byte) (uintptr, error) {
231230
n := len(dest)
232231
if n == 0 {
233232
return 0, nil
234233
}
235-
if err = _ReadProcessMemory(handle, baseAddress, uintptr(unsafe.Pointer(&dest[0])), uintptr(n), &numRead); err != nil {
236-
return 0, err
237-
}
238-
return numRead, nil
234+
235+
var numRead uintptr
236+
err := windows.ReadProcessMemory(windows.Handle(handle), baseAddress, &dest[0], uintptr(n), &numRead)
237+
return numRead, err
239238
}
240239

241240
// GetProcessHandleCount retrieves the number of open handles of a process.

ntdll.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"fmt"
2525
"syscall"
2626
"unsafe"
27+
28+
"golang.org/x/sys/windows"
2729
)
2830

2931
const (
@@ -108,20 +110,11 @@ type RtlUserProcessParameters struct {
108110
CommandLine UnicodeString
109111
}
110112

111-
// Syscalls
112-
// Warning: NtQueryInformationProcess is an unsupported API that can change
113-
// in future versions of Windows. Available from XP to Windows 10.
114-
//sys _NtQueryInformationProcess(handle syscall.Handle, infoClass uint32, info uintptr, infoLen uint32, returnLen *uint32) (ntStatus uint32) = ntdll.NtQueryInformationProcess
115-
116-
// NtQueryInformationProcess is a wrapper for ntdll.NtQueryInformationProcess.
117-
// The handle must have the PROCESS_QUERY_INFORMATION access right.
118-
// Returns an error of type NTStatus.
119-
func NtQueryInformationProcess(handle syscall.Handle, infoClass ProcessInfoClass, info unsafe.Pointer, infoLen uint32) (returnedLen uint32, err error) {
120-
status := _NtQueryInformationProcess(handle, uint32(infoClass), uintptr(info), infoLen, &returnedLen)
121-
if status != 0 {
122-
return returnedLen, NTStatus(status)
123-
}
124-
return returnedLen, nil
113+
// Deprecated: use x/sys/windows
114+
func NtQueryInformationProcess(handle syscall.Handle, infoClass ProcessInfoClass, info unsafe.Pointer, infoLen uint32) (uint32, error) {
115+
var returnedLen uint32
116+
err := windows.NtQueryInformationProcess(windows.Handle(handle), int32(infoClass), info, infoLen, &returnedLen)
117+
return returnedLen, err
125118
}
126119

127120
// Error prints the wrapped NTSTATUS in hex form.

psapi.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import (
2424
"fmt"
2525
"syscall"
2626
"unsafe"
27+
28+
"golang.org/x/sys/windows"
2729
)
2830

2931
// Syscalls
3032
//sys _GetProcessMemoryInfo(handle syscall.Handle, psmemCounters *ProcessMemoryCountersEx, cb uint32) (err error) = psapi.GetProcessMemoryInfo
3133
//sys _GetProcessImageFileNameA(handle syscall.Handle, imageFileName *byte, nSize uint32) (len uint32, err error) = psapi.GetProcessImageFileNameA
32-
//sys _EnumProcesses(lpidProcess *uint32, cb uint32, lpcbNeeded *uint32) (err error) = psapi.EnumProcesses
3334

3435
var (
3536
sizeofProcessMemoryCountersEx = uint32(unsafe.Sizeof(ProcessMemoryCountersEx{}))
@@ -90,7 +91,7 @@ func GetProcessImageFileName(handle syscall.Handle) (string, error) {
9091
func EnumProcesses() (pids []uint32, err error) {
9192
for nAlloc, nGot := uint32(128), uint32(0); ; nAlloc *= 2 {
9293
pids = make([]uint32, nAlloc)
93-
if err = _EnumProcesses(&pids[0], nAlloc*4, &nGot); err != nil {
94+
if err = windows.EnumProcesses(pids, &nGot); err != nil {
9495
return nil, err
9596
}
9697
if nGot/4 < nAlloc {

version.go

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,11 @@ import (
2424
"errors"
2525
"fmt"
2626
"unsafe"
27-
)
2827

29-
// Syscalls
30-
//sys _GetFileVersionInfo(filename string, reserved uint32, dataLen uint32, data *byte) (success bool, err error) [!success] = version.GetFileVersionInfoW
31-
//sys _GetFileVersionInfoSize(filename string, handle uintptr) (size uint32, err error) = version.GetFileVersionInfoSizeW
32-
//sys _VerQueryValueW(data *byte, subBlock string, pBuffer *uintptr, len *uint32) (success bool, err error) [!success] = version.VerQueryValueW
28+
"golang.org/x/sys/windows"
29+
)
3330

34-
// FixedFileInfo contains version information for a file. This information is
35-
// language and code page independent. This is an equivalent representation of
36-
// VS_FIXEDFILEINFO.
37-
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms646997(v=vs.85).aspx
31+
// Deprecated: use x/sys/windows
3832
type FixedFileInfo struct {
3933
Signature uint32
4034
StrucVersion uint32
@@ -83,25 +77,20 @@ func (d VersionData) QueryValue(key string) (string, error) {
8377
CodePage uint16
8478
}
8579

86-
var dataPtr uintptr
87-
var size uint32
88-
if _, err := _VerQueryValueW(&d[0], `\VarFileInfo\Translation`, &dataPtr, &size); err != nil || size == 0 {
80+
var langCodePage *LangAndCodePage
81+
langCodeLen := uint32(unsafe.Sizeof(*langCodePage))
82+
if err := windows.VerQueryValue(unsafe.Pointer(&d[0]), `\VarFileInfo\Translation`, (unsafe.Pointer)(&langCodePage), &langCodeLen); err != nil || langCodeLen == 0 {
8983
return "", fmt.Errorf("failed to get list of languages: %w", err)
9084
}
9185

92-
offset := int(dataPtr - (uintptr)(unsafe.Pointer(&d[0])))
93-
if offset <= 0 || offset > len(d)-1 {
94-
return "", errors.New("invalid address")
95-
}
96-
97-
l := *(*LangAndCodePage)(unsafe.Pointer(&d[offset]))
98-
99-
subBlock := fmt.Sprintf(`\StringFileInfo\%04x%04x\%v`, l.Language, l.CodePage, key)
100-
if _, err := _VerQueryValueW(&d[0], subBlock, &dataPtr, &size); err != nil || size == 0 {
86+
var dataPtr uintptr
87+
var size uint32
88+
subBlock := fmt.Sprintf(`\StringFileInfo\%04x%04x\%v`, langCodePage.Language, langCodePage.CodePage, key)
89+
if err := windows.VerQueryValue(unsafe.Pointer(&d[0]), subBlock, (unsafe.Pointer)(&dataPtr), &size); err != nil || langCodeLen == 0 {
10190
return "", fmt.Errorf("failed to query %v: %w", subBlock, err)
10291
}
10392

104-
offset = int(dataPtr - (uintptr)(unsafe.Pointer(&d[0])))
93+
offset := int(dataPtr - (uintptr)(unsafe.Pointer(&d[0])))
10594
if offset <= 0 || offset > len(d)-1 {
10695
return "", errors.New("invalid address")
10796
}
@@ -118,38 +107,30 @@ func (d VersionData) QueryValue(key string) (string, error) {
118107
// version-information resource. It queries the root block to get the
119108
// VS_FIXEDFILEINFO value.
120109
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647464(v=vs.85).aspx
121-
func (d VersionData) FixedFileInfo() (*FixedFileInfo, error) {
110+
func (d VersionData) FixedFileInfo() (*windows.VS_FIXEDFILEINFO, error) {
122111
if len(d) == 0 {
123112
return nil, errors.New("use GetFileVersionInfo to initialize VersionData")
124113
}
125114

126-
var dataPtr uintptr
127-
var size uint32
128-
if _, err := _VerQueryValueW(&d[0], `\`, &dataPtr, &size); err != nil {
115+
var fixedInfo *windows.VS_FIXEDFILEINFO
116+
fixedInfoLen := uint32(unsafe.Sizeof(*fixedInfo))
117+
if err := windows.VerQueryValue(unsafe.Pointer(&d[0]), `\`, (unsafe.Pointer)(&fixedInfo), &fixedInfoLen); err != nil {
129118
return nil, fmt.Errorf("VerQueryValue failed for \\: %w", err)
130119
}
131120

132-
offset := int(dataPtr - (uintptr)(unsafe.Pointer(&d[0])))
133-
if offset <= 0 || offset > len(d)-1 {
134-
return nil, errors.New("invalid address")
135-
}
136-
137-
// Make a copy of the struct.
138-
ffi := *(*FixedFileInfo)(unsafe.Pointer(&d[offset]))
139-
140-
return &ffi, nil
121+
return fixedInfo, nil
141122
}
142123

143124
// GetFileVersionInfo retrieves version information for the specified file.
144125
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647003(v=vs.85).aspx
145126
func GetFileVersionInfo(filename string) (VersionData, error) {
146-
size, err := _GetFileVersionInfoSize(filename, 0)
127+
size, err := windows.GetFileVersionInfoSize(filename, nil)
147128
if err != nil {
148129
return nil, fmt.Errorf("GetFileVersionInfoSize failed: %w", err)
149130
}
150131

151132
data := make(VersionData, size)
152-
_, err = _GetFileVersionInfo(filename, 0, uint32(len(data)), &data[0])
133+
err = windows.GetFileVersionInfo(filename, 0, uint32(len(data)), unsafe.Pointer(&data[0]))
153134
if err != nil {
154135
return nil, fmt.Errorf("GetFileVersionInfo failed: %w", err)
155136
}

zsyscall_windows.go

Lines changed: 8 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)