Solved

Easy Question...

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX and jaudiotracker playing memory stream 29 87
Drag & Drop... Data from one grid to another 2 42
firemonkey keyboard covers the controls 1 37
enhance the following code 3 37
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

828 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