2018-03-30

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace TestForShm
{
    public class Program
    {
        [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        struct TShareMem
        {
            public int i;
            [MarshalAs(UnmanagedType.ByValArray,SizeConst =256)]
            public char[] data;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
            public float[] a;
        }
        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr OpenFileMapping(
            int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName);
        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr MapViewOfFile(
            IntPtr hFileMapping, uint dwDesiredAccess, uint FileOffsetHigh, uint FileOffsetLow, uint dwNumberOfBytesYoMap);
        [DllImport("kernel32.dll")]
        public static extern void CopyMemory(byte[] Destination, IntPtr Source, int Length);

        public static unsafe void ByteCopy(byte[] dst,IntPtr src)
        {
            fixed(byte * pDst = dst)
            {
                byte* pdst = pDst;
                byte* psrc = (byte*)src;
                while ((*pdst++ = *psrc++) != '\n')
                    ;
            }
        }
        public static object ByteToStruct(byte []bytes,Type type)
        {
            int size = Marshal.SizeOf(type);
            IntPtr structPtr = Marshal.AllocHGlobal(size);
            Marshal.Copy(bytes, 0, structPtr, size);
            object obj = Marshal.PtrToStructure(structPtr, type);
            Marshal.FreeHGlobal(structPtr);
            return obj;
        }
        static unsafe void Main(string[] args)
        {
            while (true)
            {
                TShareMem recv = new TShareMem();
                Console.ReadLine();
                IntPtr hMap = OpenFileMapping(0x0002, false, "NewMap");
                IntPtr hAddress = MapViewOfFile(hMap, 0x0002, 0, 0, 0);
                int size = Marshal.SizeOf(recv);
                byte[] byteStr = new byte[size];
                CopyMemory(byteStr, hAddress, size);
                recv =(TShareMem) ByteToStruct(byteStr, typeof(TShareMem));
                Console.WriteLine(recv.a[6]);
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (万尚学习会)打卡第46天 姓名:何炳 部门:业务部 组别:努力三组 【知~学习】 诵读《活法》第三章 日本人为什...
    CrisWellin阅读 155评论 0 0
  • 我希望,年迈时能够住在一个小农场,有马有狗,养鹰种茶花。 到时候,老朋友相濡以沫住在一起,读书种地,酿酒喝普洱茶。...
    大才子呀阅读 144评论 0 0
  • 2017 新的一年, 祝大家新年快乐! 很开心在简书和大家相识, 遇见这么多志同道合的人, 也很开心我的设计和小画...
    你好_我是周小v阅读 1,412评论 6 11
  • 津城晨曦 一片雨露拨开雾霾的影子 百合慵懒地伸了懒腰 刺眼的光不合时宜地照在脸上 梦中你的样子还未消散 你说静谧的...
    懒人岳鑫阅读 218评论 0 0
  • 阳春的午后,慵懒的我如果不喝上一杯咖啡,简直无法思考和工作。 于是打开瓶盖,取出三勺咖啡粉,不加糖...
    敬亭山峰阅读 1,469评论 5 3