Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#

How to use a memory-mapped file with C# in WinCE

Rate me:
Please Sign up or sign in to vote.
4.62/5 (11 votes)
13 Dec 2013CPOL 43.4K   605   13  
How to use a memory-mapped file with C# in WinCE.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.WinCEAPI;
using System.Runtime.InteropServices;
using LPIGeneral;
using LPISerializeData;

namespace ShareB
{
    public partial class frmMain : Form
    {
        const string MapName = "OrzCloud";
        const int MapSize = 1024;
        IntPtr m_pMapFile;
        IntPtr m_pBuff;

        public frmMain()
        {
            InitializeComponent();
            m_pMapFile = IntPtr.Zero;
            m_pBuff = IntPtr.Zero;
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            int t_i4Error = 0;
            try
            {
                if (m_pMapFile == IntPtr.Zero)
                {
                    // open map file
                    //m_pMapFile = WinCE.CreateFileMapping(WinCEConst.INVALID_HANDLE_VALUE, null,
                    //    WinCEConst.PAGE_READWRITE, 0, 0, MapName);
                    m_pMapFile = WinCE.OpenFileMapping(WinCEConst.FILE_MAP_ALL_ACCESS, false, MapName);
                    // check error code
                    t_i4Error = Marshal.GetLastWin32Error();

                    // get map file
                    m_pBuff = WinCE.MapViewOfFile(m_pMapFile, WinCEConst.FILE_MAP_ALL_ACCESS, 0, 0, MapSize);
                }

                byte[] bytData = new byte[MapSize];
                // read data from map file
                Marshal.Copy(m_pBuff, bytData, 0, MapSize);
                // change byte array to string
                txtData.Text = Encoding.ASCII.GetString(bytData,0,MapSize).Trim();
            }
            catch (Exception ex)
            {
                t_i4Error = Marshal.GetLastWin32Error();
                MessageBox.Show(ex.Message);
            }
        }

        private void frmMain_Closing(object sender, CancelEventArgs e)
        {
            try
            {
                WinCE.UnmapViewOfFile(m_pBuff);
                WinCE.CloseHandle(m_pMapFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SIS
Taiwan Taiwan
CloudBox cross-platform framework. (iOS+ Android)
Github: cloudhsu
My APP:
1. Super Baby Pig (iOS+Android)
2. God Lotto (iOS+Android)
2. Ninja Darts (iOS)
3. Fight Bingo (iOS)

Comments and Discussions