-
Memory Mapped Files
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 is
Code:
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);
}
}
-
Re: Memory Mapped Files
Hi Royal,
Your exception is:
The specified Type must be a struct containing no references.
Parameter name: type
If the error is refering to the types of your variables declared in your structure than your string variable is the cause.
(public string s)
A string is a common reference type. It looks like you are only able to use value types within your structure in this particular instance.
If the error is not refering to the variable types in your structure then I'm not sure.