Solved

Easy Question...

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

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

21 Experts available now in Live!

Get 1:1 Help Now