I have the following GO code:
package main
import (
"syscall"
)
func main() {
var mod, _ = syscall.LoadLibrary("user32.dll")
}
Which successfully loads the user32.dll
file. (This is a modified version of an example I found online).
However I do not know where on the filesystem this DLL exists. I have been searching for a way for Go to be able to tell me that through the syscall
libs, but have not found a way to do it.
Does anyone know of a way to get the path to the DLL that was loaded?
I have no access to a Windows machine running Go right now, but it should be something along this lines. Notice the lack of proper resource/error management in the code.
Please tests it and share your results so I can amend/edit my answer.
var (
kernel32, _ = syscall.LoadLibrary("kernel32.dll")
getModuleHandle, _ = syscall.GetProcAddress(kernel32, "GetModuleHandleW")
getModuleFilename, _ = syscall.GetProcAddress(kernel32, "GetModuleFileNameA")
)
func StringToCharPtr(str string) *uint8 {
chars := append([]byte(str), 0) // null terminated
return &chars[0]
}
func GetDllFileName() (filename byte[]) {
var nargs uintptr = 1
var handle uintprt
modulename = StringToPtrChar("user32.dll")
if ret, _, callErr := syscall.Syscall(uintptr(getModuleHandle), nargs, modulename, 0, 0); callErr != 0 {
panic("Call GetModuleHandle")
} else {
handle = ret
}
name = make(byte[],50) // I'm making this number out of the blue
nargs = 3
if ret, _, callErr := syscall.Syscall(uintptr(getModuleFilename), nargs, handle, &name[0], 50); callErr != 0 {
panic("Call GetModuleFileName")
}
else{
filename = name
}
return
}