1
0
mirror of https://github.com/fumiama/gozel.git synced 2026-06-05 00:10:24 +08:00
Files
gozel/internal/zecall/zecall_windows.go
2026-03-20 22:46:06 +08:00

64 lines
1.2 KiB
Go

package gozel
import (
"errors"
"syscall"
)
const (
zeLibraryName = "ze_loader.dll"
)
var (
// ErrZeCallNotInit please call Init() first.
ErrZeCallNotInit = errors.New("zecall not init")
// ErrNoSuchProcess please register the process first.
ErrNoSuchProcess = errors.New("no such process")
)
var (
libZeLoader *syscall.DLL
procMap = map[string]*syscall.Proc{}
)
// Init load lib using syscall.
func Init() error {
h, err := syscall.LoadLibrary(zeLibraryName)
if err != nil {
return err
}
libZeLoader = &syscall.DLL{Handle: h, Name: zeLibraryName}
return nil
}
// Register a process for calling.
func Register(name string) error {
if libZeLoader == nil {
return ErrZeCallNotInit
}
proc, err := libZeLoader.FindProc(name)
if err != nil {
return err
}
procMap[name] = proc
return nil
}
// Call invokes a registered proc by name.
// The go:uintptrescapes directive tells the compiler that args may contain
// pointers converted to uintptr, so the GC will keep them alive during the call.
//
//go:uintptrescapes
func Call(name string, args ...uintptr) (r1, r2 uintptr, err error) {
fn, ok := procMap[name]
if !ok {
return 0, 0, ErrNoSuchProcess
}
r1, r2, err = fn.Call(args...)
if r1 == 0 {
err = nil
}
return
}