c# Memory sharing #1

메모리맵을 사용하기 위한 클래스들을 만듭니다.
제 프로젝트에서 따온거리 네임스페이스가 프로젝트 네임스페이스입니다..
자기에게 맞는걸로 바꾸시면 되겠죠.

sharedMemoryController.cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Runtime.InteropServices;
 
namespace SMC_Mapper
{
    class sharedMemoryController
    {
        [StructLayout(LayoutKind.Sequential)]
        internal class SecurityAttributes
        {
            public SecurityAttributes(object securityDescriptor)
            {
                this.lpSecurityDescriptor = securityDescriptor;
            }
 
            uint nLegnth = 12;
            object lpSecurityDescriptor;
            [MarshalAs(UnmanagedType.VariantBool)]
            bool bInheritHandle = true;
        }
 
        [DllImport("Kernel32.dll", EntryPoint = "CreateFileMapping", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern IntPtr CreateFileMapping(uint hFile, SecurityAttributes lpAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
 
        [DllImport("Kernel32.dll", EntryPoint = "OpenFileMapping", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern IntPtr OpenFileMapping(uint dwDesiredAccess, bool bInheritHandle, string lpName);
 
        [DllImport("Kernel32.dll", EntryPoint = "MapViewOfFile", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);
 
        [DllImport("Kernel32.dll", EntryPoint = "UnmapViewOfFile", SetLastError = true, CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.VariantBool)]
        internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
 
        [DllImport("kernel32", EntryPoint = "CloseHandle", SetLastError = true, CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.VariantBool)]
        internal static extern bool CloseHandle(uint hHandle);
 
        [DllImport("kernel32", EntryPoint = "GetLastError", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern uint GetLastError();
    }
}
 

저같은 경우는 메모리맵을 여러개를 열어야 해서 xml 파일로 메모리맵에 대한 정보들을 가지고 프로그램 시작시 불러와서 맵을 생성하는 방식이었습니다.

Mapper.cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Xml;
 
namespace SMC_Mapper
{
    public class Mapper
    {
        public Dictionary<string, Map> m_MapDic = new Dictionary<string, Map>();        
 
        public class Map
        {
            public int isReadOnly = 0;
 
            public enum FileAccess : int
            {
                ReadOnly = 2,
                ReadWrite = 4
            }
 
            private MemoryMappedFile file;
            private MemoryMappedFileView view;
 
            public void OpenMem(int size, string mapName)
            {                
                {
                    file = MemoryMappedFile.CreateFile(mapName, MemoryMappedFile.FileAccess.ReadWrite, size);
                    view = file.CreateView(0, size, MemoryMappedFileView.ViewAccess.ReadWrite);
                }                
            }
 
            public byte[] Read(byte[] rb)
            {
                view.ReadBytes(rb);
 
                return rb;
            }
 
            public void Write(byte[] rb)
            {
                view.WriteBytes(rb);
            }
        }
 
        public Map m_CummunicationMap;
 
        public Mapper()
        {
            ReadXML();
 
            
        }
 
        public void OpenCMap()
        {
            m_CummunicationMap = new Map();
 
            m_CummunicationMap.OpenMem(2, "CMAP");
        }
 
        private void ReadXML()
        {
            string url = @"Config\map.xml";
 
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(url);
 
                XmlElement keyList = xml.DocumentElement;
                XmlNodeList nodeList = xml.SelectNodes("/DATA/Packet");
 
                foreach (XmlNode node in nodeList)
                {
                    Map m_map = new Map();
 
                    string mapName = node.Attributes["Name"].Value;
                    XmlNodeList nodeChild = node.ChildNodes;
                    
                    XmlElement eChild = (XmlElement)nodeChild[0];
                    string length = eChild.InnerText;
 
                    eChild = (XmlElement)nodeChild[1];
                    string ro = eChild.InnerText;
                    m_map.isReadOnly = int.Parse(ro);
                    m_map.OpenMem(int.Parse(length), mapName);
 
                    m_MapDic.Add(mapName, m_map);
                }
            }
            catch (System.Exception ex)
            {
                
            }
              
        }     
    }
}
 

댓글

이 블로그의 인기 게시물

c# GDI+ 꺼져!! BufferedGraphics

c# 에서 C++ dll 사용하기

c# string 에서 mfc의 LPCTSTR간의 변환