# config.dll 使用教程 ## 1.创建DLL工具实例 ### 加载DLL文件 ```gotemplate // ConfigDLL 配置文件读取DLL结构 type ConfigDLL struct { dll *syscall.DLL readConfigFile *syscall.Proc // 读取配置文件 getVersion *syscall.Proc // 获取版本信息 freeCString *syscall.Proc // 释放C字符串 } // 初始化ConfigDLL func InitConfigDLL() (*ConfigDLL, error) { dllPath := filepath.Join("dll", "config.dll") if _, err := os.Stat(dllPath); os.IsNotExist(err) { return nil, fmt.Errorf("config DLL 不存在: %s", dllPath) } if dll, err := syscall.LoadDLL(dllPath); err != nil { return nil, fmt.Errorf("加载config DLL 失败: %s", err) } else { return &ConfigDLL{ dll: dll, readConfigFile: dll.MustFindProc("ReadConfigFile"), getVersion: dll.MustFindProc("GetVersion"), freeCString: dll.MustFindProc("FreeCString"), }, nil } } dll, err := InitConfigDLL() ``` #### 获取C字符串 ```gotemplate // cStr 获取C字符串 func (m *ConfigDLL) cStr(p uintptr) string { if p == 0 { return "" } b := []byte{} for i := uintptr(0); ; i++ { c := *(*byte)(unsafe.Pointer(p + i)) if c == 0 { break } b = append(b, c) } s := string(b) if m.freeCString != nil { m.freeCString.Call(p) } return s } ``` ### 2. 使用DLL函数示例 ```gotemplate // 读取配置文件 func (m *ConfigDLL) ReadConfigFile(filePath, fileName string) (string, error) { proc, err := m.dll.FindProc("ReadConfigFile") if err != nil { return "", fmt.Errorf("找不到函数 ReadConfigFile: %v", err) } filePathPtr, _ := syscall.BytePtrFromString(filePath) fileNamePtr, _ := syscall.BytePtrFromString(fileName) resultPtr, _, _ := proc.Call( uintptr(unsafe.Pointer(filePathPtr)), uintptr(unsafe.Pointer(fileNamePtr)), ) result := m.cStr(resultPtr) return result, nil } ``` # 接口详情 ## 读取配置文件接口--ReadConfigFile ### 请求信息 ```gotemplate dll.ReadConfigFile(filePath, fileName) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|----------| | filePath | string | 是 | 文件路径(可以是本地路径或HTTP/HTTPS URL) | | fileName | string | 是 | 配置文件名 | #### 功能说明 ```text 此接口用于读取多种格式的配置文件,支持以下特性: 1.支持的文件格式: JSON (.json) YAML (.yaml, .yml) INI (.ini, .conf) 2.支持的数据源: 本地文件路径 HTTP/HTTPS URL 3.配置解析: JSON/YAML文件解析为Map结构 INI文件支持多节(section)解析 DEFAULT节的内容会合并到顶层 ``` ### 响应示例 ```text JSON字符串 { "database": { "host": "localhost", "port": 3306, "username": "admin" }, "server": { "port": 8080, "timeout": 30 } } ``` ### 错误响应示例 ```text "error": "JSON解析错误: invalid character '}' looking for beginning of object key string" ``` ## 释放C字符串内存--FreeCString ### 请求信息 ```gotemplate dll.FreeCString(str) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|------------| | str | string | 是 | 需要释放的C字符串 | ### 响应示例 ```text 无 ``` ## 获取版本信息接口--GetVersion ### 请求信息 ```gotemplate dll.GetVersion() ``` ### 请求参数 无 ### 响应示例 ```text v3 ``` ### 错误响应示例 ```text { "error": "找不到函数 GetVersion" } ```