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

x
?
Solved

StrPCopy problem

Posted on 1998-07-18
7
Medium Priority
?
1,860 Views
Last Modified: 2012-08-13
Hi...
   I'm just trying to do a very simple conversion of a regular old string to a PChar variable. here's my code:

StrPcopy(optparms, hoptparms);

...optparms is declared as PChar. hoptparms is declared as a string.

 Upon executing this line, the following error window appears:

"Project myproj.exe raised exception class EAccessViolation with message 'Access violation at address 00407E5D in module 'MYPROJ.EXE'. Read of address FFFFFFFF'. Process stopped. Use Step or Run to continue."

...Why? Seems like a simple enough thing to do?

Regards,
  Shawn Halfpenny
  drumme59@sprint.ca

P.S: I am using Delphi v3
0
Comment
Question by:aztec
[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
7 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1358175
"The destination buffer must have room for at least Length(Source)+1 characters." (Help)

Are you sure your null-terminated string is allocated corectly?
Try this:

optparms := StrAlloc(Length(hoptparms)+1);
StrPcopy(optparms, hoptparms);
0
 
LVL 2

Expert Comment

by:omsec
ID: 1358176
try first initalizing the pChar:

optparams := ;'';
0
 
LVL 3

Accepted Solution

by:
vladika earned 200 total points
ID: 1358177
1) Try simply PChar(hoptparms)

2) from help
StrPCopy does not perform any length checking.
The destination buffer must have room for at least Length(Source)+1 characters.

So,
  optparms := StrAlloc(Length(hoptparms)+1); // allocate enough space for string
  try
    StrPCopy(optparms, hoptparms); // copy string
    //  process optparms
  finally
    StrDispose(optparms); // free space
  end;

0
Industry Leaders: 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!

 
LVL 3

Expert Comment

by:Matvey
ID: 1358178
Vladika, isn't it exactly what was said before?!!
0
 
LVL 3

Expert Comment

by:vladika
ID: 1358179
Matvey,
You are right.
But you forget about StrDispose
If space for a string is allocated with StrAlloc,
it should be deallocated via StrDispose.

I downloaded question before your comment,
so I could not see it. Sorry.


0
 
LVL 3

Expert Comment

by:vladika
ID: 1358180
Aztec,

Since you use Delphi V3 ...

From help:
.
Delphi supports three types of strings: Short strings, long strings and wide strings.
.
The long string type represents a dynamically-allocated string
with a maximum length limited only by available memory.
For new applications, it is recommended that you use the long string type.
.
Since long strings are implicitly null terminated and dynamically allocated, the use of StrAlloc is in decline

So, you can use PChar(hoptparms)

0
 

Author Comment

by:aztec
ID: 1358181
right on! Thanks!

Shawn
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 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