Go modules and relative paths to resources

Hey there :slight_smile:

I have a problem wirh one of my modules. Basicly it can’t find my *.dll file that I included into the module. The problem is if I try to use the module in one of my other modules I get a error.

I have the following file structure:

- yjuqsoft
-- lua53
--- go.mod
--- lua.go
--- lua53.dll
-- test
--- go.mod
--- main.go

Here the content of the files:

yjuqsoft/lua53/go.mod

module yjuqsoft/lua53

go 1.14

require golang.org/x/sys v0.0.0-20200331124033-c3d80250170d

yjuqsoft/lua53/lua.go

package lua53

import (
	"fmt"

	"golang.org/x/sys/windows"
)

var dll *windows.DLL

func init() {
	tmp, err := windows.LoadDLL("lua53.dll") // problematic line
	dll = tmp
	fmt.Println(err)
}

// DllRelease unloads DLL from memory
func DllRelease() {
	dll.Release()
}

yjuqsoft/test/go.mod

module yjuqsoft/test

go 1.14

require yjuqsoft/lua53 v0.0.0

replace yjuqsoft/lua53 => ../lua53

yjuqsoft/test/main.go

package main

import _ "yjuqsoft/lua53"

func main() {
	// lua53.DllRelease()
}

If I gonna run this, I get the following output: Failed to load lua53.dll: The specified module could not be found.

All right,… The module can’t be found. If I use an absolute path everything works fine. But I don’t want to do that since my module yjuqsoft/lua53 should be independent. So, how do I gonna solve that?

By the way, all files are stored on a usb stick. The GOPATH didn’t point to this USB Stick. If it matters for some reasons.

@Yjuq how are you running this? I see the DLL file is with your Go source code, but if you’re running something like go install, the binary is probably being put somewhere else. You might need to manually move/copy that file when you install.

I just go into the test directory and use go build and run it. I think the problem is that the working directory is simply wrong. I just found a solution that works for me. So basicly get rid of the relative path and generate a absolute path based on where the lua.go file is stored.

package lua53

import (
	"golang.org/x/sys/windows"
	"path/filepath"
	"runtime"
)

var dll *windows.DLL

func init() {
	_, path, _, _ := runtime.Caller(0)
	path = filepath.Join(filepath.Dir(path), "lua53.dll")
	dll = windows.MustLoadDLL(path)
}

// DllRelease unloads DLL from memory
func DllRelease() {
	dll.Release()
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.