无法加载dll。找不到指定的模块

时间:2019-10-18 02:42:04

标签: windows ubuntu go dll cgo

我有一些用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构建一个没有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

1 个答案:

答案 0 :(得分:0)

首先,您需要像这样导出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不能超过两个。