刚才有人问起..顺手写的.
他是用于DLL,所以我多写了2个函数.在DLL里提供内存指针输出,一定要记得提供相应的内存释放函数.
有借有还,再借不难...
老规矩..下面是demo

 1 var
2 MS: TMemoryStream;
3 bytes: PByte;
4 size: Integer;
5 begin
6 MS := TMemoryStream.Create;
7 MS.LoadFromFile('d:\1.rar');
8 size := StreamToBytes(MS, bytes);
9
10 MS.Clear;
11
12 BytesToStream(bytes, size, MS, True);
13 MS.SaveToFile('d:\2.rar');
14 end;

  

 1 procedure GetBytes(out Bytes: PByte; ByteSize: Integer);
2 begin
3 GetMem(Bytes, ByteSize);
4 end;
5
6 //如果是用在DLL里,一定要记得Export这个函数!!
7 procedure FreeBytes(Bytes: PByte);
8 begin
9 FreeMem(Bytes);
10 end;
11
12 procedure BytesToStream(Bytes: PByte; ByteSize: Integer;
13 Stream: TStream; NeedFree: Boolean);
14 begin
15 Stream.Size := 0;
16 Stream.Write(Bytes^, ByteSize);
17 //是否要释放Bytes?
18 if NeedFree then
19 FreeBytes(Bytes);
20 end;
21
22 function StreamToBytes(Stream: TStream;
23 out Bytes: PByte): Integer;
24 begin
25 Result := Stream.Size;
26 GetBytes(Bytes, Result);
27 Stream.Seek(0, soFromBeginning);
28 Stream.Read(Bytes^, Result);
29 end;

  

posted on 2011-07-21 23:10  solokey  阅读(332)  评论(0编辑  收藏  举报