How can I delete memory allocated by a legacy C++ DLL
Hi, I am having a problem deleting memory allocated from a legacy C++ DLL.
The DLL uses PostMessage to send data to a parent App, and expects the parent app to delete the memory when it has finished with it.
I have created a new C++ DLL to interface the calls from my new .NET application as one of the functions in the legacy DLL required a CWnd* parameter.
I am trapping the message posted by the legacy DLL by overriding the WndProc of my .NET form, and I am collecting the data from the memory pointer in the posted message with Marshal.PtrToStructure(m.LParam, new myStruct().GetType()).
I then tried calling a function in my new C++ DLL, passing m.LParam as the pointer to the memory I need to delete, but I get an error - _CrtIsValidHeapPointer(pUserData) thrown up from the Visual C++ debugger.
I have also tried calling Marshal.FreeHGlobal(m.LParam) from my .NET form, but this doesn't work either.
Can anyone please help me?

Answers
Know the answer? Post it. Somebody with the same question will find it here.