首页  编辑  

OLE流化存储读取

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

OLE 流化存储读取

uses ActiveX;

function OleControlLoadFromStream(mOleControl: TOleControl;

 mStream: TStream): Boolean;

var

 Stream: IStream;

 vHandle: HGLOBAL;

 vBuffer: PChar;

 vPersistStream: IPersistStream;

begin

 Result := False;

 if not Assigned(mOleControl) then Exit;

 if not Assigned(mStream) then Exit;

 mStream.Position := 0;

 vHandle := GlobalAlloc(GHND, mStream.Size);

 try

   vBuffer := GlobalLock(vHandle);

   if not Assigned(vBuffer) then Exit;

   mStream.Read(vBuffer^, mStream.Size);

   GlobalUnlock(vHandle);

   if CreateStreamOnHGlobal(vHandle, True, Stream) <> S_OK then Exit;

   mOleControl.DefaultDispatch.QueryInterface(IPersistStreamInit,

     vPersistStream);

   vPersistStream.Load(Stream);

 finally

   GlobalFree(vHandle);

 end;

 Result := True;

end; { OleControlLoadFromStream }

procedure TForm1.Button2Click(Sender: TObject);

var

 vMemoryStream: TMemoryStream;

begin

 vMemoryStream := TMemoryStream.Create;

 try

   vMemoryStream.LoadFromFile('c:\temp\temp.data');

   OleControlLoadFromStream(ShockwaveFlash1, vMemoryStream);

 finally

   vMemoryStream.Free;

 end;

end;

function OleControlSaveToStream(mOleControl: TOleControl;

 mStream: TStream): Boolean;

var

 Stream: IStream;

 vHandle: HGLOBAL;

 vBuffer: PChar;

 vPersistStream: IPersistStream;

 vSize: Int64;

 vObjectData: PHandle;

begin

 Result := False;

 if not Assigned(mOleControl) then Exit;

 if not Assigned(mStream) then Exit;

 mStream.Position := 0;

 vHandle := GlobalAlloc(GMEM_MOVEABLE, 0);

 if vHandle = 0 then OutOfMemoryError;

 try

   if CreateStreamOnHGlobal(vHandle, False, Stream) <> S_OK then Exit;

   mOleControl.DefaultDispatch.QueryInterface(IPersistStreamInit,

     vPersistStream);

   vPersistStream.Save(Stream, True);

   vBuffer := GlobalLock(vHandle);

   try

     mStream.Write(vBuffer^, GlobalSize(vHandle));

   finally

     GlobalUnlock(vHandle);

   end;

 finally

   GlobalFree(vHandle);

 end;

 Result := True;

end; { OleControlSaveToStream }