質問する質問する
 

質問C# WindowsMobileでのメモリの読み書き

  • 2009年9月17日 8:34ホースフェイス ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     

    お世話になります。

    WindowsMobileでC#を使ってメモリ上にデータを展開してそれを読み書きできればと思っており、いろいろ試行錯誤しましたが、書き込みと読み込みができません。
    どなたかおわかりになる方がいらっしゃればぜひご教授いただきたいと思います。
    開発環境は、.NETCompactFreamWork3.5
    エミュレータはwindowsmobile6.1.4を使用しております。
    開発言語はC#です。
    現在の状況としまして
    デバッグで実行したところCreateFileMapping
    とMapViewOfFile
    ではハンドルに値が入ってるように見えるのですが、WriteFileでMapViewOfFileで得たハンドルを使用して、文字列を書き込もうとしたのですが、
    WriteFileではFalse、ReadFileでもFalseが返ってきて、readFileに指定した文字列の変数にもなにも入ってこない状況です。
    いずれもWinAPIを使用して、実施しようとしております。

    以下に問題となるソースを転機しますのでよろしくお願いいたします。


    using System;
    using System.Windows.Forms;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;


    namespace AddressUpdator
    {

        /// <summary>
        ///
        /// </summary>
        class SharedMemory
        {
            enum FileProtection : uint
            {
                ReadOnly =  2,
                ReadWrite = 4
            }
            enum FileRights : uint
            {
                Read  = 4,
                Write = 2,
                ReadWrite = Read+Write
            }
            static readonly UIntPtr NoFileHandle = new UIntPtr(0xFFFFFFFF);

            [DllImport("coredll.dll", SetLastError = true)]
            static extern IntPtr CreateFileMapping
                                 (
                                  UIntPtr hFile,
                                  IntPtr lpFileMappingAttributes,
                                  FileProtection fl,
                                  uint dwMaximumSizeHigh,
                                  uint dwMaximumSizeLow,
                                  string lpName
                                 );
    using System;
    using System.Windows.Forms;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;


    namespace AddressUpdator
    {

        /// <summary>
        /// メモリマッピング時に使用する値です
        /// </summary>
        class SharedMemory
        {
            enum FileProtection : uint
            {
                ReadOnly =  2,
                ReadWrite = 4
            }
            enum FileRights : uint
            {
                Read  = 4,
                Write = 2,
                ReadWrite = Read+Write
            }
            static readonly UIntPtr NoFileHandle = new UIntPtr(0xFFFFFFFF);
            public IntPtr mhFileMap = IntPtr.Zero;
            public IntPtr pmem = IntPtr.Zero;
            const UInt32 STANDARD_RIGHTS_REQUIRED = 0x000F0000;
            const UInt32 SECTION_QUERY = 0x0001;
            const UInt32 SECTION_MAP_WRITE = 0x0002;
            const UInt32 SECTION_MAP_READ = 0x0004;
            const UInt32 SECTION_MAP_EXECUTE = 0x0008;
            const UInt32 SECTION_EXTEND_SIZE = 0x0010;
            const UInt32 SECTION_ALL_ACCESS = (
                                                STANDARD_RIGHTS_REQUIRED |
                                                SECTION_QUERY |
                                                SECTION_MAP_WRITE |
                                                SECTION_MAP_READ |
                                                SECTION_MAP_EXECUTE |
                                                SECTION_EXTEND_SIZE
                                                );
            const UInt32 FILE_MAP_ALL_ACCESS = SECTION_ALL_ACCESS;

    /// <summary>
    /// 使用するWINAPIの定義です
    /// </summary>
    /// <param name="hFile"></param>
    /// <param name="lpFileMappingAttributes"></param>
    /// <param name="fl"></param>
    /// <param name="dwMaximumSizeHigh"></param>
    /// <param name="dwMaximumSizeLow"></param>
    /// <param name="lpName"></param>
    /// <returns></returns>
            [DllImport("coredll.dll", SetLastError = true)]
            static extern IntPtr CreateFileMapping
                                 (
                                  UIntPtr hFile,
                                  IntPtr lpFileMappingAttributes,
                                  FileProtection fl,
                                  uint dwMaximumSizeHigh,
                                  uint dwMaximumSizeLow,
                                  string lpName
                                 );
            [DllImport("coredll.dll", SetLastError = true)]
            static extern IntPtr MapViewOfFile
                                (
                                    IntPtr hFile,
                                    uint dwDesiredAccess,
                                    uint dwFileOffsetHigh,
                                    uint dwFileOffsetLow,
                                    int dwNumberOfBytesToMap
                                );
            [DllImport("coredll.dll", SetLastError = true)]
            static extern bool WriteFile
                               (
                                    IntPtr hFile,
                                    string str,
                                    uint   lstlen,
                                    uint   size,
                                    OVERLAPPED over
                               );
            [DllImport("coredll.dll", SetLastError = true)]
            static extern bool ReadFile
                                (
                                    IntPtr hFile,
                                    string str,
                                    uint   lstlen,
                                    uint   size,
                                    OVERLAPPED over
                                );
            [DllImport("coredll.dll", SetLastError = true)]
            static extern void UnmapViewOfFile
                                (
                                    IntPtr hFile
                                );
            [DllImport("coredll.dll", SetLastError = true)]
            static extern void CloseHandle
                                (
                                    IntPtr hFile
                                );
            [StructLayout(LayoutKind.Sequential)]
            struct OVERLAPPED
            {
             uint     Internal;
             uint     InternalHigh;
             uint     Offset;
             uint     OffsetHigh;
             IntPtr   hEvent;
            }

     

            static void Main(string[] args)
            {
                ///
                ///メモリマッピング使用する領域の確保
                ///
               
               IntPtr mhFileMap = CreateFileMapping
                                    (
                                        (UIntPtr)NoFileHandle,
                                        (IntPtr)0,
                                        FileProtection.ReadWrite,
                                        0,
                                        512,
                                        "MyFile"
                                    );
               if (mhFileMap == null)
               {
                   MessageBox.Show("CreateFileMappingMiss(return'null'");
               }
               else
               {
                   ///
                   ///他プロセスからアクセスできるようにする
                   ///
                   IntPtr pmem = MapViewOfFile
                                  (
                                     mhFileMap,
                                     SECTION_ALL_ACCESS,
                                     0,
                                     0,
                                     512
                                     );
                   if (pmem == null)
                   {
                       MessageBox.Show("MapViewOfFileMiss(return'null')");
                   }
                   else
                   {
                       ///確保した領域に書き込みます
                       ///
                       string teststr = "testMessage";
                       OVERLAPPED over = new OVERLAPPED();
                       bool wret = true;
                       wret=WriteFile
                            (
                              pmem,
                              teststr,
                              256,
                              256,
                              over
                             );
                       if (wret == false)
                       {
                           MessageBox.Show("WriteFileMiss!");
                       }
                      //ちゃんと書き込めたか確認するために確保したメモリ領域を読み込みます。
                       string teststrR = "";
                       bool ret=true;
                       ret = ReadFile
                           (
                            pmem,
                            teststrR,
                            256,
                            256,
                            over
                           );
                       if (ret == false)
                       {
                           MessageBox.Show("ReadFileMiss!");
                       }
                       MessageBox.Show("memory="+teststrR);
                   }
                   //領域を解放します
                   UnmapViewOfFile(pmem);
               }
               CloseHandle(mhFileMap);
            }
        }

    }