我们在Delphi 2009之前有以下代码:
<span style="font: 10pt Courier New;"><span class="pas1-reservedword">function</span><span class="pas1-space"> </span><span class="pas1-identifier">MemoryStreamToString(M:</span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStream):</span><span class="pas1-space"> </span><span class="pas1-reservedword">String</span><span class="pas1-symbol">; </span><span class="pas1-reservedword">var </span><span class="pas1-space"> </span><span class="pas1-identifier">NewCapacity:</span><span class="pas1-space"> </span><span class="pas1-identifier">Longint; </span><span class="pas1-reservedword">begin </span><span class="pas1-space"> </span><span class="pas1-reservedword">if</span><span class="pas1-space"> </span><span class="pas1-symbol">(M.Size</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-number">0)</span><span class="pas1-space"> </span><span class="pas1-reservedword">or</span><span class="pas1-space"> </span><span class="pas1-symbol">(M.Memory</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-reservedword">nil</span><span class="pas1-symbol">)</span><span class="pas1-space"> </span><span class="pas1-reservedword">then </span><span class="pas1-space"> </span><span class="pas1-identifier">Result:=</span><span class="pas1-space"> </span><span class="pas1-string">'' </span><span class="pas1-space"> </span><span class="pas1-reservedword">else </span><span class="pas1-space"> </span><span class="pas1-reservedword">begin </span><span class="pas1-space"> </span><span class="pas1-reservedword">if</span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStreamProtected(M).Capacity</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-identifier">M.Size</span><span class="pas1-space"> </span><span class="pas1-reservedword">then </span><span class="pas1-space"> </span><span class="pas1-reservedword">begin </span><span class="pas1-space"> </span><span class="pas1-identifier">NewCapacity:=</span><span class="pas1-space"> </span><span class="pas1-identifier">M.Size+1; </span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStreamProtected(M).Realloc(NewCapacity); </span><span class="pas1-space"> </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">; </span><span class="pas1-space"> </span><span class="pas1-identifier">NullString(M.Memory^)[M.Size]:=</span><span class="pas1-space"> </span><span class="pas1-character">#0; </span><span class="pas1-space"> </span><span class="pas1-identifier">Result:=</span><span class="pas1-space"> </span><span class="pas1-identifier">StrPas(M.Memory); </span><span class="pas1-space"> </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">; </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">; </span></span>
解决方法
你的代码是不必要的复杂,即使对于老版本的Delphi版本。为什么获取字符串版本的流强迫流的内存被重新分配?
function MemoryStreamToString(M: TMemoryStream): string; begin SetString(Result,PChar(M.Memory),M.Size div SizeOf(Char)); end;
它适用于所有的Delphi版本,而不仅仅是Delphi 2009。当流是空的没有任何特殊情况下工作。 SetString
是一个不起眼的功能。
如果您的流的内容没有更改为Unicode,切换到Delphi 2009,那么您应该使用此功能代替:
function MemoryStreamToString(M: TMemoryStream): AnsiString; begin SetString(Result,PAnsiChar(M.Memory),M.Size); end;
这相当于你的原始代码,但跳过特殊情况。