feat(dll): 自 go-hua 迁入 Windows 系统 API 封装
- 互斥体防多开/ShellExecute/窗口类名与标题查询/顶层窗口目录, 新增依赖 lxn/win - 仅 Windows, 附测试、examples/dll 例程与包 README
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# dll
|
||||
|
||||
Windows 系统 API 封装(基于 `lxn/win`):互斥体防多开、`ShellExecute` 拉起程序、
|
||||
窗口类名/标题查询、顶层窗口路径定位。
|
||||
|
||||
> 迁移自 go-hua/dll,代码保持原样。**仅 Windows 可用**(依赖 user32/kernel32)。
|
||||
|
||||
## 用法
|
||||
|
||||
```go
|
||||
import "git.zeroonesoft.cn/golib/zogo/dll"
|
||||
|
||||
// 互斥体防多开
|
||||
h, err := dll.CreateMutex(false, "Global\\MyAppMutex") // 已存在则 err
|
||||
defer dll.ReleaseMutex(h)
|
||||
|
||||
// 拉起外部程序(hide=true 隐藏窗口)
|
||||
ok := dll.ShellExecute("", "notepad.exe", "C:\\a.txt", false)
|
||||
|
||||
// 窗口查询
|
||||
title, _ := dll.GetWindowTextString(hwnd)
|
||||
cls, _ := dll.GetWindowClassString(hwnd)
|
||||
hwnd2 := dll.GetWindowFromClassName(0, "Chrome_WidgetWin_1")
|
||||
|
||||
// 顶层窗口(主程序)所在目录
|
||||
dir, err := dll.GetTopFolderPath()
|
||||
```
|
||||
|
||||
完整可运行例程:[examples/dll/main.go](../examples/dll/main.go)
|
||||
|
||||
## 注意
|
||||
|
||||
- 构建目标必须含 Windows(`GOOS=windows`);其他平台编译请将该包的使用隔离在
|
||||
带 build tag 的文件里。
|
||||
- `CreateMutex` 的 err 判断用 `windows.ERROR_ALREADY_EXISTS` 语义,见测试用例。
|
||||
Reference in New Issue
Block a user