Thread: using spawnl for a dos program

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    using spawnl for a dos program

    I've been working on this program in which I want to use spawnl to run another app. My question is how can I assign the spawned program to a variable? For example, I will read in the program to spawn in from a cfg file (text.)

    void runProgram(char fileName[])
    {
    int flag;

    flag = spawnl(P_WAIT, fileName, NULL);

    if(flag == -1)
    blah

    The fileName is read in from the file and passed to runProgram after using strlen to make sure the name is valid. In the cfg file, the name appears as something like "C:\DOS\EDIT.COM"

    I've also tried spawnlp but I am still unable to process the request. Do I need to allocate additional memory or include the environment? If so, how exactly do I do that? The error returned is always ENOENT. If I move EDIT.COM into the working directory and use spawnl(P_WAIT, "EDIT.COM", NULL) it works, but this isn't what my program needs. Can anyone help?

    TIA
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    You are not passing the argument argv[0] to the function spawnl().
    It should be like:
    Code:
    flag = spawnl(P_WAIT, fileName, progname, NULL);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In addition to what shaik786 said

    > and use spawnl(P_WAIT, "EDIT.COM", NULL) it works,
    spawn functions do not search your PATH environment looking for directories containing the program you want to run

    So it needs to be
    spawnl(P_WAIT, "C:\\DOS\\EDIT.COM", "C:\\DOS\\EDIT.COM", NULL);

    or
    Code:
    void fn ( char *prog ) {
        spawnl(P_WAIT, prog, prog, NULL);
    }
    
    fn ( "C:\\DOS\\EDIT.COM" );

  4. #4
    Unregistered
    Guest
    Ok if I understand this right, I need to pass the program name twice? I did something wrong as I got a compiler error using a pointer to the arguments, so I removed the pointers and it compiled.

    Ignoring the correct syntax for a momment, how exactly would I do the following? If this were the dos command line:

    pkunzip -v somezip.zip

    The program reads in the cfg file as

    2 7 c:\pkware\pkunzip.exe -v c:\zips\somezip.zip

    I limit each char [] to a size of 30 so not to exceed the total size allowed by spawn.

    The 2 and 7 are ints that set the bg and fg colors and are picked up prior to reading the important part. I wrote the function as

    void getMenu(char fileName[], char arg1[], char arg2[])

    spawnl(P_WAIT, fileName, arg1, arg2, NULL);

    This sort of worked; it ran pkunzip but extracted files. It didn't recognize the -v switch, but it did unzip the file named in arg2. It also did some odd things if the input file string wasn't formatted just so. So would I use this then?

    spawnl(P_WAIT, fileName, fileName, arg1, arg2, NULL) ?

    This is my first experience with this sort of thing. Thanks for the replies.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    sheeeese the board didn't allow me more than 2 minutes to write a post before it logged me out. The above reply is mine.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I need to pass the program name twice?
    Sort of

    The first filename is the absolute path to the program you want to run.

    The second filename (and all the other parameters) are used to create the 'C' argv[] to pass to main. By convention, argv[0] is a copy of the program path, but it need not be the complete path.

    So these would be valid
    spawnl(P_WAIT, "C:\\DOS\\EDIT.COM", "C:\\DOS\\EDIT.COM", NULL);
    spawnl(P_WAIT, "C:\\DOS\\EDIT.COM", "EDIT.COM", NULL);


    > spawnl(P_WAIT, fileName, fileName, arg1, arg2, NULL) ?
    Yes - seems good to me

    I suggest you try compiling this program, then calling it via spawn to see what I mean
    Code:
    #include <stdio.h>
    int main ( int argc, char *argv[] ) {
        int i;
        for ( i = 0 ; i < argc ; i++ ) {
            printf( "Arg %d = `%s`\n", i, argv[i] );
        }
        return 0;
    }

  7. #7
    Unregistered
    Guest
    Thanks to all that have replied, and salem, I *think* that I now have a better understanding of what's going on here. Forgive my coding methods, but it's a habit that I cannot break

    What do you guys think of this? I'm still missing something here and I think it's the particular spawning member that I'm using. Spawnl is used when I know the exact number of arguments that are to be entered right? But what if the program doesn't know the exact arguements? Here is a really stripped down version of what I've got done.

    Code:
    // .h includes //
    
    void spawnMe(char *, char *, char *);
    
    int main()
    {
         int counter, ch;
         char *buff1, *buff2, *buff3;
         char fname[] = "applist.cfg";
    
         ifstream inFile;  // can't figure out how to use FILE *string :P
    
         inFile.open(fname, ios::in);
    
         if(inFile.fail())
         {
             blah
         }
        
         counter = 1;
    
         while((ch = inFile.peek()) != EOF)
         {
              inFile >> buff1 >> buff2 >> buff3;
              printf("\nTask [%d] press any key to launch %s ", counter, buff1);
              getch();
              spawnMe(buff1, buff2, buff3);
              counter++;
         }
    
         printf("\nThe End.");
    
         return 0;
    }
    
    void spawnMe(char *arg0, char *arg1, char *arg2)
    {
         int flag;
    
         flag = spawnl(P_WAIT, arg0, arg0, arg1, arg2, NULL);
    
         if(flag == -1)
           // error message
    }
    The structure of the cfg file is

    c:\pkware\pkzip.exe myzip.zip *.*
    c:\pkware\pkunzip.exe -v myzip.zip
    c:\windows\command\edit.com mytext.txt NULL
    c:\windows\command\chkdsk.exe NULL NULL

    How can I get rid of having to add NULL in the cfg file when I don't need the extra argument? I was rather stunned to see this work without hanging up at some point. Thanks for your help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Compiling a program in VC++ and run it in DOS
    By Willhunting in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-18-2003, 04:09 AM
  5. initialising a DOS program from a C enviroment
    By Robert_Ingleby in forum C Programming
    Replies: 5
    Last Post: 03-07-2002, 01:53 PM