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.ksd ata,length (instring) );
end;
function getks(inks:kstring):string ;
var
tmpstr:string;
begin
move(inks.ksdata,tmpstr[1] ,inks.ksle n);
SetLength(tmpstr,inks.ksle n);
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;
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.ksd
end;
function getks(inks:kstring):string
var
tmpstr:string;
begin
move(inks.ksdata,tmpstr[1]
SetLength(tmpstr,inks.ksle
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
begin
Putks(Edit1.Text, Employee.Emp_Notes);
end;
procedure TForm2.Button2Click(Sender
begin
Memo1.Text := getks(Employee.Emp_Notes);
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Change PutKs to the following, and it should work fine:
procedure PutKs(InStr: string; var ks: KString);
begin
StrCopy(PChar(@ks.ksData),
end;
Cheers,
JB
ASKER
Thanks JB - your answer was perfect for my needs! JE
ASKER