views:

85

answers:

1

We are using visual studio 2008-My requirement is to allocate some memory, store data into that allocated memory and pass the memory address to a DLL written in C. But when try to pass this memory address to a function in that DLL my application crashes and shows the message "The memory could not be written".

''//Memory allocation Code
Dim tmpPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Array.GetValue(Array.GetLowerBound(0), Array.GetLowerBound(1))))
''//Copying data to memory
Marshal.StructureToPtr(Array.GetValue(Array.GetLowerBound(0), Array.GetLowerBound(1)), tmpPtr, True)
''//Trying to pass to 'c' dll
StoreStack(tmpPtr)

I'm getting the error at the last line. Can anyone help?

A: 

It's pretty hard to guess what is going wrong without knowing what managed data structure you are passing to StoreStack and what data structure it expects. My best guess is that the pointer you are passing to StoreStack is smaller than it expects.

Have you checked the value returned by Marshal.SizeOf(Array.GetValue(Array.GetLowerBound(0), Array.GetLowerBound(1))) to make sure that it returns the correct size?

shf301
I also guess there might be problem while allocating memory...may be coz of not getting proper memory size...Can you please tell, How to get the total memory size allocated by two dimentional array.--Thanks
Avinash