syscall.LoadLibrary can't load user library?

2,271 views
Skip to first unread message

CrossWall

unread,
Jun 21, 2011, 9:35:43 PM6/21/11
to golan...@googlegroups.com
I can only see some example of load system libraries  kernel32.dll
can it load user library?
thanks.

brainman

unread,
Jun 21, 2011, 9:40:02 PM6/21/11
to golan...@googlegroups.com
Yes.

Alex

CrossWall

unread,
Jun 21, 2011, 9:46:11 PM6/21/11
to golan...@googlegroups.com
can load or not?

Krzysztof Kowalczyk

unread,
Jun 21, 2011, 10:23:37 PM6/21/11
to golan...@googlegroups.com
Yes, you can load any library you want. See
http://code.google.com/p/go-wiki/wiki/WindowsDLLs for an example.

-- kjk

On Tue, Jun 21, 2011 at 6:46 PM, CrossWall <tiantia...@gmail.com> wrote:
> can load or not?

CrossWall

unread,
Jun 21, 2011, 10:48:30 PM6/21/11
to golan...@googlegroups.com
sorry for my obscure expression.
I mean to load user generated dll.
but not the user.dll or user32.dll.
thanks.

I can create a dynamic dll say hello.dll, I can use C to load it as shared library.

but I can't do 
syscall.LoadLibrary("./somedir/hello.dll")

Krzysztof Kowalczyk

unread,
Jun 22, 2011, 12:30:02 AM6/22/11
to golan...@googlegroups.com
That should work. syscall.LoadLibrary() simply calls LoadLibrary()
(http://msdn.microsoft.com/en-us/library/ms684175(v=vs.85).aspx,
http://golang.org/src/pkg/syscall/zsyscall_windows_386.go?h=LoadLibrary#L103)
so it'll load any arbitrary .dll. However, if you call it with invalid
arguments, it won't work.

In this case my best guess is that the path is wrong but it could be a
myriad of other issues like the .dll not being a valid dll file or the
file having wrong permissions.

What exactly is the problem? Have you checked errno result? Have you
checked what this error means in Windows to better understand the
exact cause of the problem? Have you tried providing a full (and not
relative) path to the .dll? Have your tried using Windows (and not
Unix) style path (i.e c:\foo\bar.dll and not /foo/bar)? How did you
verify that the dll is valid?

-- kjk

mattn

unread,
Jun 22, 2011, 12:44:13 AM6/22/11
to golan...@googlegroups.com
What error occur? Check with following.

h, e := syscall.LoadLibrary("./somedir/hello.dll")
if e != nil {
println(e.String())
}

brainman

unread,
Jun 22, 2011, 1:10:59 AM6/22/11
to golan...@googlegroups.com
You don't tell us what the error is. But, I suspect, your problem is your filename.

From the doco (http://msdn.microsoft.com/en-us/library/ms684175%28v=vs.85%29.aspx):

>>>
When specifying a path, be sure to use backslashes (\), not forward slashes (/).
<<<

Alex

CrossWall

unread,
Jun 22, 2011, 1:34:35 PM6/22/11
to golan...@googlegroups.com
I'm using mingw environment.
----------------------------------------------------------------------------
when using absolute path in the LoadLibrary 
h, e := syscall.LoadLibrary("/extra/go/somedir/hello.dll")
e is an int, value is 126
-----------------------------------------------------------------------------
when using other types of path, like put the dll same path of the exe ("hello.dll") or ("../hello.dll"), or use window style url (double backslash), or run the exe from from windows cmd.
It'll show a message box say "8.out.exe stoped"
the problem signature is:
  problem name: APPCRASH
  app name: 8.out.exe
  app version: 0.0.0.0
  app time stamp: 4e0225a5
  problematic module name: 8.out.exe
  problematic module name: 0.0.0.0
  problematic module time stamp: 4e0225a5
  exception code: c0000005
  exception offset: 00001ce7
  OS version: 6.1.7600.2.0.0.256.1
  
  other info 1: d780
  other info 2: d7805a47fc6e97a786cd7142859c6773
  other info 3: 5f80
  其他信息 4: 5f80626933ceb17bc02ae0312c8392f3

Krzysztof Kowalczyk

unread,
Jun 22, 2011, 2:49:55 PM6/22/11
to golan...@googlegroups.com
According to the internets 126 is: "The specified module could not be
found. ERROR_MOD_NOT_FOUND" i.e. the file name you gave to LoadLibrary
was wrong.

After you fixed the path, your app crashed with C0000005 exception
which is access violation, which means that you're reading or writing
memory that doesn't belong to you
(http://blogs.msdn.com/b/calvin_hsia/archive/2004/06/30/170344.aspx).

It's either a bug in your dll or the way your Go code calls the code
in the dll. Either way, at this point you're on your own.

-- kjk

brainman

unread,
Jun 22, 2011, 7:26:48 PM6/22/11
to golan...@googlegroups.com
If you can provide instructions on how to reproduce your problem (http://code.google.com/p/go/issues/list), someone might be able to help you.

Alex
Message has been deleted

CrossWall

unread,
Jun 22, 2011, 8:07:57 PM6/22/11
to golan...@googlegroups.com
the dll generated from c file:
$ cat hello.c
int Hello() {
    return 999;
}
$ cat hello.h
int Hello();
--------------------------
gcc -shared hello.c -o hello.dll
--------------------------
use a main.c to test the dll
$ cat main.c
#include <stdio.h>
#include "hello.h"

int main() {
    printf("%d", Hello());
    return 0;
}
----------------------------
gcc main.c ./hello.dll    
---------------------------
./a.exe
works fine.
---------------------------
the problem is NOT happen when  calls the code
in the dll.
because only 

h, err := syscall.LoadLibrary("hello.dll")

don't do anything will get the exception dislog of C0000005 exception

Krzysztof Kowalczyk

unread,
Jun 22, 2011, 8:24:09 PM6/22/11
to golan...@googlegroups.com
I don't really get if you're saying that the crash happens inside
LoadLibrary() or when you're calling the code inside dll after that.

Either way, the first step is to verify that your hello.dll is a valid
Windows dll. To do that write a test program, using Visual C++, that
uses LoadLibrary() to load hello.dll and call Hello() function.

I don't know the gcc toolchain you're using but by using it you
introduce a lot of uncertainty about what exactly does it generate,
which is why it's important to do the test using native Windows tools
i.e. Visual C++. If that works then the bug is probably in your Go
code that calls the function (and that you haven't shown us).

-- kjk

On Wed, Jun 22, 2011 at 5:05 PM, CrossWall <tiantia...@gmail.com> wrote:
> the dll generated from c file:
> $ cat hello.c
> int Hello() {
>     return 999;
> }
> $ cat hello.h
> int Hello();
> --------------------------
> gcc -shared hello.c -o hello.dll
> --------------------------
> use a main.c to test the dll
> $ cat main.c
> #include <stdio.h>
> #include "hello.h"
> int main() {
>     printf("%d", Hello());
>     return 0;
> }
> ----------------------------
> gcc main.c ./hello.dll
> ---------------------------
> ./a.exe
> works fine.
> ---------------------------

> the problem is happen when  calls the code
> in the dll.

brainman

unread,
Jun 22, 2011, 8:43:59 PM6/22/11
to golan...@googlegroups.com
You didn't provide any go program, so I wrote my own:

package main

import (
    "fmt"
    "log"
    "syscall"
)

func main() {
    h, e := syscall.LoadLibrary("hello.dll")
    if e != 0 {
        log.Fatal(syscall.Errstr(e))
    }
    defer syscall.FreeLibrary(h)
    proc, e := syscall.GetProcAddress(h, "Hello")
    if e != 0 {
        log.Fatal(syscall.Errstr(e))
    }
    n, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
    fmt.Printf("Hello dll function returns %d\n", n)
}

And it runs fine:

$ ./hello.exe
Hello dll function returns 999

I agree with kjk. I suspect, you're just using a gcc compiler, not mingw gcc compiler on Windows. For example, I could create shared object file, as per your instructions, on my linux computer, but it will have nothing to do with Windows dll, even you named it hello.dll. If you use any proper tool to create your dll, would that be mingw gcc, Microsoft Visual Studio, your favorite hex editor, you will not have any problems.

Alex

CrossWall

unread,
Jun 22, 2011, 8:46:15 PM6/22/11
to golan...@googlegroups.com
when generate the hello.dll with VC6++ tool.

the go programs get:

$ ./8.out.exe
panic: 127

runtime.panic+0x9e /MinGW/msys/1.0/extra/go/src/pkg/runtime/proc.c:1060
        runtime.panic(0x442954, 0x7f)
main.main+0x141 D:/MinGW/msys/1.0/extra/go/misc/swig/libtest/go/test.go:22
        main.main()
runtime.mainstart+0xf 386/asm.s:93
        runtime.mainstart()
runtime.goexit /MinGW/msys/1.0/extra/go/src/pkg/runtime/proc.c:178
        runtime.goexit()
----- goroutine created by -----
_rt0_386+0xbf 386/asm.s:80


------------------
run from windows cmd gets the same error..

brainman

unread,
Jun 22, 2011, 9:03:13 PM6/22/11
to golan...@googlegroups.com
We can't help you, if you don't help us. DO SHOW YOUR PROGRAMS! Do not assume we have some brain ability to reconstruct your programs just by looking at your error messages :-).

Alex

mattn

unread,
Jun 23, 2011, 12:24:38 AM6/23/11
to golan...@googlegroups.com
CrossWall, Note that you include gcc's version into the issue. :)

Reply all
Reply to author
Forward
0 new messages