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

×

喜欢就点赞,疼爱就打赏