-1

I'm new to delphi and programming in general. I have a problem that when I compile and run the following DLL procedure I get an I/O Error 998. Could you please point out what I'm doing wrong while making the least possible amount of changes to my code? As far as I can tell it has to be a problem with the way I'm handling my files.

The idea of this procedure to to accept data from the VCL application using the DLL, save it to a record and then save the record to a .dat file.

type
  plist = ^game;
  tdata = record
    nazwa: shortstring;
    wydawca: shortstring;
    rokwyd: shortstring;
    gatunek1: shortstring;
    gatunek2: shortstring;
  end;
  game = record
    data: tdata;
    pointer: plist;
  end;

{$R *.res}

procedure DodajElement (var field1, field2, field3, field4, field5 : shortstring); stdcall;
var
  BazaDanych : file of game;
  first, current: plist;
begin
  AssignFile(BazaDanych, 'BazaDanychGier.dat');
  if not FileExists('BazaDanychGier.dat') then
    begin
      new(first);
      first^.data.nazwa := field1;
      first^.data.wydawca := field2;
      first^.data.rokwyd := field3;
      first^.data.gatunek1 := field4;
      first^.data.gatunek2 := field5;
      first^.pointer := nil;
      Rewrite(BazaDanych);
      Write(BazaDanych, first^);
      CloseFile(BazaDanych);
    end
  else
    begin
      Reset(BazaDanych);
      Read(BazaDanych, first^);
      CloseFile(BazaDanych);
      current := first;
      new(first);
      first^.data.nazwa := field1;
      first^.data.wydawca := field2;
      first^.data.rokwyd := field3;
      first^.data.gatunek1 := field4;
      first^.data.gatunek2 := field5;
      first^.pointer := current;
      Rewrite(BazaDanych);
      Write(BazaDanych, first^);
      CloseFile(BazaDanych);
    end;
end;

Sorry for the foreign variable names.

M.G.
  • 11
  • 2
  • Which line causes the exception to be raised? – Agustin Ortu Jan 29 '17 at 15:36
  • Why can't we have a [mcve]? This is not the first time we've seen this code. There are quite a lot of problems with the code. I must admit, I wonder why you aren't debugging. Why is that? Wouldn't it just be easier to learn how to debug and be able to do it yourself, instead of asking other people to debug fragments of your code? Wouldn't you like to be able to sort this our for yourself? – David Heffernan Jan 29 '17 at 15:57
  • Yes, I would like to be able to do this myself, I wouldn't be asking a question here if I hadn't already exhausted all my options. I'm asking for help not because I'm lazy but because I'm stuck. Also, as far as I can tell the debugger tells me everything is fine, I can't see any problems you are talking about. – M.G. Jan 29 '17 at 16:19
  • The else branch is the problem. As described in the answer. You should be able to debug this. – David Heffernan Jan 29 '17 at 16:58
  • 1
    Why are you trying to do this via a DLL? There are more subtle things that can go wrong. Debugging is a little more difficult.They provide no benefit for what you're trying to do. ... You're appealing for help as a beginner; I strongly recommend you save the advanced topics until you have a firm and solid understanding of the fundamentals. You're only making things more difficult for yourself by trying to run before you've figured out how to stand. – Disillusioned Jan 29 '17 at 22:29

1 Answers1

1

It's not clear to me why you are using pointers to a record (first^) when you could just as easily use the record itself.

You are dereferencing an unitialised pointer here

   Read(BazaDanych, first^);
No'am Newman
  • 6,395
  • 5
  • 38
  • 50
  • It's because I'm using a list of one way pointers to store my data, regardless I tried the changes you suggested and they didn't work. As far as I can tell there is no difference in functionality. – M.G. Jan 29 '17 at 15:10
  • @M.G.I have no idea what you actually did but I strongly suspect you don't understand the implications of using an "uninitialised pointer". So it's time for you to do some reading on that subject. Regardless, the duplicate I found as mentioned in the comment on your question is conceptually very similar to your own mistake. And this answer accurately points out that `Read` is failing, because you haven't provided the method with a ***valid*** location to store the data read from the file. – Disillusioned Jan 29 '17 at 22:41