package lazydll
import
(
"fmt"
"syscall"
"time"
"unsafe"
)
const (
MB_OK
=
0x00000000
)
func abort(funcname string, err syscall.Errno) {
panic(funcname
+
" failed: "
+
err.Error())
}
var (
user32, _
=
syscall.LoadLibrary(
"user32.dll"
)
messageBox, _
=
syscall.GetProcAddress(user32,
"MessageBoxW"
)
)
func IntPtr(n
int
) uintptr {
return
uintptr(n)
}
func StrPtr(s string) uintptr {
return
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
}
func MessageBox(caption, text string, style uintptr)
int
{
ret, _, callErr :
=
syscall.SyscallN(messageBox,
4
,
0
, StrPtr(text), StrPtr(caption), style,
0
,
0
,
0
,
0
)
if
callErr !
=
0
{
abort(
"Call MessageBox"
, callErr)
}
return
int
(ret)
}
func main() {
defer syscall.FreeLibrary(user32)
/
/
调用 MessageBox 函数
num :
=
MessageBox(
"Done Title"
,
"This test is Done."
, MB_OK)
fmt.Printf(
"Return value before MessageBox invocation: %d\n"
, num)
time.Sleep(
3
*
time.Second)
}