title: How do I pass and retrieve memory stream from my Application to/from DLL?
body: <p>Suppose I have a <code>TMemoryStream</code> I need to pass to my DLL and get back <code>TMemoryStream</code> (Bitmap stream) from the DLL.</p>
  
  <p>I was thinking my DLL would have:</p>
  
  <pre><code>procedure Process(
    InBuff: Pointer; 
    InBuffSize: Integer; 
    var OutBuff: Pointer; 
    var OutBuffSize: Integer
  ); stdcall;
  </code></pre>
  
  <p>The <code>InBuff</code> is easy (I think). I pass <code>TMemoryStream.Memory</code> and <code>TMemoryStream.Size</code>.</p>
  
  <p>Question is how do I allocate the <code>OutBuff</code> in the DLL, and the caller application can convert it back to <code>TMemoryStream</code> and later free that memory (by the caller application)?</p>
  
  <p>The caller will use dynamic <code>LoadLibrary</code>/<code>FreeLibrary</code> each DLL call.</p>
  
  <p>I would very much like a sample code. Hope I'm not being too rude.</p>
  
  <p>Note 1: The caller application dose not know the output size, and assume it can not specify a MAX buff size.</p>
  
  <p>Note 2: I am not sure about my DLL signature. Please forgive me if I did it wrong. I am Looking for a pattern that will work well (maybe not only for Delphi but for C++/C# Caller as well = Bonus for me)</p>