golang cgo调用dll

golang cgo调用dll

package main

import (
    "syscall"
)

func main(){
    h, err := syscall.LoadLibrary("kernel32.dll")
    if err != nil {
        panic("LoadLibrary failed: " + err.Error())
    }
    defer syscall.FreeLibrary(h)
    proc, err := syscall.GetProcAddress(h, "GetVersion")
    if err != nil {
        panic("GetProcAddress failed: " + err.Error())
    }
    r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
    major := byte(uint32(r))
    minor := uint8(uint32(r) >> 8)
    build := uint16(uint32(r) >> 16)
    print("windows version ", major, ".", minor, " (Build ", build, ")\n")
}

output

GOROOT=C:\Go #gosetup
GOPATH=E:\kaisawind\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\kaisa\AppData\Local\Temp\___go_build_github_com_kaisawind_ffmpeg_go_examples_avio_dir_cmd.exe github.com/kaisawind/ffmpeg.go/examples/avio_dir_cmd #gosetup
C:\Users\kaisa\AppData\Local\Temp\___go_build_github_com_kaisawind_ffmpeg_go_examples_avio_dir_cmd.exe #gosetup
windows version 6.2 (Build 9200)

Process finished with exit code 0

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

文章标题:golang cgo调用dll

本文作者:kaisawind

发布时间:2019-11-19, 06:23:54

最后更新:2020-07-21, 14:35:46

原始链接:https://kaisawind.gitee.io/2019/11/18/2019-11-19-cgo-dll/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏