windows - 加载dll失败。找不到指定的模块

标签 windows ubuntu go dll cgo

我有一些我在 golang(在 ubuntu 上)编写的代码并尝试打包为 windows exe,但不幸的是,由于来自 github 项目的一些 cgo 依赖项,我最终不得不按照这个答案 https://stackoverflow.com/a/49079049/4750381 将我的包构建为 dll因为它不会编译为 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 构建一个没有 dll 的 exe,但如果这不可行,我至少希望能够从另一个 exe 文件运行我的 dll。
感谢您的任何帮助。

********** 编辑****************************
所以我写了一些 C++ 代码来尝试打开 dll 文件(分别制作了 64 位和 32 位版本)
#define UNICODE
#include <windows.h>
#include <iostream>

/* 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;
}

同样的问题:
Screenshot showing could not load

正如您从下面的屏幕截图中看到的那样,文件路径匹配,所以我不知道发生了什么。
Screenshot showing paths confirmed

最佳答案

首先,您需要像这样的导出 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 runtimes 不能超过两个。在一个主程序中。

关于windows - 加载dll失败。找不到指定的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58443221/

相关文章:

windows - 我如何知道我的外部 IP 地址是什么?

windows - 为 Windows 应用程序制作 .exe 可安装文件

c# - 7zip 中的 7z.sfx 和 7zsd.sfx 模块有什么区别?

python - 通过 SSH 运行 python 进程,即使在关闭 SSH 连接并重新连接后也能监控进程

bash - Ubuntu 中的重定向器 "<<<"?

go - 我如何使用 Go 的 openpgp 包?

windows - Windows 批处理文件末尾的简单插入符 (^) 占用所有内存

c++ - 在 gdb/Ubuntu 14.04.4 LTS 中加载 dl-debug.c

arrays - 在 Golang 中定义包含 slice 的数组映射

logging - 堆栈跟踪作为字符串