当前位置:首页 > 开发教程 > IT博文 > 软件工程 >

c# 内存共享、内存映射文件

时间:2013-09-27 10:45 来源:互联网 作者:源码搜藏 收藏

[c-sharp] view plaincopy using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; using System.Data.SqlClient; namespace WinApp { public class Sequence { [DllImport( "kernel3
[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.InteropServices;  
  5. using System.IO;  
  6. using System.Data.SqlClient;  
  7.   
  8. namespace WinApp  
  9. {  
  10.     public class Sequence  
  11.     {  
  12.         [DllImport("kernel32.dll")]  
  13.         public static extern IntPtr CreateFileMapping(IntPtr hFile,IntPtr lpFileMappingAttributes, uint flProtect,uint dwMaximumSizeHigh,  
  14.             uint dwMaximumSizeLow, string lpName);  
  15.   
  16.         [DllImport("kernel32.dll")]  
  17.         public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint  
  18.             dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, IntPtr dwNumberOfBytesToMap);  
  19.   
  20.         [DllImport("kernel32.dll")]  
  21.         public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);  
  22.   
  23.         [DllImport("kernel32.dll")]  
  24.         public static extern bool CloseHandle(IntPtr hObject);  
  25.   
  26.         [DllImport("kernel32.dll")]  
  27.         public static extern IntPtr CreateFile(string lpFileName,int dwDesiredAccess, FileShare dwShareMode, IntPtr securityAttrs,  
  28.             FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);  
  29.   
  30.         [DllImport("kernel32.dll")]  
  31.         public static extern uint GetFileSize(IntPtr hFile, IntPtr lpFileSizeHigh);  
  32.   
  33.         public const int GENERIC_READ = -2147483648; //0x80000000  
  34.         public const int GENERIC_WRITE = 0x40000000;  
  35.         public const int GENERIC_EXECUTE = 0x20000000;  
  36.         public const int GENERIC_ALL = 0x10000000;  
  37.         public const int FILE_ATTRIBUTE_NORMAL = 0x80;  
  38.         public const int FILE_FLAG_SEQUENTIAL_SCAN = 0x8000000;  
  39.         public const int INVALID_HANDLE_VALUE = -1;  
  40.   
  41.         public const int PAGE_NOACCESS = 1;  
  42.         public const int PAGE_READONLY = 2;  
  43.         public const int PAGE_READWRITE = 4;  
  44.   
  45.         public const int FILE_MAP_COPY = 1;  
  46.         public const int FILE_MAP_WRITE = 2;  
  47.         public const int FILE_MAP_READ = 4;  
  48.   
  49.         public static string MappingFileRead()  
  50.         {  
  51.             string result = "";  
  52.             try  
  53.             {  
  54.                 IntPtr vFileHandle = CreateFile(@"c:/test.txt", GENERIC_READ | GENERIC_WRITE, FileShare.Read | FileShare.Write,IntPtr.Zero, FileMode.Open,  
  55.                 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, IntPtr.Zero);  
  56.                 if (INVALID_HANDLE_VALUE != (int)vFileHandle)  
  57.                 {  
  58.                     IntPtr vMappingHandle = CreateFileMapping(vFileHandle, IntPtr.Zero, PAGE_READWRITE, 0, 0, "~MappingTemp");  
  59.                     if (vMappingHandle != IntPtr.Zero)  
  60.                     {  
  61.                         IntPtr vHead = MapViewOfFile(vMappingHandle, FILE_MAP_COPY | FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, IntPtr.Zero);  
  62.                         if (vHead != IntPtr.Zero)  
  63.                         {                            
  64.                             uint vSize = GetFileSize(vFileHandle, IntPtr.Zero);  
  65.                             byte[] res = new byte[vSize];  
  66.                             Marshal.Copy(vHead, res, 0, (int)vSize);  
  67.                             result = ASCIIEncoding.Default.GetString(res);  
  68.                             string[] _str = result.Split('|');  
  69.                             result = _str[0];  
  70.                             /* 
  71.                             //*************************************** 
  72.                             //_IntPtr = Marshal.ReadIntPtr(vHead);                              
  73.                             byte[] contentbyte1 = new byte[vSize]; 
  74.                             for (int i = 0; i < vSize; i++) 
  75.                             { 
  76.                                 byte vTemp = Marshal.ReadByte((IntPtr)((int)vHead + i)); 
  77.                                 contentbyte1[i] = vTemp;                                
  78.                             } 
  79.                             Console.WriteLine(ASCIIEncoding.Default.GetString(contentbyte1));//全部值 
  80.                             //*************************************** 
  81.                             byte[] contentbyte = new byte[vSize]; 
  82.                             for (int i = 0; i < vSize; i++) 
  83.                             { 
  84.                                 byte vTemp = Marshal.ReadByte((IntPtr)((int)vHead + i)); 
  85.                                 contentbyte[i] = vTemp; 
  86.                                 if (vTemp == '|')//设定标识结束符合 
  87.                                 { 
  88.                                     break; 
  89.                                 } 
  90.                             }                             
  91.                             Console.WriteLine(ASCIIEncoding.Default.GetString(contentbyte));//要取的值 
  92.                             result = ASCIIEncoding.Default.GetString(contentbyte); 
  93.                             //*************************************** 
  94.                             */  
  95.                             UnmapViewOfFile(vHead);  
  96.                         }  
  97.                         CloseHandle(vMappingHandle);  
  98.                     }  
  99.                     CloseHandle(vFileHandle);  
  100.                 }  
  101.   
  102.             }  
  103.             catch  
  104.             { }  
  105.             return result;  
  106.         }  
  107.         public static IntPtr InvalidHandleValue = new IntPtr(-1);  
  108.         public static void MappingFileWrite(string val)  
  109.         {  
  110.             try  
  111.             {                  
  112.                 IntPtr vFileHandle = CreateFile(@"c:/test.txt", GENERIC_READ | GENERIC_WRITE, FileShare.Read | FileShare.Write, IntPtr.Zero, FileMode.Open,  
  113.                 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, IntPtr.Zero);  
  114.                 if (INVALID_HANDLE_VALUE != (int)vFileHandle)  
  115.                 {  
  116.                     IntPtr vMappingHandle = CreateFileMapping(vFileHandle, IntPtr.Zero, PAGE_READWRITE, 0, (UInt32)(val.Length + 1), "~MappingTemp");  
  117.                     if (vMappingHandle != IntPtr.Zero)  
  118.                     {  
  119.                         IntPtr vHead = MapViewOfFile(vMappingHandle, FILE_MAP_COPY | FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, IntPtr.Zero);  
  120.                         if (vHead != IntPtr.Zero)  
  121.                         {                                 
  122.                             byte[] by = ASCIIEncoding.Default.GetBytes(val + "|"); //设定标识结束符合  |     
  123.                             Marshal.Copy(by, 0, vHead, by.Length);                              
  124.                             UnmapViewOfFile(vHead);  
  125.                         }  
  126.                         CloseHandle(vMappingHandle);  
  127.                     }  
  128.                     CloseHandle(vFileHandle);  
  129.                 }  
  130.   
  131.             }  
  132.             catch  
  133.             { }  
  134.         }  
  135.          
  136.         public static void MappingWrite1(string val)  
  137.         {  
  138.             try  
  139.             {  
  140.                 IntPtr vMappingHandle = CreateFileMapping(InvalidHandleValue, IntPtr.Zero, PAGE_READWRITE, 0, (UInt32)(val.Length + 1), "~MappingTemp");  
  141.                 if (vMappingHandle != IntPtr.Zero)  
  142.                 {  
  143.                     IntPtr vHead = MapViewOfFile(vMappingHandle, FILE_MAP_COPY | FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, IntPtr.Zero);  
  144.                     if (vHead != IntPtr.Zero)  
  145.                     {  
  146.                         byte[] by = ASCIIEncoding.Default.GetBytes(val + "|"); //设定标识结束符合  |    
  147.                         Marshal.Copy(by, 0, vHead, by.Length);  
  148.                         UnmapViewOfFile(vHead);  
  149.                     }  
  150.                     //CloseHandle(vMappingHandle);  
  151.                 }  
  152.   
  153.             }  
  154.             catch  
  155.             { }  
  156.         }  
  157.         public static string MappingRead1()  
  158.         {  
  159.             string result = "";  
  160.             try  
  161.             {  
  162.                 IntPtr vMappingHandle = CreateFileMapping(InvalidHandleValue, IntPtr.Zero, PAGE_READWRITE, 0, 7, "~MappingTemp");  
  163.                 if (vMappingHandle != IntPtr.Zero)  
  164.                 {  
  165.                     IntPtr vHead = MapViewOfFile(vMappingHandle, FILE_MAP_COPY | FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, IntPtr.Zero);  
  166.                     if (vHead != IntPtr.Zero)  
  167.                     {  
  168.                         byte[] bytData = new byte[7];  
  169.                         Marshal.Copy(vHead, bytData, 0, 7);  
  170.                         result = ASCIIEncoding.Default.GetString(bytData);  
  171.                         UnmapViewOfFile(vHead);  
  172.                     }  
  173.                     CloseHandle(vMappingHandle);  
  174.                 }  
  175.             }  
  176.             catch  
  177.             {  
  178.             }  
  179.             return result;  
  180.         }  
  181.     }     
  182. }  

软件工程阅读排行

最新文章