加载 dll 失败。找不到指定的模块
来源:stackoverflow
时间:2024-04-04 13:36:33 130浏览 收藏
大家好,今天本人给大家带来文章《加载 dll 失败。找不到指定的模块》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
我有一些用 golang(在 ubuntu 上)编写的代码,并尝试打包为 windows exe,但不幸的是,由于 github 项目的一些 cgo 依赖项,我最终不得不按照以下方式将我的包构建为 dll这个答案https://stackoverflow.com/a/49079049/4750381,因为它不会编译为windows的可运行exe文件(即使使用mingw)。
我的编译行是:
goos=windows goarch=386 cgo_enabled=1 cc=i686-w64-mingw32-gcc go build -buildmode=c-shared -o main.dll main.go
我的主包代码如下所示:
package main import ( "c" "fmt" console "github.com/asynkronit/goconsole" "github.com/asynkronit/protoactor-go/actor" "path/to/repo" ) const cfgpath string = "./config.json" func main() { fmt.println("from main") } func dllrun() { // used for running the test and various other operations, thus generally all lines except 1 will be commented out ctx := actor.emptyrootcontext props := actor.propsfromproducer(testmachine.newtestmachine(cfgpath)) pid, err := ctx.spawnnamed(props, "tm") if err != nil { panic(err) } defer func() { // run after the read line fucntion executes and terminates the program ctx.poison(pid) }() console.readline() }
我编写了另一个 go 脚本(这次使用 windows)来尝试加载和读取该 dll 文件:
import ( "syscall" ) func main() { mydll := syscall.newlazydll("c:/users/konyenso/documents/dllopener/main.dll") maincall := mydll.newproc("dllrun") ret, _, err := maincall.call() if err != nil { panic(err) // calling mydll.maincall failed } if ret == 0 { print("could not set the desired attributes") // todo: call getlasterror to get more information } print("ok") }
但是现在即使我的文件路径没问题,我总是收到以下错误:
panic: failed to load c:/users/konyenso/documents/dllopener/main.dll: the specified module could not be found. goroutine 1 [running]: syscall.(*lazyproc).mustfind(0x13019400) c:/go/src/syscall/dll_windows.go:311 +0x42 syscall.(*lazyproc).call(0x13019400, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4623e0) c:/go/src/syscall/dll_windows.go:327 +0x21 main.main() c:/users/konyenso/documents/dllopener/main.go:11 +0xa5
请有人告诉我我做错了什么?我一整天都在对此进行调整,但收效甚微。理想情况下,我想直接从 ubuntu 构建一个 exe,而不需要 dll,但如果这不可行,我至少希望能够从另一个 exe 文件运行我的 dll。 感谢您的帮助。
************ 编辑**************************** 所以我写了一些c++代码来尝试打开dll文件(制作了64位和32位版本)
#define UNICODE #include#include /* Define a function pointer for our imported * function. * This reads as "introduce the new type f_funci as the type: * pointer to a function returning an int and * taking no arguments. * * Make sure to use matching calling convention (__cdecl, __stdcall, ...) * with the exported function. __stdcall is the convention used by the WinAPI */ typedef int (__stdcall *f_funci)(); int main() { HINSTANCE hGetProcIDDLL = LoadLibrary((LPCWSTR)"C:\\Users\\konyenso\\Documents\\DLLOpener\\main.dll"); if (!hGetProcIDDLL) { std::cout << "could not load the dynamic library" << std::endl; return EXIT_FAILURE; } // resolve function address here f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "dllRun"); if (!funci) { std::cout << "could not locate the function" << std::endl; return EXIT_FAILURE; } std::cout << "funci() returned " << funci() << std::endl; return EXIT_SUCCESS; }
同样的问题: 屏幕截图显示无法加载
正如您从下面的屏幕截图中看到的,文件路径匹配,所以我不知道发生了什么。 显示已确认路径的屏幕截图
解决方案
首先,你需要像这样导出dll方法:
// export dllRun func dllRun() {
有一个简单的例子, https://github.com/whtiehack/checkdll_log
第二,
go main
程序无法正确加载 go dll
因为
https://github.com/golang/go/issues/34168
https://github.com/golang/go/issues/22192
综上所述,在windows中,一个主程序中不能有两个以上的go运行时
。
终于介绍完啦!小伙伴们,这篇关于《加载 dll 失败。找不到指定的模块》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习