Solved

EAccessViolation Error

Posted on 1997-12-07
4
575 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 150 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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