I'm having a hard time writing a string value to a memory mapped file, I've tried char arrays, byte arrays, and I just can't figure out how to get it to work. The exception I get for all these types isCode:The specified Type must be a struct containing no references. Parameter name: type
My code is:
Code:public struct Test { public int a; public int b; public int c; public int d; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 40)] public string s; public void Add(int i) { a += i; b += i; c += i; d += i; } public void Append(string str) { s += str; } } private void button1_Click(object sender, EventArgs e) { int TestSize = Marshal.SizeOf(typeof(Test)); MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("Test", TestSize, MemoryMappedFileAccess.ReadWriteExecute); MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(0, TestSize); Test _test; for (long i = 0; i < TestSize; i += TestSize) { accessor.Read(i, out _test); _test.Add(100); _test.Append("hello"); accessor.Write(i, ref _test); } }




Reply With Quote