Solved

StrPCopy problem

Posted on 1998-07-18
7
1,808 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

791 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