popen is a function in Visual C's library.  Basically, it creates a pipe,
executes the process, "cdrecord -atip", and redirect its output to the pipe.

Unfortunately Delphi does not have a popen in the VCL, but you can achieve
the same using a few lines of code that makes WinAPI calls.  The following
algorithm uses a temporary file instead of a pipe, but the result is the
same - you get the output of the spawned process.

1.  Call GetTempFileName to get the name of a temporary file

2.  Declare TSecurityAttributes like so:

var
  SecAtrrs: TSecurityAttributes;
begin
  SecAtrrs.nLength := SizeOf(SecAtrrs);
  SecAtrrs.lpSecurityDescriptor := nil;
  SecAtrrs.bInheritHandle := True;  // Must be set to True!!!

3.  Create the file using a call to CreateFile, passing in SecAtrrs.  It is
important to use SecAtrrs - otherwise it will not work.  Save the returned
handle in a variable.   See example:

var
  hOutput: THandle;
begin
  hOutput := CreateFile (PChar(TempFileFilename),
    GENERIC_READ or GENERIC_WRITE,
    FILE_SHARE_READ or FILE_SHARE_WRITE, @SecAtrrs, CREATE_ALWAYS,
    FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_TEMPORARY, 0);

4.  Declare TStartupInfo like so:

var
  StartupInfo: TStartupInfo;
begin
  FillChar (StartupInfo, Sizeof(StartupInfo), 0);
  StartupInfo.cb := SizeOf (StartupInfo);
  StartupInfo.wShowWindow := SW_HIDE; // Or you may choose to display it
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or
    STARTF_FORCEOFFFEEDBACK or
    STARTF_USESTDHANDLES;
  StartupInfo.hStdOutput := hOutput;  // Reassign stdout and stderr
  StartupInfo.hStdError := hOutput;

5.  Declare TProcessInformation like so:

var
  ProcessInformation: TProcessInformation;
begin
  FillChar (ProcessInformation, Sizeof(ProcessInformation), 0);
  FillChar (SecAtrrs, SizeOf(SecAtrrs), 0);

6.  Then call CreateProcess like so:

  CreateProcess (Nil, "cdrecord -atip", Nil, Nil, True,
    CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, Nil,
    PChar(WorkingDir), StartupInfo, ProcessInformation);

where WorkingDir is the working directory for "cdrecord".

7.  Retrieve the handles:

var
  ProcessHandle, ThreadHandle: THandle;
begin
  ProcessHandle := ProcessInformation.hProcess;
  ThreadHandle := ProcessInformation.hThread;

8.  Wait for it complete.

  if WaitForSingleObject (ProcessHandle, INFINITE) = WAIT_OBJECT_0 then

9.  Close the output file and other handles.  It is important to close the
process and thread handles because Windows will not do that automatically
for you.

  CloseHandle (hOutput); // To make sure all data is flushed to disk
  CloseHandle (ProcessHandle);
  CloseHandle (ThreadHandle);

10.  You can now read the contents of the temporary file (eg. open it using
a TFileStream), and delete it once you are done.

HTH,
Dennis.

----- Original Message -----
From: "Alistair George" <[EMAIL PROTECTED]>
To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]>
Sent: Thursday, June 26, 2003 3:32 PM
Subject: [DUG]: Conversion


> Hi all.
> Can anyone translate this to pascal for me?
>
> FILE *fp
> fp=popen ("cdrecord -atip", "r")
> fread (fp) //gets the output
>
> --------------------------------------------------------------------------
-
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
>
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to