go - 如何使用 DLL 的序数值从 DLL 中查找过程?
问题描述
我正在尝试使用序号值从 DLL 调用过程(无名称)。
我可以在 C#中使用这个 DLL,将序数值设置为EntryPoint
.DllImport
...或者您可以通过其序数来识别入口点。序数以# 符号为前缀,例如#1。[...]
C# 中的示例:
[DllImport("dllname.dll", EntryPoint = "#3", CharSet = CharSet.Unicode, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public static extern int DeviceList(ref IntPtr hDeviceList);
在 Go 中尝试查找带有“#”号的程序时,会显示以下错误:
无法在 dllname.dll 中找到 #3 过程:找不到指定的过程。
我用来dumpbin
显示DLL的信息,没有函数有名字:
有没有办法找到具有序数值的过程(如 C#)?
解决方案
这里有一个 github 问题,但从 Go 1.10.3(我现在使用的版本)开始似乎还没有合并。
无论如何,github问题链接到一个变更集,其中包含我从中提取代码以执行您想要的操作的相应函数:
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetProcAddress = kernel32.NewProc("GetProcAddress")
)
// GetProcAddressByOrdinal retrieves the address of the exported
// function from module by ordinal.
func GetProcAddressByOrdinal(module syscall.Handle, ordinal uintptr) (uintptr, error) {
r0, _, _ := syscall.Syscall(procGetProcAddress.Addr(), 2, uintptr(module), ordinal, 0)
proc := uintptr(r0)
if proc == 0 {
return 0, syscall.EINVAL
}
return proc, nil
}
为了完整起见,这里是我测试它的完整示例,使用 Dependecy Walker 我发现 kernel32.dll 中的第一个函数是AcquireSRWLockExclusive
并且使用新函数它表明 proc 地址确实匹配。
package main
import (
"fmt"
"syscall"
)
func main() {
dll, err := syscall.LoadDLL("kernel32.dll")
check(err)
want, err := syscall.GetProcAddress(dll.Handle, "AcquireSRWLockExclusive")
check(err)
fmt.Println(want)
first, err := GetProcAddressByOrdinal(dll.Handle, 1)
check(err)
fmt.Println(first)
}
func check(err error) {
if err != nil {
panic(err)
}
}
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetProcAddress = kernel32.NewProc("GetProcAddress")
)
// GetProcAddressByOrdinal retrieves the address of the exported
// function from module by ordinal.
func GetProcAddressByOrdinal(module syscall.Handle, ordinal uintptr) (uintptr, error) {
r0, _, _ := syscall.Syscall(procGetProcAddress.Addr(), 2, uintptr(module), ordinal, 0)
proc := uintptr(r0)
if proc == 0 {
return 0, syscall.EINVAL
}
return proc, nil
}
推荐阅读
- sql - 解释 SQL 查询
- django - 在运行 redis-CLI 时,我的连接每次都被取消
- python - 在 docker 容器上运行时,Flask App 无法正常工作
- java - 如何制作一个类的所有实例的数组?
- java - 如何检查 LiveData 或 MutableLiveData 是否已设置值?
- python - matplotlib 表:每列范围的单独颜色图
- ansible - 如何从库存中获取组主机
- docker - 从 env var 获取 OAuth 设置,并在键中使用句号
- python-3.x - 如何将带有字典的列拆分为 2 列
- vue.js - 将项目添加到数组时实时重新加载组件。Vue.js