Solved

Easy Question...

Posted on 1998-03-02
4
158 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
  • 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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