Welcome, Guest
Guest Settings
Help

Thread: Linux - execv function call.


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Apr 15, 2017 1:02 PM Last Post By: Matthew Vesperman Threads: [ Previous | Next ]
Matthew Vesperman

Posts: 7
Registered: 1/11/01
Linux - execv function call.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 15, 2017 11:00 AM
Hello,

I am trying to import the execv function from libc.

The C header definition is

int execv(const char *path, char *const argv[]);

So far I have this:

function _execv(const Command: MarshaledAString; const ArgV: Pointer): Integer; cdecl; external libc name _PU + 'execv';

The command parameter works fine. The problem I am having is the array parameter for the parameters to the command.
Anyone have any suggestions on how to do this?
Matthew Vesperman

Posts: 7
Registered: 1/11/01
Re: Linux - execv function call.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 15, 2017 1:02 PM   in response to: Matthew Vesperman in response to: Matthew Vesperman
Hello All,

I finally figured out how to do this. Below is the code to make this work.

program Project1;
 
{$APPTYPE CONSOLE}
 
{$R *.res}
 
uses
  Posix.Base,
  System.SysUtils,
  Posix.Stdlib;
 
 
function _execv(const Command: MarshaledAString; const ArgV: Pointer): Integer; cdecl; external libc name _PU + 'execv';
function _fork (): Integer; cdecl; external libc name _PU + 'fork';
 
 
var
  pid: Integer;
  Args: array[0..2] of Pointer;
  Junk: string;
 
begin
  pid := _fork;
 
  Args[0] := MarshaledAString('/usr/bin/gedit');
  Args[1] := MarshaledAString('~/test.txt');
  Args[2] := MarshaledAString(#0#0);
 
  if pid = 0 then
    begin
      _execv('/usr/bin/gedit', @Args[0]);
      halt;
    end;
 
  writeln('Hello. I am the parent process. I am going to wait some time...');
  sleep(3000);
  writeln('Close parent process.');
  writeln('Bye.');
  Writeln('Press ENTER to continue.');
  Readln(Junk);
end.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02