<div dir="ltr"><div class="gmail_extra">Or always allocate and free the buffer in the calling program and load the contents into the buffer in the dll. This way you never have to worry about allocating in one place and freeing in the other. Windows API does stuff like this a lot.</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">In the DLL:</div><div class="gmail_extra"><br></div><div class="gmail_extra">Procedure GetSomeString(StringPointer: Pointer, Var StringLen: Integer);</div><div class="gmail_extra">
Begin<br></div><div class="gmail_extra"> StringLen = CalculateSizeOfString</div><div class="gmail_extra"> if StringPointer != nil then</div><div class="gmail_extra"> // copy string to StringPointer<br></div><div class="gmail_extra">
End;</div><div class="gmail_extra"><br></div><div class="gmail_extra">In your App:</div><div class="gmail_extra"><br></div><div class="gmail_extra">Var StringLen: Integer;</div><div class="gmail_extra">AllocedMem : Pointer;</div>
<div class="gmail_extra">. . .</div><div class="gmail_extra">// Find out how much memory needed</div><div class="gmail_extra">GetSomeString(nil, &StringLen);</div><div class="gmail_extra">GetMem(AllocedMem, StringLen);</div>
<div class="gmail_extra">GetSomeString(AllocedMem, &StringLen);</div><div class="gmail_extra">// use the string for whatever you need</div><div class="gmail_extra">. . .</div><div class="gmail_extra">FreeMem(AllocedMem);<br>
</div><div class="gmail_extra"><br></div><div class="gmail_extra">Very bad psuedo code there, but it should give you the idea. <br></div><div class="gmail_extra"><br></div><div class="gmail_extra">To summarize, your app needs to:</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">1. Call once with nil to get how much memory needed</div><div class="gmail_extra">2. Allocate the memory</div><div class="gmail_extra">3. Call again with pointer to memory</div>
<div class="gmail_extra">4. Use the allocated memory</div><div class="gmail_extra">5. Free the allocated memory</div><div class="gmail_extra"><br></div><div class="gmail_extra">And your dll function needs to:</div><div class="gmail_extra">
<br></div><div class="gmail_extra">1. Check pointer for nil</div><div class="gmail_extra">2. If so, only set the length parameter to the size of memory needed and exit</div><div class="gmail_extra">3. If not, copy data to pointer as well</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">Jeff.</div></div>