Running .EXEs From A String Path

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compman9902
    New Member
    • Mar 2007
    • 105

    Running .EXEs From A String Path

    Hello, and thank you for your interest in this disscussion. Here is my problem: I need to be able to run .EXE files from my console-based application. Here is the biggest bump in the road: The path is in a string variable. How do I run it? Please post code. Thank you.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by compman9902
    Hello, and thank you for your interest in this disscussion. Here is my problem: I need to be able to run .EXE files from my console-based application. Here is the biggest bump in the road: The path is in a string variable. How do I run it? Please post code. Thank you.
    Take a look at exec* and spawn* functions families.

    I'm prety sure that u can do this all by u self! ;)

    Savage

    Comment

    • compman9902
      New Member
      • Mar 2007
      • 105

      #3
      Originally posted by Savage
      Take a look at exec* and spawn* functions families.

      I'm prety sure that u can do this all by u self! ;)

      Savage
      Any particular direction you'd like to point me in?

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by compman9902
        Any particular direction you'd like to point me in?
        Now,that depends on u:Do u wish that ur main programme terminate when another app is calld or not?

        If u wish to terminate ur main programm use execlp,it allows u to serach for app in that directory specifed by path(e.g)

        char *path="C:\\WIND OWS\\NOTEPAD.EX E;
        execlp(path,pat h,NULL);

        Savage

        Comment

        • compman9902
          New Member
          • Mar 2007
          • 105

          #5
          Originally posted by Savage
          Now,that depends on u:Do u wish that ur main programme terminate when another app is calld or not?

          If u wish to terminate ur main programm use execlp,it allows u to serach for app in that directory specifed by path(e.g)

          char *path="C:\\WIND OWS\\NOTEPAD.EX E;
          execlp(path,pat h,NULL);

          Savage
          what include do I use?

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by compman9902
            what include do I use?
            If u are using windows include process.h if u are using unix include unistd.h

            Savage

            Comment

            • compman9902
              New Member
              • Mar 2007
              • 105

              #7
              Originally posted by Savage
              If u are using windows include process.h if u are using unix include unistd.h

              Savage
              This is just not working for me. Could you just point me tword a web tutorial for this command?

              Comment

              • nearestniladri2003
                New Member
                • May 2007
                • 11

                #8
                Enable your program to load and run other files (child processes)

                Use these execl... functions when you know how many separate arguments
                you'll have:

                Declaration:
                int execl (char *path, char *arg0, ..., NULL);
                int execle (char *path, char *arg0, ..., NULL, char **env);
                int execlp (char *path, char *arg0, ...);
                int execlpe(char *path, char *arg0, ..., NULL, char **env);

                Use these execv... functions when you don't know in advance how many
                separate arguments you will have:

                Declaration:
                int execv (char *path, char *argv[]);
                int execve (char *path, char *argv[], char **env);
                int execvp (char *path, char *argv[]);
                int execvpe(char *path, char *argv[], char **env);

                Remarks:
                When an exec... call succeeds, the child process overlays the parent
                process. There must be sufficient memory available for loading and executing
                the child process.

                The l, v, e, and p suffixes added to "exec" specify certain capabilities of
                the exec... function.

                Argument ³ What It Is/Does
                path ³ File name of the called child process
                argN ³ Argument pointer(s) passed as separate arguments
                argv[N] ³ Argument pointer(s) passed as an array of pointers
                env ³ Array of character pointers.

                The exec... functions search for path using the
                standard DOS search algorithm.

                When an exec... function call is made, any open files remain open in the
                child process.

                Return Value:
                On success, the functions do not return.
                On error, the functions return -1 and set
                errno to one of the following:
                E2BIG Arg list too long
                EACCES Permission denied
                EMFILE Too many open files
                ENOENT Path or file name not found
                ENOEXEC Exec format error
                ENOMEM Not enough core

                ***************
                execv example
                ***************
                #include <process.h>
                #include <stdio.h>
                #include <errno.h>

                int main(int argc, char *argv[])
                {
                int i;

                printf("Command line arguments:\n");
                for (i=0; i < argc; i++)
                printf("[%2d] : %s\n", i, argv[i]);

                printf("About to exec child with arg1 arg2 ...\n");
                execv("CHILD.EX E", argv);

                perror("exec error");
                exit(1);
                return 0;
                }

                Comment

                • compman9902
                  New Member
                  • Mar 2007
                  • 105

                  #9
                  Originally posted by nearestniladri2 003
                  Enable your program to load and run other files (child processes)

                  Use these execl... functions when you know how many separate arguments
                  you'll have:

                  Declaration:
                  int execl (char *path, char *arg0, ..., NULL);
                  int execle (char *path, char *arg0, ..., NULL, char **env);
                  int execlp (char *path, char *arg0, ...);
                  int execlpe(char *path, char *arg0, ..., NULL, char **env);

                  Use these execv... functions when you don't know in advance how many
                  separate arguments you will have:

                  Declaration:
                  int execv (char *path, char *argv[]);
                  int execve (char *path, char *argv[], char **env);
                  int execvp (char *path, char *argv[]);
                  int execvpe(char *path, char *argv[], char **env);

                  Remarks:
                  When an exec... call succeeds, the child process overlays the parent
                  process. There must be sufficient memory available for loading and executing
                  the child process.

                  The l, v, e, and p suffixes added to "exec" specify certain capabilities of
                  the exec... function.

                  Argument ³ What It Is/Does
                  path ³ File name of the called child process
                  argN ³ Argument pointer(s) passed as separate arguments
                  argv[N] ³ Argument pointer(s) passed as an array of pointers
                  env ³ Array of character pointers.

                  The exec... functions search for path using the
                  standard DOS search algorithm.

                  When an exec... function call is made, any open files remain open in the
                  child process.

                  Return Value:
                  On success, the functions do not return.
                  On error, the functions return -1 and set
                  errno to one of the following:
                  E2BIG Arg list too long
                  EACCES Permission denied
                  EMFILE Too many open files
                  ENOENT Path or file name not found
                  ENOEXEC Exec format error
                  ENOMEM Not enough core

                  ***************
                  execv example
                  ***************
                  #include <process.h>
                  #include <stdio.h>
                  #include <errno.h>

                  int main(int argc, char *argv[])
                  {
                  int i;

                  printf("Command line arguments:\n");
                  for (i=0; i < argc; i++)
                  printf("[%2d] : %s\n", i, argv[i]);

                  printf("About to exec child with arg1 arg2 ...\n");
                  execv("CHILD.EX E", argv);

                  perror("exec error");
                  exit(1);
                  return 0;
                  }
                  Okay, all I need is to make a seperate program run from my c++ console based program, where the file path is in a string variable.

                  Comment

                  • compman9902
                    New Member
                    • Mar 2007
                    • 105

                    #10
                    Originally posted by compman9902
                    Okay, all I need is to make a seperate program run from my c++ console based program, where the file path is in a string variable.
                    The issue has been resolved, thank you for all of your help
                    all I did was add quotes to the beggining and end of the path, then make the path into an "old style string", then put it into the system command. Easy, simple, and working.

                    Comment

                    Working...