Registriert seit: 23. Okt 2003
159 Beiträge
Delphi 2010 Architect
|
Re: TBitmap von zwei Programmen austauschen (Speicher zuweis
16. Mär 2008, 17:17
Also ich habe es jetzt folgendermaßen leider erfolglos probiert ein Bitmap über ein Memory Mapped File auszutauschen.
Leider kommen in dem Stream nur Nullen an
Findet jemand einen Fehler?
Sender:
Delphi-Quellcode:
const
mmfName = 'FaceBitmapData';
procedure WriteToMMF(m: TMemoryStream; Filename: string);
var
hFileMapping: THandle;
Mem: Pointer;
begin
hFileMapping:=CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, m.Size, PChar(Filename));
if hFileMapping <> 0 then
begin
Mem:=MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
if Assigned(Mem) then
begin
CopyMemory(Mem, m.Memory, m.Size);
CloseHandle(hFileMapping);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
begin
bmp:=TBitmap.Create;
mStream:=TMemoryStream.Create;
try
bmp.LoadFromFile('c:\t.bmp');
bmp.SaveToStream(mStream);
WriteToMMF(mStream, mmfName);
ExecuteFile(ExtractFilePath(ParamStr(0)) + 'ReadKonsole.exe', mmfName + ' ' + IntToStr(mStream.Size), '', false, true);
finally
FreeAndNil(bmp);
FreeAndNil(mStream);
end;
end;
Empfänger (KonsolenApp):
Delphi-Quellcode:
var
Bmp: TBitmap;
OldRegVal: String;
inSize: Integer;
hFileMapping: THandle;
Mem: Pointer;
m: TMemoryStream;
begin
Bmp:=TBitmap.Create;
m:=TMemoryStream.Create;
try
inSize:=StrToInt(ParamStr(2));
hFileMapping:=CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READONLY, 0, inSize, PChar(ParamStr(1)));
if hFileMapping <> 0 then
begin
Mem:=MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
if Assigned(Mem) then
begin
m.SetSize(inSize);
m.Seek(0, soFromBeginning); //unnötig?!
CopyMemory(m.Memory, Mem, inSize);
m.SaveToFile('c:\stream.dat');
Bmp.LoadFromStream(m);
Bmp.SaveToFile('c:\out.bmp');
end;
CloseHandle(hFileMapping);
end;
finally
FreeAndNil(Bmp);
FreeAndNil(m);
end;
Danke schonma!
|
|
Zitat
|