PByteArray到TMemoryStream并反转

时间:2013-07-23 13:00:55

标签: delphi

我有以下代码。我正在尝试对buf指向的数据进行计算。

buf:PByteArray
inmem,outmem:TmemoryStream;
...
size := GetFileSize(aFile, nil);
buf := MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);

inmem.SetSize(size);
inmem.Position := 0;

//buf to memorystream
inmem.Write(buf[0], size);
outmem := tmemorystream.Create;
outmem.SetSize(size);

//Do memory stream processing
ProcMemoryStreams(inmem,outmem,size);
//

outmem.Position:=0;
//memorystream to buf
outmem.Read(buf[0],size); <-Access violation

我做错了什么?

1 个答案:

答案 0 :(得分:4)

您正在尝试写入只读缓冲区

buf := MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0); //read-only flag HERE!
...
outmem.Read(buf[0],size);  //writing to read-only memory region

所以添加FILE_MAP_WRITE标志(不要忘记CreateFileMapping中的访问标志)