From ae083e9d9d1a3d04bb1e7353cca43b83d29e4bac Mon Sep 17 00:00:00 2001 From: kingecg Date: Sun, 21 Sep 2025 14:13:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=8A=A8=E6=80=81=E5=BA=93?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=8A=9F=E8=83=BD=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AF=A6=E7=BB=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interceptor/dl.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/interceptor/dl.go b/interceptor/dl.go index 0b9e667..cb19d09 100644 --- a/interceptor/dl.go +++ b/interceptor/dl.go @@ -1,3 +1,5 @@ +// Package interceptor 提供了动态加载和调用共享库函数的功能。 +// 该包使用purego库实现跨平台的动态库加载,支持Windows的DLL和Linux的SO文件。 package interceptor import ( @@ -9,11 +11,15 @@ import ( "github.com/ebitengine/purego" ) +// funcType 定义了函数的参数类型和返回值类型。 +// 用于在动态加载库函数时指定函数签名。 type funcType struct { ParamTypes []string ReturnTypes []string } +// typeMap 将字符串类型名映射到对应的Go reflect.Type。 +// 支持基本数据类型如int、float、string等,以及特殊的"void"类型表示无返回值。 var typeMap = map[string]reflect.Type{ "int": reflect.TypeOf(int(0)), "int32": reflect.TypeOf(int32(0)), @@ -26,6 +32,10 @@ var typeMap = map[string]reflect.Type{ "void": nil, // 用于表示无返回值 } +// makeFuncType 根据给定的参数类型和返回值类型字符串列表,创建对应的函数类型。 +// paramTypes: 函数参数类型的字符串列表 +// returnTypes: 函数返回值类型的字符串列表 +// 返回: 创建的函数类型和可能的错误 func makeFuncType(paramTypes, returnTypes []string) (reflect.Type, error) { var in, out []reflect.Type @@ -53,6 +63,11 @@ func makeFuncType(paramTypes, returnTypes []string) (reflect.Type, error) { return reflect.FuncOf(in, out, false), nil } +// loadLibrary 加载指定路径的动态库文件,并根据提供的函数映射表注册库中的函数。 +// 如果库已经被加载过,则直接返回之前加载的实例。 +// libPath: 动态库文件的路径 +// libFuncMap: 库中函数名到函数类型的映射表 +// 返回: 包含所有注册函数的接口实例 func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]funcType) interface{} { libName := filepath.Base(libPath) libName = strings.TrimSuffix(libName, filepath.Ext(".dll")) @@ -104,6 +119,8 @@ func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]func return a.libMap[libName] } +// Close 关闭所有已加载的动态库句柄,释放资源。 +// 应在不再需要使用动态库时调用此方法。 func (a *Ankointerceptor) Close() { for _, lib := range a.libhabdles { purego.Dlclose(lib)