0

我有以下代码。我正在尝试对 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

我究竟做错了什么?

4

1 回答 1

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 中的访问标志)

于 2013-07-23T13:25:34.130 回答