1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-15 18:22:27 +08:00

增加GetExeDllVersion函数获取exe,dll版本号

This commit is contained in:
guanren
2024-10-18 00:00:18 +08:00
parent 213e2b4ead
commit a74462941e
5 changed files with 74 additions and 4 deletions

View File

@@ -14,8 +14,11 @@ import (
"encoding/csv"
"errors"
"fmt"
"github.com/duke-git/lancet/v2/validator"
"golang.org/x/sys/windows"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
@@ -23,8 +26,7 @@ import (
"sort"
"strings"
"sync"
"github.com/duke-git/lancet/v2/validator"
"unsafe"
)
// FileReader is a reader supporting offset seeking and reading one
@@ -947,3 +949,33 @@ func ParallelChunkRead(filePath string, linesCh chan<- []string, chunkSizeMB, ma
return nil
}
// GetExeDllVersion 获取exe或dll文件的版本信息
func GetExeDllVersion(filePath string) (string, error) {
// 获取版本信息大小
size, err := windows.GetFileVersionInfoSize(filePath, nil)
if err != nil {
return "", fmt.Errorf("无法获取版本信息大小: %w", err)
}
// 读取版本信息
data := make([]byte, size)
err = windows.GetFileVersionInfo(filePath, 0, size, unsafe.Pointer(&data[0]))
if err != nil {
return "", fmt.Errorf("无法获取版本信息: %w", err)
}
// 查询版本信息
var fixedInfo *windows.VS_FIXEDFILEINFO
var fixedInfoLen uint32
err = windows.VerQueryValue(unsafe.Pointer(&data[0]), `\`, unsafe.Pointer(&fixedInfo), &fixedInfoLen)
if err != nil {
return "", fmt.Errorf("无法查询版本信息: %w", err)
}
// 提取版本号
major := fixedInfo.FileVersionMS >> 16
minor := fixedInfo.FileVersionMS & 0xFFFF
build := fixedInfo.FileVersionLS >> 16
revision := fixedInfo.FileVersionLS & 0xFFFF
log.Println(fixedInfo.FileVersionMS)
return fmt.Sprintf("%d.%d.%d.%d", major, minor, build, revision), nil
}

View File

@@ -619,3 +619,10 @@ func TestChunkRead(t *testing.T) {
assert.Equal("Jim,21,male", lines[1])
}
func TestGetExeDllVersion(t *testing.T) {
v, err := GetExeDllVersion(`C:\Program Files\Tencent\WeChat\WeChat.exe`)
if err != nil {
t.Error(err)
}
t.Log(v)
}