package main
import (
"fmt"
"syscall"
"unsafe"
)
func main() {
ShareMemName, _ := syscall.BytePtrFromString("ShareMemory") //ShareMemory共享内存名称.
var sharememname = uintptr(unsafe.Pointer(ShareMemName))
kernet32 := syscall.NewLazyDLL("kernel32.dll")
OpenFileMapping := kernet32.NewProc("OpenFileMappingA")
ptr, _, err := OpenFileMapping.Call(0x0004, 0, sharememname) //0x0004 表示只读
if err.Error() != "The operation completed successfully." {
fmt.Println("This:", err)
return
}
ptr, err = syscall.MapViewOfFile(syscall.Handle(ptr), 0x0004, 0, 0, 0)
if err != nil {
fmt.Println("There:", err)
return
}
buf := *(*[60]byte)(unsafe.Pointer(ptr)) //我共享内存里面使用的是字符串所以这里映射成[:]byte array
fmt.Println(string(buf[:]))
err = syscall.UnmapViewOfFile(ptr)
fmt.Println(err)
}
有疑问加站长微信联系(非本文作者)