Autor Beitrag
meolus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 78

Gentoo, Debian, Win7 64-bit, WinXP
Delphi 2006 Prof., Delphi 2005 PE
BeitragVerfasst: Di 29.03.05 12:23 
Hallo zusammen,

ich verwende eine Abgewandelte Version eines Programmiertipps von Swissdelphicenter -> www.swissdelphicente...showcode.php?id=1260

Das Speichern und Laden in der Registry funktioniert bereits einwandfrei. Jedoch bereitet mir das Speichern in einen Stream schwierigkeiten!

Beim Ausführen des folgenden Codes bekomme ich in der Zeile mit " MyStream.Write(sz, SizeOf(Integer));", also eigentlich dem ersten Zugriff auf den TStream MyStream eine 'Zugriffverletzung bei Addresse 004C41A3 in Modul 'MyEXE.exe'. Lesen von Addresse 00000010'.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
type
  FontRec = packed record
    Color: TColor;
    BackGround: TColor;
    LogFont: TLogFont;
  end;

var
    MyStream: TStream;
    sz: Integer;



      sz := SizeOf(fRec.LogFont);
      if Windows.GetObject(RichEdit.Font.Handle, sz, @fRec.LogFont) > 0
      then begin
        MyStream.Write(sz, SizeOf(Integer));
        fRec.Color := RichEdit.Font.Color;
        fRec.BackGround := RichEdit.Color;
        MyStream.Write(fRec, SizeOf(fRec));
      end else begin
        sz := 0;
        MyStream.Write(sz, SizeOf(Integer));
      end;


Wie bekomme ich den Code ans rennen?

MfG Meolus

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt.

_________________
Real programmers don't comment their code;
if it was hard to write, it should be hard to read.
HolgerB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 91

Win XP
D6 Pers
BeitragVerfasst: Di 29.03.05 23:10 
Hallo,

du hast Mystream := TStream.createvergessen. 8)

Da kommen wir auch gleich zum zweiten Problem. TStream ist eine sogenannte abstrakte Klasse. Die kann garnix, außer abstürzen und ist nur für die Kompatiblität da. Du musst Dich entscheiden ob du einen Memorystream oder einen Filestream verwenden möchtest. Objekte kann man in einem Recourcestream Abspeichern. Das sollte also auch für einen TFont funktionieren. Das habe ich aber selber noch nicht ausprobiert.

In der OH findest du alles nötige unter Streams.

gruß
Holger
meolus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 78

Gentoo, Debian, Win7 64-bit, WinXP
Delphi 2006 Prof., Delphi 2005 PE
BeitragVerfasst: Di 29.03.05 23:39 
NAbend,

danke HolgerB! Das mit Mystream := TStream.Create hatte ich bereits versucht, jedoch auch nur abstürze bekommen! Mit TResourceStream (der leider in der OH nicht kreuzverbunden ist mit TStream) kann ich scheinbar nur auf Resourcen zugreifen, neue zu erstellen gelang mir gar nicht erst!

Ich habe jetzt aus dem TStream einen TMemoryStream gemacht und mit MyStream := TMemoryStream.Create; erstellt, aber ich bekomme einfach nichts in den Stream rein! Die MyStream.Write - Zeilen scheinen gänzlich wirkungslos und ja es ist etwas in dem packed record fRec drin! :)

Wie bekomme ich nun den Array in den Stream? Die Daten müssen in den Stream, weil ich mit TIniFile nur WriteBinaryStream machen kann! Oder kennt jemand eine andere Möglichkeit TFonts (+ Zusatzdaten) in einer Ini-Datei zu speichern?

MfG Meolus

_________________
Real programmers don't comment their code;
if it was hard to write, it should be hard to read.
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mi 30.03.05 07:40 
Hallo,

hast du den Memorystream, bevor Du ihn in die Ini-Datei, speicherst auch wieder auf die Anfangsposition gesetzt?

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
meolus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 78

Gentoo, Debian, Win7 64-bit, WinXP
Delphi 2006 Prof., Delphi 2005 PE
BeitragVerfasst: Mi 30.03.05 09:26 
Hm, ich glaube nein! Ich habe nur die Zeilen aus meinem ersten Post. Von TStream und seinen Ableitungen habe ich jedoch sogut wie keine Ahnung! Wie setzte ich den MemoryStream auf die Anfangsposition?

MfG Meolus

_________________
Real programmers don't comment their code;
if it was hard to write, it should be hard to read.
HolgerB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 91

Win XP
D6 Pers
BeitragVerfasst: Mi 30.03.05 11:53 
Hallo,

mit dem Recourcestream habe ich mich leider total Verbockt. Der hat mit Komponentenspeicherung leider nix zu tun.

Der richtige Befehl ist WriteComponent. Damit kannst du Komponenten speichern. Leider ist TFont eine direkte Ableitung der Windows API und daher keine Komponente. Auf jeden Fall gibt mir mein Delphi6 nur ne Felhermeldung aus.

Daher wirst Du deinen Font wohl manuell abspeichern müssen.

Über Position kannst Du die Position in deinem Stream ändern.

In deinem Code solltest du integer noch gegen longint oder int64 tauschen. Je nach Delphiversion kann ein Integer unterschiedlich viele Bytes haben und wenn du mal umsteigst kann neu Compilierter Code die alten Streams nicht lesen...sehr ärgerlich und kaum zu finden.

Gruß
Holger
meolus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 78

Gentoo, Debian, Win7 64-bit, WinXP
Delphi 2006 Prof., Delphi 2005 PE
BeitragVerfasst: Mi 30.03.05 12:35 
Ich habe jetzt folgenden Code, keine Fehlermeldungen mehr, jedoch scheint auch nichts in den Stream hineingeschriebem zu werden :-/

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
      sz := SizeOf(fRec.LogFont);
      if Windows.GetObject(RichEdit.Font.Handle, sz, @fRec.LogFont) > 0
      then begin
        MyStream := TMemoryStream.Create;
        MyStream.Position := 0; //Startposition im Stream?!
        MyStream.Write(sz, SizeOf(LongInt));
        fRec.Color := RichEdit.Font.Color;
        fRec.BackGround := RichEdit.Color;
        MyStream.Write(fRec, SizeOf(fRec));
      end else begin
        sz := 0;
        MyStream.Write(sz, SizeOf(LongInt));
      end;


Mit euren Ratschlägen kann ich bisher leider sogut wie nichts anfangen, da ich keine Ahnung von Streams habe!

Wäre cool, wenn ihr mir sagen könnt, was ich da noch machen muss, damit das gespeichert wird!


MfG Meolus

_________________
Real programmers don't comment their code;
if it was hard to write, it should be hard to read.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Mi 30.03.05 13:00 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
type
  TFontRec = packed record
    Color: TColor;
    BackGround: TColor;
    LogFont: TLogFont;
  end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure SaveToFile(const FileName: String; const FontRec: TFontRec);
var
  Stream: TFileStream;
begin

  Stream := TFileStream.Create(FileName, fmCreate);
  try
    Stream.WriteBuffer(FontRec, SizeOf(TFontRec));
  finally
    Stream.Free;
  end;

end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure LoadFromFile(const FileName: String; var FontRec: TFontRec);
var
  Stream: TFileStream;
begin

  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    Stream.ReadBuffer(FontRec, SizeOf(TFontRec));
  finally
    Stream.Free;
  end;

end;

_________________
Ciao, Sprint.
meolus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 78

Gentoo, Debian, Win7 64-bit, WinXP
Delphi 2006 Prof., Delphi 2005 PE
BeitragVerfasst: Mi 30.03.05 13:20 
Ich habe jetzt folgendes gemacht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
      if Windows.GetObject(RichEdit.Font.Handle, SizeOf(fRec.LogFont), @fRec.LogFont) > 0
      then begin
        MyStream := TMemoryStream.Create;
        MyStream.WriteBuffer(fRec, SizeOf(TFontRec));
        ini.WriteBinaryStream('Main','RichEdit', MyStream);
        MyStream.Free;
      end;


GetObject und MemoryStream.Create funktionieren einwand frei - Der Record schaut so aus, wie er soll und dann wir halt WriteBuffer ausgeführt, ohne Fehler, bewirkt aber nichts! Der Stream bleibt leer!

Was ist daran bloß falsch?

MfG Meolus

_________________
Real programmers don't comment their code;
if it was hard to write, it should be hard to read.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Mi 30.03.05 13:29 
user profile iconmeolus hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
      if Windows.GetObject(RichEdit.Font.Handle, SizeOf(fRec.LogFont), @fRec.LogFont) > 0
      then begin
        MyStream := TMemoryStream.Create;
        MyStream.WriteBuffer(fRec, SizeOf(TFontRec));
        ini.WriteBinaryStream('Main','RichEdit', MyStream);
        MyStream.Free;
      end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
      if Windows.GetObject(RichEdit.Font.Handle, SizeOf(fRec.LogFont), @fRec.LogFont) > 0
      then begin
        MyStream := TMemoryStream.Create;
        MyStream.WriteBuffer(fRec, SizeOf(TFontRec));
        MyStream.Position := 0;
        ini.WriteBinaryStream('Main','RichEdit', MyStream);
        MyStream.Free;
      end;

_________________
Ciao, Sprint.
meolus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 78

Gentoo, Debian, Win7 64-bit, WinXP
Delphi 2006 Prof., Delphi 2005 PE
BeitragVerfasst: Mi 30.03.05 13:33 
Hey, Sprint big THX! *knutsch* *knuddel* ;)

Das wars... ich glaube da wird mir was im Umgang mit Streams klar :D Bisher dachte ich, dass einfach der komplette Stream übergeben wird und ich daher nicht da auf den Anfang setzen muss...

MfG Meolus

_________________
Real programmers don't comment their code;
if it was hard to write, it should be hard to read.