Solved

StrPCopy problem

Posted on 1998-07-18
7
1,793 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
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now