Solved

StrPCopy problem

Posted on 1998-07-18
7
1,801 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
indy tidtcpclient threading issue 8 63
Machine not responding during CopyFile() 3 89
Delphi cmd execution 6 56
Tembedded WB animatid gifs not animated on some pcs 2 73
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…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

13 Experts available now in Live!

Get 1:1 Help Now