Cannot link exe using spawnlp()

This is a discussion on Cannot link exe using spawnlp() within the C++ Programming forums, part of the General Programming Boards category; I have 2 programs (1) Program P1.EXE, which inputs a name from user, calls P2.EXE using spawnlp() and passes the ...

  1. #1
    I'm Back
    Join Date
    Dec 2001
    Posts
    556

    Question Cannot link exe using spawnlp()

    I have 2 programs

    (1) Program P1.EXE, which inputs a name from user, calls P2.EXE using spawnlp() and passes the name as an argument

    (2) Program P2.EXE which takes the name passed from P1.EXE as an argument and displays it

    My problem is that

    (1) when i use P_WAIT in the spawn() it doesnt link to P2.EXE but when i use P_OVERLAY it does?? The programs are not big for any memory error to happen

    (2) and when it links, the argument name is not passed on to P2.EXE


    heres the code

    CODE FOR P1.EXE
    Code:
    #include<iostream.h>
    #include<process.h>
    #include<conio.h>
    
    int main()
    {
    char name[21];
    int retval;
    
    clrscr();
    
    cout<<"This is P1.EXE\n";
    
    cout<<"Enter your name : ";
    cin.getline(name,20,'\n');
    
    cout<<"P1.EXE will now try to link to P2.EXE with your name passed on as an argument.\n";
    
    retval=spawnlp(P_OVERLAY,"P2.EXE",name,NULL);
    
    if(retval==0)
     cout<<"Linking with P2.EXE was successful.\n";
    else
     cout<<"Linking with P2.EXE was unsuccessful.\n";
    
    cout<<"Exiting P1.EXE\n\n";
    return 0;
    }

    CODE FOR P2.EXE
    Code:
    #include<iostream.h>
    
    int main(int argc, char *argv[])
    {
     int retval;
    
     cout<<"**************\n";
     cout<<"This is P2.EXE \n";
     cout<<"Link from P1.EXE established.\n";
    
     //errchk
     cout<<"\t\tARGC : "<<argc<<"\n";
     for(int i=0;i<argc;i++)
      cout<<"\t\tARGV["<<i<<"] : "<<argv[i]<<"\n";
    
     switch(argc)
     {
     case 2:
    	cout<<"Your name is : "<<argv[1]<<"\n";
    	retval=0;
     break;
     case 3:
    	cout<<"Your name is : "<<argv[1]<<" "<<argv[2]<<"\n";
    	retval=0;
     break;
     default:
    	cout<<"Argument format is not correct : P2.EXE (first_name) (last_name)\n";
    	retval=-1;
     break;
     }
     cout<<"Exiting P2.EXE\n";
     cout<<"**************\n";
    
     return retval;
    }

    Can anyone shed some light on this problem??

    Heres the zip also...
    Attached Files Attached Files

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Any coding tips, strict coding No-Nos visible etc are also welcome

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > (1) when i use P_WAIT in the spawn() it doesnt link to P2.EXE but when i use P_OVERLAY it does?
    Works for me - but that might be because I've also fixed...

    > (2) and when it links, the argument name is not passed on to P2.EXE
    Read the manual
    int spawnlp(int mode, const char *path, const char *argv0, ..., NULL);
    It starts with argv0 - the name of the program

    So
    retval=spawnlp(P_WAIT,"P2.EXE","P2.EXE",name,NULL) ;

    > cin.getline(name,20,'\n');
    Whilst you can input a name with spaces, when it gets to P2, it will all be in argv[1]

    If you want to make it act like a command line invocation, then you've got to split 'name' into words yourself (and then use spawnv)

    > strict coding No-Nos visible etc are also welcome
    returning -1 from main

    If successful and `mode' is `P_WAIT', these functions return the exit
    code of the child process in the lower 8 bits of the return value.
    In this case, your -1 would transform into a status of 255

  4. #4
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Thanks a lot, Salem

    I dont know how i missed it I checked the format so many times.

    >>Whilst you can input a name with spaces, when it gets to P2, it will all be in argv[1]

    Well, here the name gets seperated in to argv[1] and argv[2] if i use firstname and lastname, meaning if the name entered in the form of "Mr XYZ" then
    argv[0] - P2.EXE
    argv[1] - Mr
    argv[2] - XYZ


    Another problem i'm having is that how do i return back to P1.EXE after P2.EXE exits using P_OVERLAY. Is it possible??

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    Well the separation into words happens in P1 not P2

    I think you need to do something like this
    Code:
        char *args[10];     // array of upto 10 args
        char *p;
        int   i;
        args[0] = "P2.EXE"; // first is program name
        for ( i = 1, p = strtok(name," ");
              p != NULL && i < 9;
              i++, p = strtok(NULL," ") ) {
            args[i] = p;    // separate input line into words
        }
        args[i] = NULL;     // last is NULL
        retval=spawnv(P_WAIT,args[0], args);
    > how do i return back to P1.EXE after P2.EXE exits using P_OVERLAY
    You don't - you must use P_WAIT if you want to suspend P1 while P2 runs, then resume P1 when P2 exits.

    OVERLAY means replace the calling program with the called program - there is no way back from that.

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Thanks for the info again Salem


    >>OVERLAY means replace the calling program with the called program - there is no way back from that.

    I thought that in OVERLAY the called program takes the memory space used by the parent program when it is called. And after it finshes the parent program regains the memory set for it.
    Thereby less memory is used for running both the processes.

    I might be wrong though..


    OK Here's a question for you : How did you get P_WAIT to run??

    It seems that i can use only P_OVERLAY. Could it be a compiler thing. I am making this program using the old Turbo c++ 3 compiler

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > Could it be a compiler thing
    Yeah, because it works just fine here with DJGPP

    Stop using that old fossil

  8. #8
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Originally posted by Salem
    > Could it be a compiler thing
    Yeah, because it works just fine here with DJGPP

    Stop using that old fossil

    Yeah, i know but i kinda like it. conio/dos/dir/bios nice functions in them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 05:23 AM
  2. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. How to link bpl files into exe?
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-22-2002, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21