Solved

StrPCopy problem

Posted on 1998-07-18
7
1,816 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 50 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 120
URL for downloading Google Chrome for Win XP 2 235
JAudiorecorder record freezing the app 29 83
shape, triangle, dbctrlgrid 3 33
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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