Solved

Easy Question...

Posted on 1998-03-02
4
161 Views
Last Modified: 2010-05-18
Hi,
   I'm trying to pass a filename to WinExec. The problem is
that I have to build the name using info from the user.
I have done this:
var
   AppName : String;
   FileName: String;
   ExecuteLin : String;
   ExecuteLine : PChar;
begin
   AppName := 'Latex ';
   FileName := Edit1.Text;
   ExecuteLin := AppName + FileName;
   StrPCopy(ExecuteLine, ExecuteLin);
   WinExec(ExecuteLine, SW_SHOWMAXIMISED);
end;

When I try to run it I get a General Protection Fault, with
the StrPCopy line highlighted. Anyone know what seems to be
the problem?
Thanks...

0
Comment
Question by:NikWhit
[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
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 0 total points
ID: 1359967
The problem is that PChar is a Pointer to a Null-Terminated string, so it needs to be initialized; since you didn't allocate memory for it, your StrPCopy() call generates that GPF error.

Anyway, solving the problem is trivial, due to Delphi assignment compatibility; just typecast your "ExecuteLin" to PChar:

--//--
var
  AppName: String;
  FileName: String;
  ExecuteLin : String;
begin
  AppName := 'Latex ';
  FileName := Edit1.Text;
  ExecuteLin := AppName + FileName;
  WinExec(PChar(ExecuteLin), SW_SHOWMAXIMISED);
end;
--//--

BTW, say you want to keep the "ExecuteLine" stuff for some reason; the easier way to make your code work is:

ExecuteLine: array[0..128] of Char; { now space is allocated }

Technically, array of Char and PChar are not the same, but this could be matter for another hard question :)
0
 
LVL 1

Author Comment

by:NikWhit
ID: 1359968
I've just tried your suggestion, and on compilation, it reports
that it's an invalid cast. Any ideas?
Thanks...
Nik
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1359969
Is it Delphi1?

Sorry, try my second suggestion; declare ExecuteLine as:

ExecuteLine: array[0..128] of Char;

Let me know...
0
 
LVL 1

Author Comment

by:NikWhit
ID: 1359970
julio,
seems to work fine! I guess there must be a difference between
my version and yours. Using the char array is no problem for
my app, though. Thanks for your help...
Nik

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 135
Delphi Form ownership 4 160
Find and Replace Stream with 0s 8 88
DBGrid or StringGrid ? 6 136
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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