200分求助!内存映射时,该如何共享TMemoryStream?(200)

  • 主题发起人 主题发起人 dalse
  • 开始时间 开始时间
看看万一老师的教程~~http://www.cnblogs.com/del/category/118249.html
 
看看de410的笔记,哈哈。。。http://www.delphibbs.com/keylife/iblog.asp?author=de410
 
[:D]不是笔记,是文摘~
 
我找的一个文件加解密的例子供你参考一下:function TForm1.DoDecrypt(InFile: string): Boolean;var FFile: TFileStream; FMems: TMemoryStream; hMapFile: THandle; pMapFile: Pointer;//PByte; pSrc, pDest: PByte; I: Integer;begin Result := False; if not FileExists(InFile) then Exit;// raise Exception.Create('File does not exist.')// else FFile := TFileStream.Create(InFile, fmOpenRead); if not Assigned(FFile) then Exit;// raise Exception.Create('Failed to create file stream.'); try FMems := TMemoryStream.Create; //用你的加密处理代替下面的这一句; FMems.LoadFromStream(FFile); //Descrypt(FFile, FMems); finally FFile.Free; //释放文件流; end; try //创建一个(没有文件对应的)特殊的内存映射文件; hMapFile := CreateFileMapping( DWord(-1), //要映射的文件句柄,此处为无文件; nil, //指向一个结构,指明是否可被子进程继承; PAGE_READWRITE, //保护模式(权限); 0, FMems.Size, //内存映射文件大小的高、低字节; nil); //内存映射文件对象的名字; if hMapFile = 0 then Exit; // raise Exception.Create('Failed to create file mapping'); try //创建一个内存映射文件的视图; pMapFile := MapViewOfFile( hMapFile, //内存映射文件对象句柄; FILE_MAP_ALL_ACCESS, //访问模式(权限); 0, 0, //开始映射的起始位置(高、低位); 0{FMems.Size}); //视图映射的字节数,为零时全部映射; if pMapFile = nil then Exit; // raise Exception.Create('Failed to map view of file'); finally CloseHandle(hMapFile); //释放内存映射文件对象句柄; end; try //复制解密后的内存流到内存映射文件中; //此处用Move函数处理是不行的,没有一次不发生异常; pSrc := PByte(FMems.Memory); pDest := PByte(pMapFile); for I := 1 to FMems.Size do begin pDest^ := pSrc^; Inc(pSrc); Inc(pDest); end; //或者用以下函数复制内存流到内存映射文件中; //StrCopy(PChar(pMapFile), PChar(ms.Memory)); //测试用,在此处使用内存映射文件; Memo1.Lines.Text := PChar(pMapFile); Result := True; finally UnmapViewOfFile(pMapFile); //释放内存映射文件的视图; end; finally FMems.Free; //释放内存流; end;end;
 
创建内存映射文件时,所用API为:HANDLE CreateFileMapping( HANDLE hFile, // handle to file to map LPSECURITY_ATTRIBUTES lpFileMappingAttributes, // optional security attributes DWORD flProtect, // protection for mapping object DWORD dwMaximumSizeHigh, // high-order 32 bits of object size DWORD dwMaximumSizeLow, // low-order 32 bits of object size LPCTSTR lpName // name of file-mapping object ); 若hfile参数为:$FFFFFFFF,则映射内存流,看下边DELPHI帮助的说明:ParametershFileIdentifies the file from which to create a mapping object. The file must be opened with an access mode compatible with the protection flags specified by the flProtect parameter. It is recommended, though not required, that files you intend to map be opened for exclusive access. If hFile is (HANDLE)0xFFFFFFFF, the calling process must also specify a mapping object size in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. The function creates a file-mapping object of the specified size backed by the operating-system paging file rather than by a named file in the file system. The file-mapping object can be shared through duplication, through inheritance, or by name. 然后PCHAR取得内存流的地址,怎么样操作都可以。若文件已存在,则用openfilemapping即可,查一下API即可
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
562
import
I
后退
顶部