function GetMsgData(FileName: String; Stream: TMemoryStream): Boolean;
var pvStg: IStorage;
pvStm: IStream;
stat: TStatStg;
dwSize: Integer;
const
StgFlags = STGM_READ or STGM_SHARE_EXCLUSIVE;
StmName = '__substg1.0_1000001E';
begin
// Set default result
result:=False;
// Open storage file
if (StgOpenStorage(PWideChar(WideString(FileName)), nil, StgFlags, nil, 0, pvStg) = S_OK) then
begin
// Attempt to get the stream
if (pvStg.OpenStream(PWideChar(WideString(StmName)), nil, StgFlags, 0, pvStm) = S_OK) then
begin
// Stat the stream to get the size
if (pvStm.Stat(stat, STATFLAG_NONAME) = S_OK) then
begin
// Clear memory stream
Stream.Clear;
Stream.Size:=stat.cbSize;
// Write contents of stream to memory stream
result:=(pvStm.Read(Stream.Memory, stat.cbSize, @dwSize) = S_OK);
end;
// Release the stream
pvStm:=nil;
end;
// Release the storage interface
pvStg:=nil;
end;
end;
-----
Example of usage
var
ms: TMemoryStream;
begin
ms:=TMemoryStream.Create;
if GetMsgData('c:\mypath\myfile.msg', ms) then
MessageBox(0, ms.Memory, nil, MB_OK);
if GetEmlData('c:\mypath\myfile.eml', ms) then
MessageBox(0, ms.Memory, nil, MB_OK);