?
Solved

EAccessViolation Error

Posted on 1997-12-07
4
Medium Priority
?
576 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

765 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