Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Easy Question...

Posted on 1998-03-02
4
Medium Priority
?
166 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

971 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