登录
首页 >  Golang >  Go问答

在 DLL 中如何根据序号值查找过程?

来源:stackoverflow

时间:2024-03-06 09:00:28 252浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《在 DLL 中如何根据序号值查找过程?》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

我正在尝试使用序号值从 dll 调用过程(没有名称)。

我可以在 c# 中使用此 dll,将序数值设置为 dllimport 的属性 entrypoint

...或者您可以通过其序号来识别入口点。序数词以 # 号为前缀,例如 #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#)?


解决方案


这个有 a github issue here 个,但从 go 1.10.3(我现在使用的版本)开始似乎还没有被合并。

无论如何,github 问题链接到 a changeset with the respective function,我从中提取了代码来执行您想要的操作:

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 并使用新函数,它显示过程地址确实匹配。

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
}

本篇关于《在 DLL 中如何根据序号值查找过程?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>