Golang1.7使用kernel32.dll 读取共享内存,代码小示例

前端之家收集整理的这篇文章主要介绍了Golang1.7使用kernel32.dll 读取共享内存,代码小示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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)
    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)
}
原文链接:https://www.f2er.com/go/189518.html

猜你在找的Go相关文章