windows – Failed to load dll. Specified module could not be found-ThrowExceptions

Exception or error:

I have some code that I wrote in golang (on ubuntu) and tried to package as a windows exe but unfortunately, due to some cgo dependencies from a github project, I ended up having to build my package as a dll as per this answer https://stackoverflow.com/a/49079049/4750381 because it would not compile as a runnable exe file for windows (even using MinGw).

My compile line was:

GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -buildmode=c-shared -o main.dll main.go

My main package code looks like this:

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()
}

I wrote another go script (using windows this time) to try to load and read that DLL file:


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")
}

But now I always get the error below even though my file path is 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

Please could someone tell me what I’m doing wrong? I’ve been tweaking this all day with little success. Ideally I would want to build an exe straight from ubuntu without the dll but if that’s not feasible, I would at least like to be able to run my dll from another exe file.
Thanks for any assistance.

********** EDIT****************************
So I wrote some C++ code to try to open the dll file (made 64 and 32 bit versions of both)

#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;
}

Same issue:
Screenshot showing could not load

As you can see from this below screenshot, the file path matches so I have no idea what’s going on.
Screenshot showing paths confirmed

How to solve:

First,you need export dll method like this:

// export dllRun
func dllRun() {

There is a simple example,
https://github.com/whtiehack/checkdll_log

Second,
Go main program could not load Go dll correct because
https://github.com/golang/go/issues/34168
https://github.com/golang/go/issues/22192

In summary, in Windows, there can not have more than two go runtimes in a main program.

Leave a Reply

Your email address will not be published. Required fields are marked *