首页 > 解决方案 > 如何使用 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#)?

标签: godll

解决方案


这里有一个 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
}

推荐阅读