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

问题描述 投票:0回答:1

我有一些代码,是在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

windows ubuntu go dll cgo
1个回答
0
投票

首先,您需要这样的导出dll方法:

© www.soinside.com 2019 - 2024. All rights reserved.