为什么Go无法在dll中找到指定的过程?
我为Windows x86编译了一个my.dll
库(操作系统是Windows 7 x64;但我使用Go x86二进制文件 - 使用LiteIDE - 并且C#代码也是为x86架构显式编译的)。我在C#中使用它并且它可以工作:
[DllImport("my.dll", EntryPoint = "my_function")]
public static extern double my_function(double x);
但是当我尝试使用它时(这里我只想找到它)来自Go by:
var (
dllMine = syscall.NewLazyDLL("my.dll")
my_function = dllMine.NewProc("my_function")
)
func main() {
err := my_function.Find()
if err != nil {
fmt.Println(err)
return
}
//...
}
它说Failed to find my_function procedure in my.dll: The specified procedure could not be found.
。 my.dll
文件与生成的.exe
文件位于同一目录中。入口点名称("my_function"
)确实存在,因为它在C#中导入时工作正常并且没有说Failed to load my.dll: The specified module could not be found.
。
实际部分:我试图调用的库是swedll32.dll
,它是Swiss Ephemeris的核心库(可以下载here - GNU),只是为了测试这个场景中要调用的函数是swe_julday
;用这个签名重现错误:
double swe_julday(
int year, int month, int day, double hour,
int gregflag); /* Gregorian calendar: 1, Julian calendar: 0 */
另一件事是我的GOROOT
环境参数实际上是一个NTFS交接点(所以我可以在x86和x64版本之间切换) - 但我不认为它是相关的,因为输出{{1生成应用程序没有任何问题(只是为了承认我所有的罪!)。
答案 0 :(得分:4)
我已从http://www.astro.com/ftp/swisseph/sweph.zip下载了你的dll,看看是否有swe_julday函数:
C:\foo>dumpbin /exports swedll32.dll | find "swe_julday"
74 49 0000C440 _swe_julday@24
75 4A 0000D4A0 _swe_julday_d@24
我在那里看不到swe_julday功能。相反,我看到_swe_julday @ 24函数。因此,如果我将您的计划更改为:
C:\foo>type foo.go
package main
import (
"syscall"
"fmt"
)
var (
dllMine = syscall.NewLazyDLL("swedll32.dll")
my_function = dllMine.NewProc("_swe_julday@24")
)
func main() {
err := my_function.Find()
if err != nil {
fmt.Println(err)
return
}
//...
}
它运行没有任何错误:
C:\foo>go run foo.go
C:\foo>