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开发者_Python百科 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?
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?
精彩评论