Autor Beitrag
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 14.10.07 00:06 
Gar nicht, das musst du dir schon merken. In der decode-Funktion gibts ja auch die Variable idx, die den aktuellen Buchstaben im Ergebnis angibt. Eigentlich hatte ich die Hoffnung, dass du verstanden hast, was der Quelltext macht...
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: So 14.10.07 08:11 
Habe ich, aber bei der encode-Funktion wird nicht bei jedem Schritt nur ein Zeichen in den resultBuf geschrieben...
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 14.10.07 09:08 
user profile iconWolle92 hat folgendes geschrieben:
Habe ich, aber bei der encode-Funktion wird nicht bei jedem Schritt nur ein Zeichen in den resultBuf geschrieben...
Du musst eben idx um die Anzahl der geschriebenen Zeichen erhöhen.
Und die innere Schleife muss nicht mit text2 sondern mit current laufen, idx muss eben innerhalb der inneren Schleife bei jedem Buchstaben erhöht werden, statt als for-Variable verwendet zu werden.
Dann hast du sowas:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
    for j := 1 to Length(current) do
    begin
      resultBuf[idx] := current[j];
      ...
    end;
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: So 14.10.07 09:22 
darauf hätt ich auch kommen können, bin ich aber nicht, aber zum glück gibt es dieses Forum... Danke
aber j muss doch bei 0 anfangen, oder?

Wieso kriege ich jetzt bei der decode-Funktion eine Access-Violation wenn ich die encode-Funktion geändert habe?
Bei einer Datei, die mit der neuen encode kodiert wurde...

Achso, weil nichts in der Datei steht...
Aber wieso steht nichts drin?

Ich meine, ich würd dir ja gern die crypt.dll-Source schicken aber irgendwie wird das ja immer weniger mein programm...
Wenn das keine Freeware werden würde würdest du Geld bekommen...
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: So 14.10.07 14:39 
so, hab wieder nen bisschen an der DLL rumgebastelt und bekomme zur Belohnung eine AccessViolation beim letzten FreeMem im Hauptprogramm...
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 14.10.07 14:44 
user profile iconWolle92 hat folgendes geschrieben:
aber j muss doch bei 0 anfangen, oder?
Nein, eben nicht... Strings fangen mit Index 1 an, der erste Buchstabe ist z.B. current[1].
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:
25:
function encode(const text: PChar; const resultBuf : PChar;
  const resultBufSize: Integer): Integer; stdcall; export;
var i, j, idx: Integer;
    text2, current: String;
begin
  SetLength(text2, StrLen(text));
  StrCopy(PChar(text2), text);
  if Length(text2) > resultBufSize then
  begin
    result := -1;
    Exit;
  end;
  idx := 0;
  for i := 1 to Length(text2) do
  begin
    current := IntToStr(Ord(text2[i])) + 'z';
    for j := 1 to Length(current) do
    begin
      resultBuf[idx] := current[j];
      Inc(idx);
    end;
  end;
  resultBuf[idx] := #0;
  result := StrLen(resultBuf);
end;

Dann hast du noch einen Fehler drin (Unit3):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
      if SaveDialog1.Execute then
      begin
        saveFileName := saveDialog1.FileName;
      end;
      AssignFile(f, saveFileName);

Hmm? Wenn der Dialog erfolgreich ausgeführt wurde, packst du den Dateinamen in saveFileName, sonst packst du da gar nix rein. Und egal, ob der Dialog abgebrochen wurde und dann gar kein Dateiname in der Variablen steht, versuchst du AssignFile und so weiter aufzurufen...
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Mo 15.10.07 14:12 
ok, letzte frage (fürs erste): Wieso muss denn idx mit 0 anfangen? ist das bei PChars anders als bei Strings?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 15.10.07 14:29 
Ja, ist es, PChar ist eigentlich einfach nur ein Pointer auf ein Array von Chars im Speicher. Und in einem Array ist das erste Element eben das mit dem Index 0. Bei Strings ist das anders, weil bei ShortStrings (früher die normalen Strings) im ersten Zeichen 0 die Länge gespeichert wurde. (Was bei PChars ja nicht notwendig ist, weil das Nullzeichen #0 das Ende des Strings anzeigt, deshalb heißen PChars ja nullterminiert.)
Da die jetzigen Strings (AnsiStrings) nicht mehr auf 255 Zeichen begrenzt sind, reicht ein Zeichen dafür gar nicht mehr, deshalb ist das jetzt anders. Damit aber nicht plötzlich alle Quelltexte anders sein müssen und sich alle umgewöhnen müssen, ist der erste Buchstabe bei Strings weiterhin der mit dem Index 1. (Abwärtskompatibilität ;-))
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Mo 15.10.07 14:57 
OK, wieder was dazugelernt, aber dafür gibts jetzt wieder nen FreeMem-Problem:
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:
25:
26:
27:
AssignFile(f, saveFileName);
ReWrite(f);
cntvoc := ListView1.Items.Count;
codeBufSize := Length(IntToStr(cntvoc))*4;
GetMem(codeBuf, codeBufSize);
encode(PChar(IntToStr(cntvoc)),codeBuf,codeBufSize);
WriteLn(f, String(codeBuf));
for i := 0 to cntvoc-1 do
begin
  buffer := ListView1.Items[i].Caption;
  codeBufSize := Length(buffer)*4;
  FreeMem(codeBuf);
  GetMem(codeBuf, codeBufSize);
  encode(PChar(buffer),codeBuf,codeBufSize);
  WriteLn(f, String(codeBuf));
  buffer := ListView1.Items[i].SubItems[0];
  codeBufSize := Length(buffer)*4;
  FreeMem(codeBuf);
  GetMem(codeBuf, codeBufSize);
  encode(PChar(buffer),codeBuf,codeBufSize);
  WriteLn(f, String(codeBuf));
end;
CloseFile(f);
FreeMem(codeBuf);
FreeLibrary(handle1);
ShowMessage('Datei erfolgreich gespeichert!');
Form3.Hide;

Ich hab eicht keinen Plan, woher das kommen könnte...
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 15.10.07 15:35 
Zunächst mal: Es ist ziemlich schlechter Programmierstil, dass du das Freigeben von Speicher erst vor dem nächsten GetMem machst und nicht dort, wo du ihn nicht mehr brauchst. Zu jedem GetMem gibts zwar ein FreeMem, aber das zu dem getMem vor der Schleife steht in der Schleife, usw.
Das macht das ganze extrem unübersichtlich.
Wie wäre es so (auch das mit dem Dialog ist so korrekt):
ausblenden volle Höhe 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:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
      if SaveDialog1.Execute then
        saveFileName := saveDialog1.FileName
      else
        Exit;
      handle1 := LoadLibrary(PChar(ExtractFilePath(Application.ExeName)+'crypt.dll'));
      if handle1 = 0 then
      begin
        ShowMessage('DLL crypt.dll konnte nicht geladen werden!');
        Exit;
      end;
      @encode := GetProcAddress(handle1, 'encode');
      AssignFile(f, saveFileName);
      ReWrite(f);
      cntvoc := ListView1.Items.Count;
      codeBufSize := 255;
      GetMem(codeBuf, codeBufSize);
      encode(PChar(IntToStr(cntvoc)),codeBuf,codeBufSize);
      WriteLn(f, String(codeBuf));
      FreeMem(codeBuf);
      for i := 0 to cntvoc-1 do
      begin
        buffer := ListView1.Items[i].Caption;
        GetMem(codeBuf, codeBufSize);
        encode(PChar(buffer),codeBuf,codeBufSize);
        WriteLn(f, String(codeBuf));
        FreeMem(codeBuf);

        buffer := ListView1.Items[i].SubItems[0];
        GetMem(codeBuf, codeBufSize);
        encode(PChar(buffer),codeBuf,codeBufSize);
        WriteLn(f, String(codeBuf));
        FreeMem(codeBuf);
      end;
      CloseFile(f);
      FreeLibrary(handle1);
      ShowMessage('Datei erfolgreich gespeichert!');
      Form3.Hide;

Btw: Es würde auch reichen, codeBuf zu leeren (bzw. eigentlich würde es sogar ohne das funktionieren), anstatt immer GetMem und FreeMem immer wieder zu benutzen.
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Mo 15.10.07 15:43 
danke, immerhin gibt es jetzt keinen Fehler mehr... aber jetzt schreibt er wieder nichts in die Datei...
Vielleicht wärs besser gewesen, ich hätte einfach die brlndmm.dll mitgeliefert und es bei Strings belassen, aber immerhin habe ich eine Herausforderung... :D
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 15.10.07 15:48 
:nixweiss: :lol: Bei mir gehts so^^
Ich schick dir das Pojekt mal per PN. ;-)
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Mo 15.10.07 15:52 
oh mann, eigentlich soll ich latein lernen...