mirror of
https://github.com/golang/sys.git
synced 2026-02-08 03:36:03 +03:00
windows: add (*DLL).FindProcByOrdinal
Change-Id: I0b0ef2c62994fad4d89bf15c8c6c95fa660190c3
GitHub-Last-Rev: 379ab4a0f5
GitHub-Pull-Request: golang/sys#69
Reviewed-on: https://go-review.googlesource.com/c/sys/+/227439
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
This commit is contained in:
committed by
Alex Brainman
parent
3aab700007
commit
6aff5f38e5
@@ -104,6 +104,35 @@ func (d *DLL) MustFindProc(name string) *Proc {
|
||||
return p
|
||||
}
|
||||
|
||||
// FindProcByOrdinal searches DLL d for procedure by ordinal and returns *Proc
|
||||
// if found. It returns an error if search fails.
|
||||
func (d *DLL) FindProcByOrdinal(ordinal uintptr) (proc *Proc, err error) {
|
||||
a, e := GetProcAddressByOrdinal(d.Handle, ordinal)
|
||||
name := "#" + itoa(int(ordinal))
|
||||
if e != nil {
|
||||
return nil, &DLLError{
|
||||
Err: e,
|
||||
ObjName: name,
|
||||
Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),
|
||||
}
|
||||
}
|
||||
p := &Proc{
|
||||
Dll: d,
|
||||
Name: name,
|
||||
addr: a,
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// MustFindProcByOrdinal is like FindProcByOrdinal but panics if search fails.
|
||||
func (d *DLL) MustFindProcByOrdinal(ordinal uintptr) *Proc {
|
||||
p, e := d.FindProcByOrdinal(ordinal)
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// Release unloads DLL d from memory.
|
||||
func (d *DLL) Release() (err error) {
|
||||
return FreeLibrary(d.Handle)
|
||||
|
||||
@@ -79,3 +79,24 @@ func TestGetWindowsDirectory(t *testing.T) {
|
||||
t.Fatalf("System Windows directory does not end in windows: %s", d2)
|
||||
}
|
||||
}
|
||||
func TestFindProcByOrdinal(t *testing.T) {
|
||||
// Attempt calling shlwapi.dll:IsOS, resolving it by ordinal, as
|
||||
// suggested in
|
||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb773795.aspx
|
||||
dll, err := windows.LoadDLL("shlwapi.dll")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load shlwapi.dll: %s", err)
|
||||
}
|
||||
procIsOS, err := dll.FindProcByOrdinal(437)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not find shlwapi.dll:IsOS by ordinal: %s", err)
|
||||
}
|
||||
if procIsOS.Name != "#437" {
|
||||
t.Fatalf("Proc's name is incorrect: %s,expected #437", procIsOS.Name)
|
||||
}
|
||||
const OS_NT = 1
|
||||
r, _, _ := syscall.Syscall(procIsOS.Addr(), 1, OS_NT, 0, 0)
|
||||
if r == 0 {
|
||||
t.Error("shlwapi.dll:IsOS(OS_NT) returned 0, expected non-zero value")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user