look for a example: call a DLL's function on Windows by CGO

566 views
Skip to first unread message

Fino

unread,
Aug 2, 2017, 11:33:53 PM8/2/17
to golang-nuts
yesterday I just found that, the syscall way to use DLL on Windows cannot use float32&float64 as parameters,

so I am look for a example:  call a DLL's function on Windows in the  CGO way,

the DLL is not Windows' own DLL, but a third-party DLL which have a lot of double&float parameters,

thanks in advance,

BR fino

Konstantin Khomoutov

unread,
Aug 3, 2017, 3:16:50 AM8/3/17
to Fino, golang-nuts
On Wed, Aug 02, 2017 at 08:33:53PM -0700, Fino wrote:

> yesterday I just found that, the syscall way to use DLL on Windows cannot
> use float32&float64 as parameters,

Could you please elaborate on that as I do not beleive this is not
possible: IIRC Go's float types follow IEEE 754 semantics and you should
be able to use them "as is".

If not, maybe it would be possible to use the Float64bits,
Float64FromBits from the math package (for doubles) and their 32-bit
counterparts (for floats).

> so I am look for a example: call a DLL's function on Windows in the CGO
> way,
>
> the DLL is not Windows' own DLL, but a third-party DLL which have a lot of
> double&float parameters,

I would do this only as a last resort: you'd need a working C compiler
to build your package (and that wouldn't be MSVC++, IIRC).

Konstantin Khomoutov

unread,
Aug 3, 2017, 8:26:14 AM8/3/17
to Fino Meng, golan...@googlegroups.com
Hi!

Unfortunately you forgot to use the "reply to all" or "reply to list"
action so the message has only been sent to me, not to the list.
Please re-send/re-post correctly.
> after one day's study, I can use CGO with DLL on Windows now, it seems that
> the CGO's code is same for static or dynmatic libs.
>
> but I still don't know the right way to tell ld where is the DLL.
>
> now I need to put the DLL into this MINGW64's lib path:
> C:\Bin\mingw-w64\x86_64-7.1.0-win32-seh-rt_v5-rev0\mingw64\x86_64-w64-mingw32\lib

Please see the help on the cmd/cgo "package" (running `go help cmd/cgo`
is one way to get this help, or navigate to [1].

Basically you use something like

// #cgo LDFLAGS=-Lpath/to/the/dll -ldllname

in your cgo comment block to make the contents of that variable passed
to the linker.

> for syscall's float problem, I found this:
>
> I'm wondering if we can link a go code via cgo with a dll created with
> mingw-gcc?
>
> It's possible. Add the path to the DLL to the cgo LDFLAGS, and use cgo to
>
> call exported function from DLL as usual.
>
> To call integer functions from DLL, you can also use the syscall package
> (search for DLL
>
> in godoc), but there are some constrains in calling functions using the
> syscall package
>
> (you can't pass floating point arguments, and the stack size is limited to
> 64K if it's a pure
>
> Go program), so the recommended way to use a windows DLL (no matter how
> it's created,
>
> MSVC or mingw) is to use cgo.

I was able to google for one of the exact phrase out of this citation,
and found out that most probably you were referring to this post on
golang-nuts [2]. Next time, please do this yourself so we have a proper
context of the citation (which can be studied and used for future
reference).

I'm not sure what to say about that: minux is definitely a knowledgeable
person; I had extensive experience calling Windows DLLs via the syscall
package but I never had a chance of calling functions which would
require passing double/float arguments from Go.

I wonder whether Alex Brainman maybe has something to say on these
matters.

1. https://golang.org/pkg/cmd/cgo/
2. https://groups.google.com/d/msg/golang-nuts/EdqqBX1EolI/SQ1HXhfoaAsJ

Reply all
Reply to author
Forward
0 new messages