Solved

EAccessViolation Error

Posted on 1997-12-07
4
567 Views
Last Modified: 2010-04-04
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;
0
Comment
Question by:JohnE
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 150 total points
Comment Utility
The following gives no access violations:

type
  KString = record
    ksData: array[0..1024] of Char;
  end;

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

function GetKs(ks: KString): string;
begin
  Result := string(PChar(@ks.ksData));
end;


You will note that I have removed the ksLen field of the KString record, but if you want the length of a KString record call:
"Length(GetKs(AKs))".

Regards,
JB
0
 

Author Comment

by:JohnE
Comment Utility
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?
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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
0
 

Author Comment

by:JohnE
Comment Utility
Thanks JB - your answer was perfect for my needs! JE
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now