Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

EAccessViolation Error

Posted on 1997-12-07
4
Medium Priority
?
577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 600 total points
ID: 1353061
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
ID: 1353062
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
ID: 1353063
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
ID: 1353064
Thanks JB - your answer was perfect for my needs! JE
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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