2013/08/05

Twitterで「Go言語で、いわゆるプラグインの動的ロードってできる?」という発言を見かけたので。 CGOが書けるのでなんでも出来ます。Linux であれば goffi を使えばほぼ何でも出来ます。
cookieo9/goffi - GitHub

Go FFI (and dlopen) packages to wrap C libraries. This code is not being actively d...

https://github.com/cookieo9/goffi
Windows であれば CGO を使わずとも、もともと Windows API には LoadLibrary、GetProcAddress という物が用意されており、Go言語にもその wrapper が提供されています。 package main

import (
    "log"
    "syscall"
    "unsafe"
)

func main() {
    dll, err := syscall.LoadDLL("user32.dll")
    if err != nil {
        log.Fatal(err)
    }
    defer dll.Release()

    proc, err := dll.FindProc("MessageBoxW")
    if err != nil {
        log.Fatal(err)
    }

    proc.Call(0,
        uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("メッセージ"))),
        uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("ボックス"))),
        0)
}
golang
便利ですね。
Posted at 17:32 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.