Results 1 to 2 of 2

Thread: Memory Mapped Files

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    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);
                }
            }

  2. #2
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    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.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width