2011-08-22 63 views
1

我在分片存儲器中有TXT文件。代碼在最後。我一直試圖將它從內存中取出並寫入C:\驅動器中的文件。共享內存,寫入文件

但我得到一個錯誤:

Type 'SharedMemSaveToFile.SharedMemSaver+Data' cannot be marshaled as an 
unmanaged structure; no meaningful size or offset can be computed. 

如果我改變代碼寫的CMD,它的工作原理的記憶,所以我知道內存是存在的。我用這些寫TXT也試過:

System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\file.txt"); 
file.WriteLine(d); 

和:

using (StreamWriter outfile = new StreamWriter(d + @"C:\\file.txt")) 
{ 
    outfile.Write(sb.ToString()); 
} 

和:

StreamWriter sw = new StreamWriter("file.txt"); 
    sw.Write(d); 
    sw.Close(); 

謝謝!

public class Data 
    { 
     static void Main(string[] args) 
     { 
      SharedMemSaver sf = new SharedMemSaver(); 
      sf.OpenView(); 
      String d = sf.GetData(); 
      System.IO.File.WriteAllText(@"C:\file.txt", d); 

     } 
    } 

    #region Win32 API stuff 
    public const int FILE_MAP_READ = 0x0004; 

    [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)] 
    internal static extern IntPtr OpenFileMapping(int dwDesiredAccess, 
     bool bInheritHandle, StringBuilder lpName); 

    [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)] 
    internal static extern IntPtr MapViewOfFile(IntPtr hFileMapping, 
     int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, 
     int dwNumberOfBytesToMap); 

    [DllImport("Kernel32.dll")] 
    internal static extern bool UnmapViewOfFile(IntPtr map); 

    [DllImport("kernel32.dll")] 
    internal static extern bool CloseHandle(IntPtr hObject); 
    #endregion 

    private bool fileOpen = false; 
    private IntPtr map; 
    private IntPtr handle; 

    ~SharedMemSaver() 
    { 
     CloseView(); 
    } 

    public bool OpenView() 
    { 
     if (!fileOpen) 
     { 
      StringBuilder sharedMemFile = new StringBuilder("Mem_Values"); 
      handle = OpenFileMapping(FILE_MAP_READ, false, sharedMemFile); 
      if (handle == IntPtr.Zero) 
      { 
       throw new Exception("Unable to open file mapping."); 
      } 
      map = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, Marshal.SizeOf((Type)typeof(Data))); 
      if (map == IntPtr.Zero) 
      { 
       throw new Exception("Unable to read shared memory."); 
      } 
      fileOpen = true; 
     } 
     return fileOpen; 
    } 

    public void CloseView() 
    { 
     if (fileOpen) 
     { 
      UnmapViewOfFile(map); 
      CloseHandle(handle); 
     } 
    } 

    public String GetData() 
    { 
     if (fileOpen) 
     { 
      String data = (String)Marshal.PtrToStringAuto(map); 
      return data; 
     } 
     else 
     { 
      return null; 
     } 
    } 
} 

}

回答

4

我會強烈建議使用內置MemoryMappedFile類(新的.NET 4)。

+0

像處理過程b從這裏:[鏈接](http://msdn.microsoft.com/en-us/library/dd267552.aspx) – Csharpz

+0

我知道,鏈接,但我不你明白你的意思嗎? – Yahia

+0

對不起,我的意思是一個很好的例子,以我的程序爲基礎?如果我要走那條路? – Csharpz

1

請參閱Yahia對解決方案的回答。

但是,試圖修復您的代碼,錯誤消息說,所有:
什麼是你想獲得與Marshal.SizeOf((Type)typeof(Data))
它沒有大小,因爲它不包含數據。 望着MSDN doc。 MapViewOfFile的的最後一個參數,"If this parameter is 0 (zero), the mapping extends from the specified offset to the end of the file mapping."

+0

它的一個txt字符串。 – Csharpz

+0

@Csharpz:這只是公共類Data {static void Main(string [] args){...}}',所以這個類的對象甚至不會保存任何數據。你有沒有嘗試用'0'代替'Marshal.SizeOf'? – ordag

+0

沒問題,它創建了C:\上的文件,但它不是我所期望的。應該是一個XML文件,但它是「敢㹬䥎ㅃ」s – Csharpz