Interop/Structs/Arrays Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kobrakai
    New Member
    • Aug 2008
    • 1

    Interop/Structs/Arrays Problem

    Hi, I have an IntPtr that points to an unmanaged array. This unmanaged array contains struct pointers. I am able to retrieve the first struct using Marshal.PtrToSt ructure() but I have been unable to retrieve the other structs in the array.

    I have been trying to increment the pointer to get the location of the next struct in memory using: ThePointer = (IntPtr)((int)T hePointer + Marshal.SizeOf( TheStruct));

    But as soon as I try calling Marshal.PtrToSt ructure() again to get the next struct, I always get an "attempted to read or write to protected memory" exception.

    Is there a way that I can do this?
    Thanks in advance.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by Kobrakai
    Hi, I have an IntPtr that points to an unmanaged array. This unmanaged array contains struct pointers. I am able to retrieve the first struct using Marshal.PtrToSt ructure() but I have been unable to retrieve the other structs in the array.

    I have been trying to increment the pointer to get the location of the next struct in memory using: ThePointer = (IntPtr)((int)T hePointer + Marshal.SizeOf( TheStruct));

    But as soon as I try calling Marshal.PtrToSt ructure() again to get the next struct, I always get an "attempted to read or write to protected memory" exception.

    Is there a way that I can do this?
    Thanks in advance.
    Why don't you just get the pointer to the next Struct from your array and then use the Marshal.PtrToSt ructure() again?

    Your Structs may not be stored sequentially in memory...they could be stored at different locations in memory. This means that when you increment your pointer to the end of your Struct you are making a huge assumption that the next Struct will be stored there. Apparently this is not the case because you are getting the "attempt to read or write to protect memory" exception.

    -Frinny

    Comment

    Working...