请教:TMemoryStream的Memory属性用法(10)

  • 主题发起人 主题发起人 BiPigBen1001
  • 开始时间 开始时间
B

BiPigBen1001

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠,你们好,本人想实现一个如下功能,我创建一个TMemoryStream对象,然后往这个流中写入'abcdefg',我想通过TMemoryStream把流的内容拷贝到一个字节数组中,可惜报错,往各位前辈指教var pStream:TMemoryStream; ss:String; pp:array of byte; pLength:Integer;begin ss:='abcdefg'; pLength:=Length(ss); pStream:=TMemoryStream.Create; pStream.WriteBuffer(ss[1],length(ss)); pStream.Seek(0,0); setLength(pp,pLength); Move(Pointer(Integer(pStream.memory))^,pp,pLength); freeAndNil(pStream); //end;我跟踪了PP,这个值不对,而且还报错
 
使用下面不可以么!pStream.Seek(0,0);pStream.ReadBuffer(pp[0], pLength)
 
procedure TForm1.Button2Click(Sender: TObject);var StrFile: string; OD: TOpenDialog; MS: TMemoryStream; bData: TByteDynArray; Count: Int64; HTTPRIO1: THTTPRIO; ufss: UploadFileServiceSoap;begin Edit1.Clear; Application.ProcessMessages; OD := TOpenDialog.Create(nil); if not OD.Execute() then begin FreeAndNil(OD); exit; end; StrFile := OD.FileName; FreeAndNil(OD); MS := TMemoryStream.Create(); MS.LoadFromFile(StrFile); Count := MS.Size; MS.Position := 0; SetLength(bData, Count); Move(MS.Memory^, bData[0], Count); FreeAndNil(MS); StrFile := ExtractFileName(StrFile); HTTPRIO1 := THTTPRIO.Create(nil); HTTPRIO1.HTTPWebNode.UseUTF8InHeader := true; HTTPRIO1.WSDLLocation := Edit2.Text; HTTPRIO1.Service := 'UploadFileService'; HTTPRIO1.Port := 'UploadFileServiceSoap'; ufss := HTTPRIO1 as UploadFileServiceSoap; Application.ProcessMessages; Edit1.Text := ufss.UploadFile(StrFile, bData); FreeAndNil(HTTPRIO1);end;
 
谢谢草原骏马和楼上的兄弟,草原骏马的方法正是我想要的,OK了
 
Vartmp:pointer;tmp:=pStream.memory;Move(tmp^,pp,pLength);
 
后退
顶部