Link to home
Start Free TrialLog in
Avatar of JohnE
JohnE

asked on

EAccessViolation Error

This question partly refers to a previous question (Records and Longstrings 26/11/97). Anilm gave me the following code (the kstring part) so I could try to save a record with more than 255 characters. I have tried to implement it, but keep getting an EAccessViolation error. I have tried to read as much about pointers etc but still am no nearer a solution. Is there a solution to this, or perhaps a better way of working around the records and longstring problem?

Sample source below (thanks to Anilm for previous contribution)

Type
kstring = record
kslen: integer;
ksdata:array[1..1024] of char
end;

procedure putks(instring:string;var outks:kstring);
begin
outks.kslen := length(instring);
move(instring[1],outks.ksdata,length(instring));
end;

function getks(inks:kstring):string;
var
tmpstr:string;
begin
move(inks.ksdata,tmpstr[1],inks.kslen);
SetLength(tmpstr,inks.kslen);
getks := tmpstr;
end;

Type
Emp_Rec = record
Emp_Id:string[10];
Emp_Name:string[40];
Emp_Notes:kstring;
end;

var
Employee: Emp_Rec;

procedure TForm2.Button1Click(Sender: TObject);
begin
Putks(Edit1.Text, Employee.Emp_Notes);
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
Memo1.Text := getks(Employee.Emp_Notes);
end;
ASKER CERTIFIED SOLUTION
Avatar of JimBob091197
JimBob091197

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JohnE
JohnE

ASKER

Hi JB, and thanks for your answer - no more EAccessViolation errors! However it seems I am still stuck with my original problem. When I implement this code it still only allows me 255 characters. in the Kstring instead of the 1k allowed for in the ksdata array. Any ideas?
Sorry, I forgot, StrPCopy treats strings as old Pascal-style strings with length of 255 chars.

Change PutKs to the following, and it should work fine:

procedure PutKs(InStr: string; var ks: KString);
begin
  StrCopy(PChar(@ks.ksData), PChar(InStr));
end;

Cheers,
JB
Avatar of JohnE

ASKER

Thanks JB - your answer was perfect for my needs! JE