1

I am trying to create an array of Integers into a memory mapped file and read the file in C++, but I won't ask the c++ part here. MakeMem() has {"Not enough space available in the buffer."}

Private Sub MakeMem()
Dim integer2 = New Integer(31) {}
For j As Integer = 0 To integer2.Length - 1
        integer2(j) = 35 - j
Next
MemoryMappedFile.CreateOrOpen("0MemF", integer2.Length + 1).CreateViewAccessor(0, integer2.Length).WriteArray(Of Integer)(0, integer2, 0, integer2.Length)
End Sub

Testing first in VB, Read Array From Memory

Private Sub ReadMem()
    Try
        Dim integer3 = New Integer(31) {}

        MemoryMappedFile.OpenExisting("0MemF").CreateViewAccessor(0, integer3.Length + 1).ReadArray(Of Integer)(0, integer3, 0, integer3.Length)

                TextBox1.Text = ""
                For i As Integer = 0 To integer3.Length - 1
                    TextBox1.AppendText(CStr(integer3(i)) + " ")
                Next


            End Using
        End Using
    Catch noFile As FileNotFoundException

        TextBox1.Text = "Mem-map not found"
    Catch Ex As Exception

    End Try
End Sub
jester
  • 238
  • 5
  • 13
  • So is the exception thrown by `CreateOrOpen`, `CreateViewAccessor` or `WriteArray`? – jmcilhinney Sep 25 '17 at 06:57
  • Exception is thrown on WriteArray(Of Integer)(0, integer2, 0, integer2.Length) – jester Sep 25 '17 at 07:00
  • The documentation for `CreateOrOpen` says that the second parameter is the maximum size **in bytes** to allocate to the file. Given that a VB.NET `Integer` is four bytes in size, you'd need to use `integer2.Length * 4` rather than just `integer2.Length`. – jmcilhinney Sep 25 '17 at 07:03
  • Thank you, and works: CreateOrOpen("0MemF", integer2.Length * 4 + 1 * 4).CreateViewAccessor(0, integer2.Length * 4) – jester Sep 25 '17 at 07:08
  • showing the stacktrace might also prove a little helpful. – Simon Price Sep 25 '17 at 07:10

0 Answers0